"""General helper functions for WAeUP. """ import os import re import sys import shutil import grok from cStringIO import StringIO from docutils.core import publish_string from zope.component.interfaces import IFactory from zope.interface import implementedBy def removeFileOrDirectory(filepath): """Remove a file or directory. Different to :func:`shutil.rmtree` we also accept not existing paths (returning silently) and if a dir turns out to be a regular file, we remove that. """ filepath = os.path.abspath(filepath) if not os.path.exists(filepath): return if os.path.isdir(filepath): shutil.rmtree(filepath) else: os.unlink(filepath) return def copyFileSystemTree(src, dst, overwrite=False, del_old=False): """Copy contents of directory src to directory dst. Both directories must exists. If `overwrite` is true, any same named objects will be overwritten. Otherwise these files will not be touched. If `del_old` is true, copied files and directories will be removed from the src directory. This functions returns a list of non-copied files. Unix hidden files and directories (starting with '.') are not processed by this function. """ if not os.path.exists(src): raise ValueError('source path does not exist: %s' % src) if not os.path.exists(dst): raise ValueError('destination path does not exist: %s' % dst) if not os.path.isdir(src): raise ValueError('source path is not a directory: %s' % src) if not os.path.isdir(dst): raise ValueError('destination path is not a directory: %s' % dst) not_copied = [] for item in os.listdir(src): if item.startswith('.'): continue # We do not copy hidden stuff... itemsrc = os.path.join(src, item) itemdst = os.path.join(dst, item) if os.path.exists(itemdst): if overwrite is True: removeFileOrDirectory(itemdst) else: not_copied.append(item) continue if os.path.isdir(itemsrc): shutil.copytree(itemsrc, itemdst) else: shutil.copy2(itemsrc, itemdst) if del_old: removeFileOrDirectory(itemsrc) return not_copied def getInnerHTMLPart(html_code): """Return the 'inner' part of a complete HTML snippet. If there is a form part, get this. If there is no form part, try to return the body part contents. If there is no body, return as-is. Let's see how that works. If we deliver some doc with form, we will get that form only: >>> doc = '
Outside the form' >>> getInnerHTMLPart(doc) '' No form? Then seek for a body part and get the contents: >>> doc = 'My BodyTrailing Trash' >>> getInnerHTMLPart(doc) 'My Body' If none of these is included, return what we got: >>> doc = 'without body nor form' >>> getInnerHTMLPart(doc) 'without body nor form' """ try: result = re.match('^.+().+$', html_code, re.DOTALL).groups()[0] return result except AttributeError: # No