Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 import re
00002 import glob
00003 from unittest import TestSuite
00004 from sys import modules
00005 from os.path import join, abspath, dirname, basename
00006 
00007 def normalize_html(s):
00008     s = re.sub(r"\s+", " ", s)
00009     s = re.sub(r"(?s)\s+<", "<", s)
00010     s = re.sub(r"(?s)>\s+", ">", s)
00011     s = re.sub(r"\r", "", s)
00012     return s
00013 
00014 def build_test_suite(package_name,module_names,required=1):
00015     """
00016     Utlitity for building a test suite from a package name
00017     and a list of modules.
00018 
00019     If required is false, then ImportErrors will simply result
00020     in that module's tests not being added to the returned
00021     suite.
00022     """
00023 
00024     suite = TestSuite()
00025     try:
00026         for name in module_names:
00027             the_name = package_name+'.'+name
00028             __import__(the_name,globals(),locals())
00029             suite.addTest(modules[the_name].test_suite())
00030     except ImportError:
00031         if required:
00032             raise
00033     return suite
00034 
00035 PREFIX = abspath(dirname(__file__))
00036 
00037 def input_file_path(file):
00038     return join(PREFIX, 'input', file)
00039 
00040 def output_file_path(file):
00041     return join(PREFIX, 'output', file)
00042 
00043 def matching_inputs(pattern):
00044     return [basename(path) for path in glob.glob(join(PREFIX, "input", pattern))]
00045 
00046 def load(dotted_name, globals=None):
00047     """ load a python module from it's name """
00048     mod = __import__(dotted_name, globals)
00049     components = dotted_name.split('.')
00050     for comp in components[1:]:
00051         mod = getattr(mod, comp)
00052     return mod