summaryrefslogtreecommitdiffstats
path: root/cnucnu/config.py
blob: 88aa73fd340daf25164bd29ae585cb93afea9d87 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/python
# vim: fileencoding=utf8 foldmethod=marker
# {{{ License header: GPLv2+
#    This file is part of cnucnu.
#
#    Cnucnu is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    Cnucnu is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with cnucnu.  If not, see <http://www.gnu.org/licenses/>.
# }}}
""" :author: Till Maas
    :contact: opensource@till.name
    :license: GPLv2+
"""
__docformat__ = "restructuredtext"

from helper import pprint, filter_dict
import yaml

DEFAULT_YAML="""
bugzilla:
    user: upstream-release-monitoring@fedoraproject.org
    password: 
    base url: https://bugzilla.redhat.com
    url: "%(base url)s/xmlrpc.cgi"
    bug url prefix: "%(base url)s/show_bug.cgi?id="
    product: Fedora
    version: rawhide
    keywords: FutureFeature,Triaged
    bug status: NEW
    explanation url: 'https://fedoraproject.org/wiki/Upstream_release_monitoring'

    short_desc template: "%%(name)s-%%(latest_upstream)s is available"
    description template: 'Latest upstream release: %%(latest_upstream)s

                           Current version/release in %%(repo_name)s: %%(repo_version)s-%%(repo_release)s

                           URL: %%(url)s


                           Please consult the package updates policy before you
                           issue an update to a stable branch:
                           https://fedoraproject.org/wiki/Updates_Policy


                           More information about the service that created this bug can be found at:

    %(explanation url)s'


repo:
    path: 'http://kojipkgs.fedoraproject.org/mash/rawhide/source/SRPMS'
    name: Fedora Rawhide

scm:
    view_scm_url: https://pkgs.fedoraproject.org/cgit/%(name)s.git/plain/sources
    cainfo: "fedora-server-ca.cert"

package list:
    mediawiki:
        base url: 'https://fedoraproject.org/w/'
        page: Upstream_release_monitoring


# vim: filetype=yaml
"""


class Config(object):
    """ Config management class for cnucnu.
    """
    def __init__(self, yaml_file=None, yaml=None, yaml_data=None, config=None, load_default=True):
        # TODO: remove yaml option
        if yaml_data:
            yaml = yaml_data
        self.config = {}

        # TODO?
        self.__getitem__ = self.config.__getitem__

        if load_default:
            self.update_yaml(DEFAULT_YAML)
        if yaml_file:
            self.update_yaml_file(yaml_file)
        elif yaml:
            self.update_yaml(yaml)
        elif config:
            self.update(config)

        self._bugzilla_config = {}

    def update_yaml_file(self, new_yaml_file, old=None):
        if not old:
            old = self.config
        file = open(new_yaml_file, "rb")
        new_yaml = file.read()
        file.close()

        old = self.update_yaml(new_yaml, old)
        return old

    def update_yaml(self, new_yaml, old=None):
        if not old:
            old = self.config
        new = yaml.load(new_yaml)
        old = self.update(new, old)
        return old

    def update(self, new, old=None):
        """ Update dictionary with new values recursively.

        :Parameters:
            new : dict
                new dictionary
            old : dict
                old dictionary, defaults to self.config

        """
        if not old:
            old = self.config
        for k, v in new.items():
            if k in old.keys():
                if isinstance(old[k], dict):
                    old[k] = self.update(new[k], old[k])
                else:
                    old[k] = new[k]
            else:
                old[k] = new[k]
        self._bugzilla_config = {}
        return old

    @property
    def bugzilla_config(self):
        if not self._bugzilla_config:
            b = {}
            b.update(self.config["bugzilla"])
            for c, v in b.items():
                if isinstance(v, str):
                    b[c] = v % b

            self._bugzilla_config = b
        return self._bugzilla_config

    @property
    def bugzilla_class_conf(self):
        rpc_conf = filter_dict(self.bugzilla_config, ["url", "user", "password"])
        return rpc_conf

    @property
    def yaml(self):
        return yaml.dump(self.config, indent=4, default_flow_style=False)

global_config = Config()


if __name__ == '__main__':
    cf = Config()

    print "Default config"
    pprint(cf.config)
    print "yaml of default config"
    print cf.yaml

    cf.update_yaml_file('../cnucnu.yaml')

    print "Global config"
    pprint(cf.config)

    print "\nBugzilla config"
    pprint(cf.bugzilla_config)

    print "\nBugzilla class config"
    pprint(cf.bugzilla_class_conf)