e2efc8a1494db59643a701a238f8dbaa6874bcb1
[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                         "recipe_not_found": "The recipe URL cannot be found (%s)",
12                         "xml_parse_error": "There was a problem parsing the %s, please check the file (%s)",
13                         "yaml_parse_error": "There was a problem parsing the %s, please check the file (%s)",
14                         "recipe_parse_error": "There was a problem parsing the recipe, please check the recipe file (%s)",
15                         "recipe_convert_error": "There was a problem converting this recipe, please check the recipes",
16                         "package_not_exist": "The default package(%s) does not exist.",
17                         "dependency_not_exist": "The %s needed by %s does not exist. should be checked for repository",
18                         "server_error": "there was a problem servicing your request. please try again later",
19                         "recipe_repo_not_exist": "%s repository does not exist in in the recipe",
20                         "default_recipe_use": "Use default recipe because there is no import data",
21                         "no_package_to_install": "No packages to install. Please select a package to install",
22                         "recipes_not_define": "Please define recipe for %s file creation"}
23                        }
24     
25     DEFAULT_TIC = {"setting": {
26                        "tempdir": "/var/tmp/tic-core",
27                        "cachedir": "/var/tmp/tic-core/cache",
28                        "logdir": "/var/tmp/tic-core/log",
29                        "default_recipe": "/etc/tic-core/recipe.yaml"},
30                    "server": {
31                        "port": 8082},
32                    "regularexp": {
33                        "meta_prefix": "building-blocks",
34                        "meta_prefix_root": "building-blocks-root-",
35                        "meta_prefix_sub1": "building-blocks-sub1-",
36                        "meta_pattern": "-(?P<meta>root|sub1|sub2|category)-(?P<pkgname>.+)",
37                        "meta_sub1_pattern": "(?P<root>.+)-(?P<sub1>.+)",
38                        "meta_sub2_pattern": "(?P<root>.+)-(?P<sub1>.+)-(?P<sub2>.+)",
39                        "profile_pattern": "(?P<pkgname>.+)-profile_(?P<profile>[^-]+)-?(?P<extra>.+)?"}
40                    }
41     
42     _instance = None
43     
44     def __new__(cls, *args, **kwargs):
45         if not cls._instance:
46             cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
47         return cls._instance
48     
49     def __init__(self):
50         self._reset()
51         for conf_path in [DEFAULT_CONF, DEFAULT_MSG_CONF]:
52             self._setConfig(conf_path)
53
54     def _reset(self):
55         for sec, vals in self.DEFAULT_TIC.iteritems():
56             setattr(self, sec, vals)
57         for sec, vals in self.DEFAULT_MESSAGE.iteritems():
58             setattr(self, sec, vals)
59     
60     def _setConfig(self, conf):
61         configParser = ConfigParser.ConfigParser()
62         try:
63             if os.path.exists(conf):
64                 configParser.read(conf)
65                 for section in configParser.sections():
66                     for option in configParser.options(section):
67                         try:
68                             opt_attr=getattr(self, section)
69                             opt_attr[option]=configParser.get(section, option)
70                         except:
71                             pass
72         except Exception as e:
73             print(e)
74
75 configmgr = ConfigMgr()
76
77 if __name__ == '__main__':
78     temp_dict = {'aaa': 'bbb'}
79     temp= temp_dict.get(temp_dict.get(None))
80     print(configmgr.setting['cachedir'])
81     print(configmgr.message['repo_not_found'])
82     print(configmgr.regularexp['meta_prefix'])
83     print(configmgr.server['port'])