source: main/waeup.sirp/trunk/src/waeup/sirp/utils/helpers.py @ 5383

Last change on this file since 5383 was 4789, checked in by uli, 15 years ago

Merge changes from ulif-layout back into trunk (finally).

File size: 2.9 KB
Line 
1"""General helper functions for WAeUP.
2"""
3import os
4import re
5import sys
6import shutil
7
8def removeFileOrDirectory(filepath):
9    """Remove a file or directory.
10    """
11    filepath = os.path.abspath(filepath)
12    if not os.path.exists(filepath):
13        return
14    if os.path.isdir(filepath):
15        shutil.rmtree(filepath)
16    else:
17        os.unlink(filepath)
18    return
19
20def copyFileSystemTree(src, dst, overwrite=False, del_old=False):
21    """Copy contents of directory src to directory dst.
22
23    Both directories must exists.
24
25    If `overwrite` is true, any same named objects will be
26    overwritten. Otherwise these files will not be touched.
27
28    If `del_old` is true, copied files and directories will be removed
29    from the src directory.
30
31    This functions returns a list of non-copied files.
32
33    Unix hidden files and directories (starting with '.') are not
34    processed by this function.
35    """
36    if not os.path.exists(src):
37        raise ValueError('source path does not exist: %s' % src)
38    if not os.path.exists(dst):
39        raise ValueError('destination path does not exist: %s' % dst)
40    if not os.path.isdir(src):
41        raise ValueError('source path is not a directory: %s' % src)
42    if not os.path.isdir(dst):
43        raise ValueError('destination path is not a directory: %s' % dst)
44    not_copied = []
45    for item in os.listdir(src):
46        if item.startswith('.'):
47            continue # We do not copy hidden stuff...
48        itemsrc = os.path.join(src, item)
49        itemdst = os.path.join(dst, item)
50
51        if os.path.exists(itemdst):
52            if overwrite is True:
53                removeFileOrDirectory(itemdst)
54            else:
55                not_copied.append(item)
56                continue
57       
58        if os.path.isdir(itemsrc):
59            shutil.copytree(itemsrc, itemdst)
60        else:
61            shutil.copy2(itemsrc, itemdst)
62        if del_old:
63            removeFileOrDirectory(itemsrc)
64    return not_copied
65
66
67def getInnerHTMLPart(html_code):
68    """Return the 'inner' part of a complete HTML snippet.
69
70    If there is a form part, get this.
71
72    If there is no form part, try to return the body part contents.
73
74    If there is no body, return as-is.
75    """
76
77    try:
78        result = re.match('^.+(<forms[^\>]*>.*</form>).+$', html_code,
79                          re.DOTALL).groups()[0]
80        return result
81    except AttributeError:
82        # No <form> part included
83        try:
84            result = re.match('^.+<body[^\>]*>(.*)</body>.*$', html_code,
85                              re.DOTALL).groups()[0]
86            return result
87        except AttributeError:
88            # No <form> and no <body> tag...
89            pass
90    return html_code
91
92def getName(context):
93    """Construct a name out of an object with prefix and title.
94    """
95    prefix = context.title_prefix
96    prefix = prefix[0].upper() + prefix[1:]
97    return '%s of %s' % (prefix, context.title)
Note: See TracBrowser for help on using the repository browser.