Passed
Push — master ( ab513a...1713a6 )
by Peter
05:21 queued 03:17
created

pyxb_builder.build_xmlschema_wrappers()   F

Complexity

Conditions 11

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 21
nop 3
dl 0
loc 41
rs 3.1764
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like pyxb_builder.build_xmlschema_wrappers() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from SCons.Script import * 
2
3
def build_xmlschema_wrappers(target, source, env):
4
    '''
5
        SCons needs real file names, so the main script must use proper file
6
        name suffixes for the Python file.
7
8
        PyXBGen, however, wants no .py suffix, otherwise modules are generated.
9
        Therefore, we strip the suffix before calling PyXBGen.
10
11
        Wee also need to use a proper "--binding-root" parameter, otherwise
12
        the path names become part of the generated Python module names
13
    '''
14
15
    sources = [str(src) for src in source]
16
17
    # Targets are the Python modules to be generated
18
    # If they all share the same directory prefix, then strip it and
19
    # use it as binding root. This gives relative imports in the generated
20
    # Python code, which is nicer
21
    prefix = ""
22
    all_same_prefix = True
23
    for t in target:
24
        new_prefix = str(t).rsplit('/',1)[0]
25
        if new_prefix != prefix:
26
            if prefix == "":
27
                prefix = new_prefix
28
            else:
29
                all_same_prefix = False
30
        prefix = new_prefix
31
32
    if all_same_prefix:
33
        targets = [str(trg).rsplit('/',1)[1].rsplit('.')[0] for trg in target]
34
        modulespec = ['-u %s -m %s'%(src, trg) for src, trg in zip(sources,targets)]
35
        cmdline = 'pyxbgen --binding-root='+prefix+' '+' '.join(modulespec)
36
    else:
37
        targets = [str(trg).rsplit('.')[0] for trg in target]
38
        modulespec = ['-u %s -m %s'%(src, trg) for src, trg in zip(sources,targets)]
39
        cmdline = 'pyxbgen '+' '.join(modulespec)
40
41
    print cmdline
42
    if os.system(cmdline) != 0:
43
        raise Exception('Execution of pyxbgen failed.')
44
45
# Define Builder for Django config file from settings INI file
46
pyxbbuilder = Builder(action = build_xmlschema_wrappers)
47
48
49