~ricardokirkner/click-reviewers-tools/frameworks-from-api

« back to all changes in this revision

Viewing changes to clickreviews/modules.py

mergedĀ lp:~dholbach/click-reviewers-tools/modules-module-plus-tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import clickreviews
 
2
import imp
 
3
import inspect
 
4
import os
 
5
import pkgutil
 
6
 
 
7
IRRELEVANT_MODULES = ['cr_common', 'cr_tests', 'cr_skeleton']
 
8
 
 
9
 
 
10
def narrow_down_modules(modules):
 
11
    '''
 
12
    Get a list of file names or module names and filter out
 
13
    the ones we know are irrelevant.
 
14
    '''
 
15
    relevant_modules = []
 
16
    for module in modules:
 
17
        module_name = os.path.basename(module).replace('.py', '')
 
18
        if module_name not in IRRELEVANT_MODULES and \
 
19
                module_name.startswith('cr_'):
 
20
            relevant_modules += [module]
 
21
    return relevant_modules
 
22
 
 
23
 
 
24
def get_modules():
 
25
    '''
 
26
    Here we have a look at all the modules in the
 
27
    clickreviews package and filter out a few which
 
28
    are not relevant.
 
29
 
 
30
    Basically we look at all the ones which are
 
31
    derived from cr_common, where we can later on
 
32
    instantiate a *Review* object and run the
 
33
    necessary checks.
 
34
    '''
 
35
 
 
36
    all_modules = [name for _, name, _ in
 
37
                   pkgutil.iter_modules(clickreviews.__path__)]
 
38
    return narrow_down_modules(all_modules)
 
39
 
 
40
 
 
41
def find_main_class(module_name):
 
42
    '''
 
43
    This function will find the Click*Review class in
 
44
    the specified module.
 
45
    '''
 
46
    module = imp.load_source(module_name,
 
47
                             '%s/%s.py' % (clickreviews.__path__[0],
 
48
                                           module_name))
 
49
 
 
50
    classes = inspect.getmembers(module, inspect.isclass)
 
51
    find_main_class = lambda a: a[0].startswith('Click') and \
 
52
        not a[0].endswith('Exception') and \
 
53
        a[1].__module__ == module_name
 
54
    main_class = list(filter(find_main_class, classes))
 
55
    init_object = getattr(module, main_class[0][0])
 
56
    return init_object
 
57
 
 
58
 
 
59
def init_main_class(module_name, click_file):
 
60
    '''
 
61
    This function will instantiate the main Click*Review
 
62
    class of a given module and instantiate it with the
 
63
    location of the .click file we want to inspect.
 
64
    '''
 
65
 
 
66
    init_object = find_main_class(module_name)
 
67
    return init_object(click_file)