source: main/waeup.sirp/trunk/src/waeup/sirp/utils/helpers.txt @ 5110

Last change on this file since 5110 was 4920, checked in by uli, 15 years ago

Make unit tests run again with the new package layout.

File size: 5.9 KB
Line 
1:mod:`waeup.sirp.utils.helpers` -- Helpers for the WAeUP SRP
2************************************************************
3
4.. module:: waeup.sirp.utils.helpers
5
6Helper functions for the WAeUP SRP.
7
8:Test-Layer: unit
9
10:func:`removeFileOrDirectory`
11=============================
12
13.. function:: removeFileOrDirectory(path)
14
15   Removes a file or directory given by a path. We can remove files:
16
17     >>> import os
18     >>> from waeup.sirp.utils.helpers import removeFileOrDirectory
19     >>> open('blah', 'wb').write('nonsense')
20     >>> 'blah' in os.listdir('.')
21     True
22
23     >>> removeFileOrDirectory('blah')
24     >>> 'blah' in os.listdir('.')
25     False
26
27   We can remove directories:
28
29     >>> os.mkdir('blah')
30     >>> 'blah' in os.listdir('.')
31     True
32
33     >>> removeFileOrDirectory('blah')
34     >>> 'blah' in os.listdir('.')
35     False
36
37
38:func:`copyFileSystemTree`
39==========================
40
41.. function:: copyFileSystemTree(src_path, dst_path[, overwrite=False[, del_old=False]])
42
43   Copies the contents of an (existing) directory to another
44   (existing) directory.
45
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.
59
60   Unix hidden files and directories (starting with '.') are not
61   processed by this function.
62
63   Without any further parameters, we can copy complete file trees:
64
65     >>> os.mkdir('src')
66     >>> os.mkdir('dst')
67     >>> open(os.path.join('src', 'blah'), 'wb').write('nonsense')
68
69     >>> from waeup.sirp.utils.helpers import copyFileSystemTree
70     >>> result = copyFileSystemTree('src', 'dst')
71
72   As a result we get a list of non-copied files:
73
74     >>> result
75     []
76
77   The created file was indeed copied:
78
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')
85     >>> result = copyFileSystemTree('src', 'dst')
86     >>> '.blah' in os.listdir('dst')
87     False
88
89   This function supports some keyword parameters as explained below.
90
91Using ``overwrite``
92-------------------
93
94Boolean. If set to ``True``, any existing and same named files and
95directories in the destination dir are overwritten with copies from
96the source. Default is `False`.
97
98Normally, existing same named files in the destination are not
99overwritten:
100
101    >>> open(os.path.join('src', 'blah'), 'wb').write('newnonsense')
102    >>> result = copyFileSystemTree('src', 'dst')
103    >>> open(os.path.join('dst', 'blah'), 'rb').read()
104    'nonsense'
105
106Instead the filename is added to the result (a list of non-copied
107files):
108
109    >>> result
110    ['blah']
111
112If, however, we use `overwrite`, the existing file will be
113overwritten:
114
115    >>> result = copyFileSystemTree('src', 'dst', overwrite=True)
116    >>> open(os.path.join('dst', 'blah'), 'rb').read()
117    'newnonsense'
118
119    >>> result
120    []
121
122This 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
131    >>> result = copyFileSystemTree('src', 'dst', overwrite=True)
132    >>> open(os.path.join('dst', 'mydir', 'blah'), 'rb').read()
133    'srcblah'
134
135
136Using ``del_old``
137-----------------
138
139Boolean. If set to ``True``, any copied files and directories will be
140removed from the src dir. Default is `False`.
141
142    >>> result = copyFileSystemTree('src', 'dst', overwrite=True,
143    ...                                           del_old=True)
144    >>> os.listdir('src')
145    ['.blah']
146
147All files and directories are removed from src, except the hidden file
148we created in the beginning.
149
150
151Clean up:
152
153    >>> removeFileOrDirectory('src')
154    >>> removeFileOrDirectory('dst')
155
156
157:func:`getInnerHTMLPart()`
158==========================
159
160.. function:: getInnerHTMLPart(html_code)
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
174     >>> from waeup.sirp.utils.helpers import getInnerHTMLPart
175     >>> print getInnerHTMLPart("""<html>
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
200     >>> print getInnerHTMLPart("""<html>
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
215     >>> print getInnerHTMLPart("""<div>
216     ...  <div>Some content</div>
217     ... </div>
218     ... """)
219     <div>
220      <div>Some content</div>
221     </div>
Note: See TracBrowser for help on using the repository browser.