summaryrefslogtreecommitdiff
path: root/tic/config.py
blob: 4648f953e1a0acad91717d4dc04b0e2d2877357c (plain)
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
#!/usr/bin/python
# Copyright (c) 2016 Samsung Electronics Co., Ltd
#
# Licensed under the Flora License, Version 1.1 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://floralicense.org/license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Contributors:
# - S-Core Co., Ltd

import os
import ConfigParser

DEFAULT_MSG_CONF = "/etc/tic-core/message.conf"
DEFAULT_CONF = "/etc/tic-core/config.conf"

class ConfigMgr(object):
    DEFAULT_MESSAGE = {"message": {
                        "repo_not_found": "The repository URL cannot be found (%s)",
                        "recipe_not_found": "The recipe URL cannot be found (%s)",
                        "xml_parse_error": "There was a problem parsing the %s, please check the file (%s)",
                        "yaml_parse_error": "There was a problem parsing the %s, please check the file (%s)",
                        "recipe_parse_error": "There was a problem parsing the recipe, please check the recipe file (%s)",
                        "recipe_convert_error": "There was a problem converting this recipe, please check the recipes",
                        "package_not_exist": "The default package(%s) does not exist.",
                        "dependency_not_exist": "The %s needed by %s does not exist. should be checked for repository",
                        "server_error": "there was a problem servicing your request. please try again later",
                        "recipe_repo_not_exist": "%s repository does not exist in in the recipe",
                        "default_recipe_use": "Use default recipe because there is no import data",
                        "no_package_to_install": "No packages to install. Please select a package to install",
                        "recipes_not_define": "Please define recipe for %s file creation"}
                       }
    
    DEFAULT_TIC = {"setting": {
                       "tempdir": "/var/tmp/tic-core",
                       "cachedir": "/var/tmp/tic-core/cache",
                       "logdir": "/var/tmp/tic-core/log",
                       "default_recipe": "/etc/tic-core/recipe.yaml"},
                   "server": {
                       "port": 8082},
                   "regularexp": {
                       "meta_prefix": "building-blocks",
                       "meta_prefix_root": "building-blocks-root-",
                       "meta_prefix_sub1": "building-blocks-sub1-",
                       "meta_pattern": "-(?P<meta>root|sub1|sub2|category)-(?P<pkgname>.+)",
                       "meta_sub1_pattern": "(?P<root>.+)-(?P<sub1>.+)",
                       "meta_sub2_pattern": "(?P<root>.+)-(?P<sub1>.+)-(?P<sub2>.+)",
                       "profile_pattern": "(?P<pkgname>.+)-profile_(?P<profile>[^-]+)-?(?P<extra>.+)?"}
                   }
    
    _instance = None
    
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(ConfigMgr, cls).__new__(cls, *args, **kwargs)
        return cls._instance
    
    def __init__(self):
        self._reset()
        for conf_path in [DEFAULT_CONF, DEFAULT_MSG_CONF]:
            self._setConfig(conf_path)

    def _reset(self):
        for sec, vals in self.DEFAULT_TIC.iteritems():
            setattr(self, sec, vals)
        for sec, vals in self.DEFAULT_MESSAGE.iteritems():
            setattr(self, sec, vals)
    
    def _setConfig(self, conf):
        configParser = ConfigParser.ConfigParser()
        try:
            if os.path.exists(conf):
                configParser.read(conf)
                for section in configParser.sections():
                    for option in configParser.options(section):
                        try:
                            opt_attr=getattr(self, section)
                            opt_attr[option]=configParser.get(section, option)
                        except:
                            pass
        except Exception as e:
            print(e)

configmgr = ConfigMgr()

if __name__ == '__main__':
    temp_dict = {'aaa': 'bbb'}
    temp= temp_dict.get(temp_dict.get(None))
    print(configmgr.setting['cachedir'])
    print(configmgr.message['repo_not_found'])
    print(configmgr.regularexp['meta_prefix'])
    print(configmgr.server['port'])