"""General helper functions for WAeUP.
"""
import os
import re
import sys
import shutil

def removeFileOrDirectory(filepath):
    """Remove a file or directory.
    """
    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.
    """

    try:
        result = re.match('^.+(<forms[^\>]*>.*</form>).+$', html_code,
                          re.DOTALL).groups()[0]
        return result
    except AttributeError:
        # No <form> part included
        try:
            result = re.match('^.+<body[^\>]*>(.*)</body>.*$', html_code,
                              re.DOTALL).groups()[0]
            return result
        except AttributeError:
            # No <form> and no <body> tag...
            pass
    return html_code

def getName(context):
    """Construct a name out of an object with prefix and title.
    """
    prefix = context.title_prefix
    prefix = prefix[0].upper() + prefix[1:]
    return '%s of %s' % (prefix, context.title)
