source: main/waeup.kofa/branches/henrik-transcript-workflow/src/waeup/kofa/doctests/helpers.txt @ 17669

Last change on this file since 17669 was 12951, checked in by Henrik Bettermann, 10 years ago

Simplify headlines in doctests. Remove some API documentation.

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