tests/test_docstrings.py
branchpy33
changeset 3794 398ea04239b5
parent 3721 0c93dd8ff567
child 3804 79c6b1df0eca
equal deleted inserted replaced
3793:cc3f9cc828f7 3794:398ea04239b5
     9 Currently, methods with leading and trailing double underscores
     9 Currently, methods with leading and trailing double underscores
    10 are skipped.
    10 are skipped.
    11 """
    11 """
    12 from reportlab.lib.testutils import setOutDir,SecureTestCase, GlobDirectoryWalker, outputfile, printLocation
    12 from reportlab.lib.testutils import setOutDir,SecureTestCase, GlobDirectoryWalker, outputfile, printLocation
    13 setOutDir(__name__)
    13 setOutDir(__name__)
    14 import os, sys, glob, string, re, unittest
    14 import os, sys, glob, re, unittest
    15 from types import ModuleType, ClassType, MethodType, FunctionType
    15 from types import ModuleType, ClassType, MethodType, FunctionType
    16 import reportlab
    16 import reportlab
    17 
    17 
    18 def getModuleObjects(folder, rootName, typ, pattern='*.py'):
    18 def getModuleObjects(folder, rootName, typ, pattern='*.py'):
    19     "Get a list of all objects defined *somewhere* in a package."
    19     "Get a list of all objects defined *somewhere* in a package."
    20 
       
    21     # Define some abbreviations.
       
    22     find = string.find
       
    23     split = string.split
       
    24     replace = string.replace
       
    25 
       
    26     objects = []
    20     objects = []
    27     lookup = {}
    21     lookup = {}
    28     for file in GlobDirectoryWalker(folder, pattern):
    22     for file in GlobDirectoryWalker(folder, pattern):
    29         folder = os.path.dirname(file)
    23         folder = os.path.dirname(file)
    30 
    24 
    38         sys.path.insert(0, folder)
    32         sys.path.insert(0, folder)
    39         cwd = os.getcwd()
    33         cwd = os.getcwd()
    40         os.chdir(folder)
    34         os.chdir(folder)
    41 
    35 
    42         modName = os.path.splitext(os.path.basename(file))[0]
    36         modName = os.path.splitext(os.path.basename(file))[0]
    43         prefix = folder[find(folder, rootName):]
    37         prefix = folder[folder.find(rootName):]
    44         prefix = replace(prefix, os.sep, '.')
    38         prefix = prefix.replace(os.sep,'.')
    45         mName = prefix + '.' + modName
    39         mName = prefix + '.' + modName
    46 
    40 
    47         try:
    41         try:
    48             module = __import__(mName)
    42             module = __import__(mName)
    49         except ImportError:
    43         except ImportError:
    52             del sys.path[0]
    46             del sys.path[0]
    53             continue
    47             continue
    54 
    48 
    55         # Get the 'real' (leaf) module
    49         # Get the 'real' (leaf) module
    56         # (__import__ loads only the top-level one).
    50         # (__import__ loads only the top-level one).
    57         if find(mName, '.') != -1:
    51         if mName.find('.') != -1:
    58             for part in split(mName, '.')[1:]:
    52             for part in mName.split('.')[1:]:
    59                 module = getattr(module, part)
    53                 module = getattr(module, part)
    60 
    54 
    61             # Find the objects in the module's content.
    55             # Find the objects in the module's content.
    62             modContentNames = dir(module)
    56             modContentNames = dir(module)
    63 
    57 
    64             # Handle modules.
    58             # Handle modules.
    65             if typ == ModuleType:
    59             if typ == ModuleType:
    66                 if find(module.__name__, 'reportlab') > -1:
    60                 if module.__name__.find('reportlab') > -1:
    67                     objects.append((mName, module))
    61                     objects.append((mName, module))
    68                     continue
    62                     continue
    69 
    63 
    70             for n in modContentNames:
    64             for n in modContentNames:
    71                 obj = eval(mName + '.' + n)
    65                 obj = eval(mName + '.' + n)
    72                 # Handle functions and classes.
    66                 # Handle functions and classes.
    73                 if typ in (FunctionType, ClassType):
    67                 if typ in (FunctionType, ClassType):
    74                     if type(obj) == typ and obj not in lookup:
    68                     if type(obj) == typ and obj not in lookup:
    75                         if typ == ClassType:
    69                         if typ == ClassType:
    76                             if find(obj.__module__, rootName) != 0:
    70                             if obj.__module__.find(rootName) != 0:
    77                                 continue
    71                                 continue
    78                         objects.append((mName, obj))
    72                         objects.append((mName, obj))
    79                         lookup[obj] = 1
    73                         lookup[obj] = 1
    80                 # Handle methods.
    74                 # Handle methods.
    81                 elif typ == MethodType:
    75                 elif typ == MethodType:
    82                     if type(obj) == ClassType:
    76                     if type(obj) == ClassType:
    83                         for m in dir(obj):
    77                         for m in dir(obj):
    84                             a = getattr(obj, m)
    78                             a = getattr(obj, m)
    85                             if type(a) == typ and a not in lookup:
    79                             if type(a) == typ and a not in lookup:
    86                                 if find(a.__self__.__class__.__module__, rootName) != 0:
    80                                 if a.__self__.__class__.__module__.find(rootName) != 0:
    87                                     continue
    81                                     continue
    88                                 cName = obj.__name__
    82                                 cName = obj.__name__
    89                                 objects.append((mName, a))
    83                                 objects.append((mName, a))
    90                                 lookup[a] = 1
    84                                 lookup[a] = 1
    91 
    85