0c6eb0954f079f6f8234faf1dec22592bfc9fe53
[archive/20170607/tools/tic-core.git] / tic / config.py
1
2 import os
3 import ConfigParser
4
5 DEFAULT_MSG_CONF = "/etc/tic-core/message.conf"
6 DEFAULT_CONF = "/etc/tic-core/config.conf"
7
8 class ConfigMgr(object):
9     DEFAULT_MESSAGE = {'message': {
10                         'repo_not_found': "The repository url cannot be found (%s)",
11                         'xml_parse_error': "There was a problem parsing the %s, please check the file (%s)",
12                         'yaml_parse_error': "There was a problem parsing the %s, please check the file (%s)",
13                         'recipe_parse_error': "There was a problem parsing the recipe, please check the recipe file (%s)",
14                         'package_not_exist': "The default package(%s) does not exist.",
15                         'dependency_not_exist': "The %s needed by %s does not exist. should be checked for repository",
16                         'server_error': "there was a problem servicing your request. please try again later" }
17                        }
18     
19     DEFAULT_TIC = {'setting': {
20                        'tempdir': '/var/tmp/tic-core',
21                        'cachedir': '/var/tmp/tic-core/cache',
22                        'logdir': '/var/tmp/tic-core/log'},
23                    'server': {
24                        'port': 8082},
25                    'regularexp': {
26                        'meta_prefix': "building-blocks",
27                        'meta_prefix_root': "building-blocks-root-",
28                        'meta_prefix_sub1': "building-blocks-sub1-",
29                        'meta_pattern': "-(?P<meta>root|sub1|sub2)-(?P<pkgname>.+)",    
30                        'meta_sub1_pattern': "(?P<root>.+)-(?P<sub1>.+)",
31                        'meta_sub2_pattern': "(?P<root>.+)-(?P<sub1>.+)-(?P<sub2>.+)",
32                        'profile_pattern': "(?P<pkgname>.+)-profile_(?P<profile>[^-]+)-?(?P<extra>.+)?"}
33                    }
34     
35     _instance = None
36     
37     def __new__(cls, *args, **kwargs):
38         if not cls._instance:
39             cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
40         return cls._instance
41     
42     def __init__(self, ):
43         self._reset()
44         for conf_path in [DEFAULT_CONF, DEFAULT_MSG_CONF]:
45             self._setConfig(conf_path)
46
47     def _reset(self):
48         for sec, vals in self.DEFAULT_TIC.iteritems():
49             setattr(self, sec, vals)
50         for sec, vals in self.DEFAULT_MESSAGE.iteritems():
51             setattr(self, sec, vals)
52     
53     def _setConfig(self, conf):
54         configParser = ConfigParser.ConfigParser()
55         try:
56             if os.path.exists(conf):
57                 configParser.read(conf)
58                 for section in configParser.sections():
59                     for option in configParser.options(section):
60                         try:
61                             opt_attr=getattr(self, section)
62                             opt_attr[option]=configParser.get(section, option)
63                         except:
64                             pass
65         except Exception as e:
66             print(e)
67
68 configmgr = ConfigMgr()
69
70 if __name__ == '__main__':
71     temp_dict = {'aaa': 'bbb'}
72     temp= temp_dict.get(temp_dict.get(None))
73     print(configmgr.setting['cachedir'])
74     print(configmgr.message['repo_not_found'])
75     print(configmgr.regularexp['meta_prefix'])
76     print(configmgr.server['port'])