[4920] | 1 | :mod:`waeup.sirp.utils.helpers` -- Helpers for the WAeUP SRP |
---|
| 2 | ************************************************************ |
---|
[4189] | 3 | |
---|
[4920] | 4 | .. module:: waeup.sirp.utils.helpers |
---|
[4377] | 5 | |
---|
| 6 | Helper functions for the WAeUP SRP. |
---|
| 7 | |
---|
[5140] | 8 | .. :doctest: |
---|
[4189] | 9 | |
---|
[7186] | 10 | :func:`remove_file_or_directory` |
---|
| 11 | ================================ |
---|
[4189] | 12 | |
---|
[7186] | 13 | .. function:: remove_file_or_directory(path) |
---|
[4189] | 14 | |
---|
[4377] | 15 | Removes a file or directory given by a path. We can remove files: |
---|
[4189] | 16 | |
---|
[4377] | 17 | >>> import os |
---|
[7186] | 18 | >>> from waeup.sirp.utils.helpers import remove_file_or_directory |
---|
[4377] | 19 | >>> open('blah', 'wb').write('nonsense') |
---|
| 20 | >>> 'blah' in os.listdir('.') |
---|
| 21 | True |
---|
[4189] | 22 | |
---|
[7186] | 23 | >>> remove_file_or_directory('blah') |
---|
[4377] | 24 | >>> 'blah' in os.listdir('.') |
---|
| 25 | False |
---|
[4189] | 26 | |
---|
[4377] | 27 | We can remove directories: |
---|
[4189] | 28 | |
---|
[4377] | 29 | >>> os.mkdir('blah') |
---|
| 30 | >>> 'blah' in os.listdir('.') |
---|
| 31 | True |
---|
[4189] | 32 | |
---|
[7186] | 33 | >>> remove_file_or_directory('blah') |
---|
[4377] | 34 | >>> 'blah' in os.listdir('.') |
---|
| 35 | False |
---|
[4189] | 36 | |
---|
| 37 | |
---|
[7186] | 38 | :func:`copy_filesystem_tree` |
---|
| 39 | ============================ |
---|
[4189] | 40 | |
---|
[7186] | 41 | .. function:: ccopy_filesystem_tree(src_path, dst_path[, overwrite=False[, del_old=False]]) |
---|
[4189] | 42 | |
---|
[4377] | 43 | Copies the contents of an (existing) directory to another |
---|
| 44 | (existing) directory. |
---|
[4189] | 45 | |
---|
[4377] | 46 | :param src_path: filesystem path to copy from |
---|
| 47 | :type src_path: string |
---|
| 48 | :param dst_path: filesystem path to copy to |
---|
| 49 | :type dst_path: string |
---|
| 50 | :keyword overwrite: Whether exiting files with same names should be |
---|
| 51 | overwritten. |
---|
| 52 | :type overwrite: bool |
---|
| 53 | :keyword del_old: Whether old contents in destination path should be |
---|
| 54 | removed. |
---|
| 55 | :type del_old: bool |
---|
| 56 | :return: List of non-copied files |
---|
| 57 | |
---|
| 58 | Both directories must exist. |
---|
[4189] | 59 | |
---|
[4377] | 60 | Unix hidden files and directories (starting with '.') are not |
---|
| 61 | processed by this function. |
---|
[4189] | 62 | |
---|
[4377] | 63 | Without any further parameters, we can copy complete file trees: |
---|
[4189] | 64 | |
---|
[4377] | 65 | >>> os.mkdir('src') |
---|
| 66 | >>> os.mkdir('dst') |
---|
| 67 | >>> open(os.path.join('src', 'blah'), 'wb').write('nonsense') |
---|
[4189] | 68 | |
---|
[7186] | 69 | >>> from waeup.sirp.utils.helpers import copy_filesystem_tree |
---|
| 70 | >>> result = copy_filesystem_tree('src', 'dst') |
---|
[4189] | 71 | |
---|
[4377] | 72 | As a result we get a list of non-copied files: |
---|
[4189] | 73 | |
---|
[4377] | 74 | >>> result |
---|
| 75 | [] |
---|
[4189] | 76 | |
---|
[4377] | 77 | The created file was indeed copied: |
---|
[4189] | 78 | |
---|
[4377] | 79 | >>> 'blah' in os.listdir('dst') |
---|
| 80 | True |
---|
| 81 | |
---|
| 82 | Hidden files (i.e. such starting with a dot) are not copied: |
---|
| 83 | |
---|
| 84 | >>> open(os.path.join('src', '.blah'), 'wb').write('nonsense') |
---|
[7186] | 85 | >>> result = copy_filesystem_tree('src', 'dst') |
---|
[4377] | 86 | >>> '.blah' in os.listdir('dst') |
---|
| 87 | False |
---|
| 88 | |
---|
| 89 | This function supports some keyword parameters as explained below. |
---|
| 90 | |
---|
| 91 | Using ``overwrite`` |
---|
| 92 | ------------------- |
---|
| 93 | |
---|
[4189] | 94 | Boolean. If set to ``True``, any existing and same named files and |
---|
| 95 | directories in the destination dir are overwritten with copies from |
---|
| 96 | the source. Default is `False`. |
---|
| 97 | |
---|
| 98 | Normally, existing same named files in the destination are not |
---|
| 99 | overwritten: |
---|
| 100 | |
---|
| 101 | >>> open(os.path.join('src', 'blah'), 'wb').write('newnonsense') |
---|
[7186] | 102 | >>> result = copy_filesystem_tree('src', 'dst') |
---|
[4189] | 103 | >>> open(os.path.join('dst', 'blah'), 'rb').read() |
---|
| 104 | 'nonsense' |
---|
| 105 | |
---|
| 106 | Instead the filename is added to the result (a list of non-copied |
---|
| 107 | files): |
---|
| 108 | |
---|
| 109 | >>> result |
---|
| 110 | ['blah'] |
---|
| 111 | |
---|
| 112 | If, however, we use `overwrite`, the existing file will be |
---|
| 113 | overwritten: |
---|
| 114 | |
---|
[7186] | 115 | >>> result = copy_filesystem_tree('src', 'dst', overwrite=True) |
---|
[4189] | 116 | >>> open(os.path.join('dst', 'blah'), 'rb').read() |
---|
| 117 | 'newnonsense' |
---|
| 118 | |
---|
| 119 | >>> result |
---|
| 120 | [] |
---|
| 121 | |
---|
| 122 | This also works for complete directories: |
---|
| 123 | |
---|
| 124 | >>> os.mkdir(os.path.join('src', 'mydir')) |
---|
| 125 | >>> os.mkdir(os.path.join('dst', 'mydir')) |
---|
| 126 | >>> open(os.path.join( |
---|
| 127 | ... 'src', 'mydir', 'blah'), 'wb').write('srcblah') |
---|
| 128 | >>> open(os.path.join( |
---|
| 129 | ... 'dst', 'mydir', 'blah'), 'wb').write('dstblah') |
---|
| 130 | |
---|
[7186] | 131 | >>> result = copy_filesystem_tree('src', 'dst', overwrite=True) |
---|
[4189] | 132 | >>> open(os.path.join('dst', 'mydir', 'blah'), 'rb').read() |
---|
| 133 | 'srcblah' |
---|
| 134 | |
---|
| 135 | |
---|
[4377] | 136 | Using ``del_old`` |
---|
| 137 | ----------------- |
---|
[4189] | 138 | |
---|
| 139 | Boolean. If set to ``True``, any copied files and directories will be |
---|
| 140 | removed from the src dir. Default is `False`. |
---|
| 141 | |
---|
[7186] | 142 | >>> result = copy_filesystem_tree('src', 'dst', overwrite=True, |
---|
[4189] | 143 | ... del_old=True) |
---|
| 144 | >>> os.listdir('src') |
---|
| 145 | ['.blah'] |
---|
| 146 | |
---|
| 147 | All files and directories are removed from src, except the hidden file |
---|
| 148 | we created in the beginning. |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | Clean up: |
---|
| 152 | |
---|
[7186] | 153 | >>> remove_file_or_directory('src') |
---|
| 154 | >>> remove_file_or_directory('dst') |
---|
[4376] | 155 | |
---|
[4377] | 156 | |
---|
[7186] | 157 | :func:`get_inner_HTML_part()` |
---|
| 158 | ============================= |
---|
[4376] | 159 | |
---|
[7186] | 160 | .. function:: get_inner_HTML_part(html_code) |
---|
[4376] | 161 | |
---|
| 162 | Get the 'inner' part out of a piece of HTML code. |
---|
| 163 | |
---|
| 164 | Helper function mainly to extract 'real content' from already |
---|
| 165 | rendered forms. |
---|
| 166 | |
---|
| 167 | The term 'inner part' here means the ``<form>`` part of an HTML |
---|
| 168 | snippet. If this cannot be found, we look for a ``<body>`` part and |
---|
| 169 | if this cannot be found as well, we simply return the whole input. |
---|
| 170 | |
---|
| 171 | If a ``<form>`` part can be found in an HTML snippet, this is |
---|
| 172 | returned with all preceeding/following stuff stripped: |
---|
| 173 | |
---|
[7186] | 174 | >>> from waeup.sirp.utils.helpers import get_inner_HTML_part |
---|
| 175 | >>> print get_inner_HTML_part("""<html> |
---|
[4376] | 176 | ... <head> |
---|
| 177 | ... </head> |
---|
| 178 | ... <BLANKLINE> |
---|
| 179 | ... <body> |
---|
| 180 | ... <form action="http://localhost/myuniversity/faculties/TF/add" |
---|
| 181 | ... method="post" class="edit-form" |
---|
| 182 | ... enctype="multipart/form-data"> |
---|
| 183 | ... <h1>Add a department</h1> |
---|
| 184 | ... </form> |
---|
| 185 | ... </body> |
---|
| 186 | ... </html> |
---|
| 187 | ... """) |
---|
| 188 | <BLANKLINE> |
---|
| 189 | <form action="http://localhost/myuniversity/faculties/TF/add" |
---|
| 190 | method="post" class="edit-form" |
---|
| 191 | enctype="multipart/form-data"> |
---|
| 192 | <BLANKLINE> |
---|
| 193 | <h1>Add a department</h1> |
---|
| 194 | </form> |
---|
| 195 | <BLANKLINE> |
---|
| 196 | <BLANKLINE> |
---|
| 197 | |
---|
| 198 | If there is no ``<form>`` part, try to find any ``<body>`` part: |
---|
| 199 | |
---|
[7186] | 200 | >>> print get_inner_HTML_part("""<html> |
---|
[4376] | 201 | ... <head> |
---|
| 202 | ... </head> |
---|
| 203 | ... <BLANKLINE> |
---|
| 204 | ... <body> |
---|
| 205 | ... <div>Some content</div> |
---|
| 206 | ... </body> |
---|
| 207 | ... </html> |
---|
| 208 | ... """) |
---|
| 209 | <BLANKLINE> |
---|
| 210 | <div>Some content</div> |
---|
| 211 | <BLANKLINE> |
---|
| 212 | |
---|
| 213 | If there is also no ``<body>`` tag, we return the input as-is: |
---|
| 214 | |
---|
[7186] | 215 | >>> print get_inner_HTML_part("""<div> |
---|
[4376] | 216 | ... <div>Some content</div> |
---|
| 217 | ... </div> |
---|
| 218 | ... """) |
---|
| 219 | <div> |
---|
| 220 | <div>Some content</div> |
---|
| 221 | </div> |
---|