:mod:`waeup.sirp.utils.helpers` -- Helpers for the WAeUP SRP ************************************************************ .. module:: waeup.sirp.utils.helpers Helper functions for the WAeUP SRP. :Test-Layer: unit :func:`removeFileOrDirectory` ============================= .. function:: removeFileOrDirectory(path) Removes a file or directory given by a path. We can remove files: >>> import os >>> from waeup.sirp.utils.helpers import removeFileOrDirectory >>> open('blah', 'wb').write('nonsense') >>> 'blah' in os.listdir('.') True >>> removeFileOrDirectory('blah') >>> 'blah' in os.listdir('.') False We can remove directories: >>> os.mkdir('blah') >>> 'blah' in os.listdir('.') True >>> removeFileOrDirectory('blah') >>> 'blah' in os.listdir('.') False :func:`copyFileSystemTree` ========================== .. function:: copyFileSystemTree(src_path, dst_path[, overwrite=False[, del_old=False]]) Copies the contents of an (existing) directory to another (existing) directory. :param src_path: filesystem path to copy from :type src_path: string :param dst_path: filesystem path to copy to :type dst_path: string :keyword overwrite: Whether exiting files with same names should be overwritten. :type overwrite: bool :keyword del_old: Whether old contents in destination path should be removed. :type del_old: bool :return: List of non-copied files Both directories must exist. Unix hidden files and directories (starting with '.') are not processed by this function. Without any further parameters, we can copy complete file trees: >>> os.mkdir('src') >>> os.mkdir('dst') >>> open(os.path.join('src', 'blah'), 'wb').write('nonsense') >>> from waeup.sirp.utils.helpers import copyFileSystemTree >>> result = copyFileSystemTree('src', 'dst') As a result we get a list of non-copied files: >>> result [] The created file was indeed copied: >>> 'blah' in os.listdir('dst') True Hidden files (i.e. such starting with a dot) are not copied: >>> open(os.path.join('src', '.blah'), 'wb').write('nonsense') >>> result = copyFileSystemTree('src', 'dst') >>> '.blah' in os.listdir('dst') False This function supports some keyword parameters as explained below. Using ``overwrite`` ------------------- Boolean. If set to ``True``, any existing and same named files and directories in the destination dir are overwritten with copies from the source. Default is `False`. Normally, existing same named files in the destination are not overwritten: >>> open(os.path.join('src', 'blah'), 'wb').write('newnonsense') >>> result = copyFileSystemTree('src', 'dst') >>> open(os.path.join('dst', 'blah'), 'rb').read() 'nonsense' Instead the filename is added to the result (a list of non-copied files): >>> result ['blah'] If, however, we use `overwrite`, the existing file will be overwritten: >>> result = copyFileSystemTree('src', 'dst', overwrite=True) >>> open(os.path.join('dst', 'blah'), 'rb').read() 'newnonsense' >>> result [] This also works for complete directories: >>> os.mkdir(os.path.join('src', 'mydir')) >>> os.mkdir(os.path.join('dst', 'mydir')) >>> open(os.path.join( ... 'src', 'mydir', 'blah'), 'wb').write('srcblah') >>> open(os.path.join( ... 'dst', 'mydir', 'blah'), 'wb').write('dstblah') >>> result = copyFileSystemTree('src', 'dst', overwrite=True) >>> open(os.path.join('dst', 'mydir', 'blah'), 'rb').read() 'srcblah' Using ``del_old`` ----------------- Boolean. If set to ``True``, any copied files and directories will be removed from the src dir. Default is `False`. >>> result = copyFileSystemTree('src', 'dst', overwrite=True, ... del_old=True) >>> os.listdir('src') ['.blah'] All files and directories are removed from src, except the hidden file we created in the beginning. Clean up: >>> removeFileOrDirectory('src') >>> removeFileOrDirectory('dst') :func:`getInnerHTMLPart()` ========================== .. function:: getInnerHTMLPart(html_code) Get the 'inner' part out of a piece of HTML code. Helper function mainly to extract 'real content' from already rendered forms. The term 'inner part' here means the ``
`` part of an HTML snippet. If this cannot be found, we look for a ```` part and if this cannot be found as well, we simply return the whole input. If a ```` part can be found in an HTML snippet, this is returned with all preceeding/following stuff stripped: >>> from waeup.sirp.utils.helpers import getInnerHTMLPart >>> print getInnerHTMLPart(""" ... ... ... ... ... ...

Add a department

... ... ... ... """)

Add a department

If there is no ``
`` part, try to find any ```` part: >>> print getInnerHTMLPart(""" ... ... ... ... ...
Some content
... ... ... """)
Some content
If there is also no ```` tag, we return the input as-is: >>> print getInnerHTMLPart("""
...
Some content
...
... """)
Some content