summaryrefslogtreecommitdiff
path: root/paste/docsupport/findmodules.py
blob: 15a48f344f76acdc7c5b545c41d8175a352e39aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
Finds all modules in a packages, loads them, and returns them.
"""

import os
import re
from paste.util.import_string import import_module

module_name = re.compile(r'^[a-zA-Z][a-zA-Z0-9_]*$')

def find_modules(package):
    pkg_name = package.__name__
    modules = []
    base = os.path.abspath(package.__file__)
    if os.path.basename(os.path.splitext(base)[0]) == '__init__':
        base = os.path.dirname(base)
    if os.path.isdir(base):
        for module_fn in os.listdir(base):
            base, ext = os.path.splitext(module_fn)
            full = os.path.join(base, module_fn)
            if not module_name.search(base):
                continue
            if (os.path.isdir(full)
                and os.path.exists(os.path.join(full, '__ini__.py'))):
                modules.extend(import_module(pkg_name + '.' + base))
            elif ext == '.py':
                modules.append(import_module(pkg_name + '.' + base))
    else:
        modules.append(package)
    return modules