Merge branch 'develop'
[archive/20170607/tools/tic-core.git] / tic / config.py
1 #!/usr/bin/python
2 # Copyright (c) 2016 Samsung Electronics Co., Ltd
3 #
4 # Licensed under the Flora License, Version 1.1 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://floralicense.org/license/
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Contributors:
17 # - S-Core Co., Ltd
18
19 import os
20 import ConfigParser
21
22 DEFAULT_MSG_CONF = "/etc/tic-core/message.conf"
23 DEFAULT_CONF = "/etc/tic-core/config.conf"
24
25 class ConfigMgr(object):
26     DEFAULT_MESSAGE = {"message": {
27                         "repo_not_found": "The repository URL cannot be found (%s)",
28                         "recipe_not_found": "The recipe URL cannot be found (%s)",
29                         "xml_parse_error": "There was a problem parsing the %s, please check the file (%s)",
30                         "yaml_parse_error": "There was a problem parsing the %s, please check the file (%s)",
31                         "recipe_parse_error": "There was a problem parsing the recipe, please check the recipe file (%s)",
32                         "recipe_convert_error": "There was a problem converting this recipe, please check the recipes",
33                         "package_not_exist": "The default package(%s) does not exist.",
34                         "dependency_not_exist": "The %s needed by %s does not exist. should be checked for repository",
35                         "server_error": "there was a problem servicing your request. please try again later",
36                         "recipe_repo_not_exist": "%s repository does not exist in in the recipe",
37                         "default_recipe_use": "Use default recipe because there is no import data",
38                         "no_package_to_install": "No packages to install. Please select a package to install",
39                         "recipes_not_define": "Please define recipe for %s file creation"}
40                        }
41     
42     DEFAULT_TIC = {"setting": {
43                        "tempdir": "/var/tmp/tic-core",
44                        "cachedir": "/var/tmp/tic-core/cache",
45                        "logdir": "/var/tmp/tic-core/log",
46                        "default_recipe": "/etc/tic-core/recipe.yaml"},
47                    "server": {
48                        "port": 8082},
49                    "regularexp": {
50                        "meta_prefix": "building-blocks",
51                        "meta_prefix_root": "building-blocks-root-",
52                        "meta_prefix_sub1": "building-blocks-sub1-",
53                        "meta_pattern": "-(?P<meta>root|sub1|sub2|category)-(?P<pkgname>.+)",
54                        "meta_sub1_pattern": "(?P<root>.+)-(?P<sub1>.+)",
55                        "meta_sub2_pattern": "(?P<root>.+)-(?P<sub1>.+)-(?P<sub2>.+)",
56                        "profile_pattern": "(?P<pkgname>.+)-profile_(?P<profile>[^-]+)-?(?P<extra>.+)?"}
57                    }
58     
59     _instance = None
60     
61     def __new__(cls, *args, **kwargs):
62         if not cls._instance:
63             cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
64         return cls._instance
65     
66     def __init__(self):
67         self._reset()
68         for conf_path in [DEFAULT_CONF, DEFAULT_MSG_CONF]:
69             self._setConfig(conf_path)
70
71     def _reset(self):
72         for sec, vals in self.DEFAULT_TIC.iteritems():
73             setattr(self, sec, vals)
74         for sec, vals in self.DEFAULT_MESSAGE.iteritems():
75             setattr(self, sec, vals)
76     
77     def _setConfig(self, conf):
78         configParser = ConfigParser.ConfigParser()
79         try:
80             if os.path.exists(conf):
81                 configParser.read(conf)
82                 for section in configParser.sections():
83                     for option in configParser.options(section):
84                         try:
85                             opt_attr=getattr(self, section)
86                             opt_attr[option]=configParser.get(section, option)
87                         except:
88                             pass
89         except Exception as e:
90             print(e)
91
92 configmgr = ConfigMgr()
93
94 if __name__ == '__main__':
95     temp_dict = {'aaa': 'bbb'}
96     temp= temp_dict.get(temp_dict.get(None))
97     print(configmgr.setting['cachedir'])
98     print(configmgr.message['repo_not_found'])
99     print(configmgr.regularexp['meta_prefix'])
100     print(configmgr.server['port'])