File: config.pyx

package info (click to toggle)
obitools 3.0.1~b26%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,756 kB
  • sloc: ansic: 24,299; python: 657; sh: 27; makefile: 21
file content (114 lines) | stat: -rwxr-xr-x 3,127 bytes parent folder | download
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#cython: language_level=3

'''
Created on 27 mars 2016

@author: coissac
'''

import sys

from .command   import  getCommandsList
from .logging   cimport getLogger
from .arguments cimport buildArgumentParser

from ..version import version


cdef dict __default_config__ = {}


cpdef str setRootConfigName(str rootname):
    global __default_config__
    if '__root_config__' in __default_config__:
        if __default_config__["__root_config__"] in __default_config__:
            __default_config__[rootname]=__default_config__[__default_config__["__root_config__"]]
            del __default_config__[__default_config__["__root_config__"]]
    __default_config__['__root_config__']=rootname
    return rootname

cpdef str getRootConfigName():
    global __default_config__
    return __default_config__.get('__root_config__',None)

cdef dict buildDefaultConfiguration(str root_config_name,
                                    dict  config):
    global __default_config__
    
    __default_config__.clear()
    setRootConfigName(root_config_name)    

    __default_config__[root_config_name]=config
    
    config['version']=version
    
    commands = getCommandsList()
    
    for c in commands:
        module = commands[c]
    
        assert hasattr(module, "run")
        
        if hasattr(module, 'default_config'):
            __default_config__[c]=module.default_config
        else:
            __default_config__[c]={}
                        
    return __default_config__
        

cpdef dict getConfiguration(str root_config_name="__default__",
                            dict  config={}):
    global __default_config__

    if '__done__' in __default_config__:
        return __default_config__
    
    if root_config_name=="__default__":
        raise RuntimeError("No root_config_name specified")
    
    if not config:
        raise RuntimeError("Base configuration is empty")
    
    
    
    config =  buildDefaultConfiguration(root_config_name,
                                        config)
    
    parser = buildArgumentParser(root_config_name,
                                 config[root_config_name]['software'])
    
    options = vars(parser.parse_args())

    if options['%s:version' % root_config_name]:
        print("%s - Version %s" % (config[root_config_name]['software'],
                                   config[root_config_name]['version']))
        sys.exit(0)
    
    for k in options:
        section,key = k.split(':')
        s = config[section]
        if options[k] is not None:
            s[key]=options[k]
            
    if not 'module' in config[root_config_name]:
        print('\nError: No command specified',file=sys.stderr)
        parser.print_help()
        sys.exit(2)
                
    getLogger(config)
    
    config['__done__']=True
            
    return config

def logger(level, *messages):
    try:
        config=getConfiguration()
        root = config["__root_config__"]
        l = config[root]['logger']
        if config[root]['verbose']:
            getattr(l, level)(*messages)
    except:
        print(*messages,file=sys.stderr)