~roadmr/ubuntu/precise/checkbox/0.13.1

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#
# This file is part of Checkbox.
#
# Copyright 2008 Canonical Ltd.
#
# Checkbox 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 3 of the License, or
# (at your option) any later version.
#
# Checkbox 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 Checkbox.  If not, see <http://www.gnu.org/licenses/>.
#
import re

from checkbox.lib.cache import cache

from checkbox.component import ComponentManager
from checkbox.properties import String


class Registry(object):
    """
    Registry base class which should be inherited by each registry
    implementation. This class basically provides methods to represent
    the items in the registry as attributes. If some items cannot
    be represented as attributes, if there are spaces in the name
    for example, this class also provides methods to reprent them as
    dictionary elements.
    """

    _id = 0

    user = String(required=False)

    def __init__(self):
        super(Registry, self).__init__()
        self.id = Registry._id
        Registry._id += 1

    def __str__(self):
        raise NotImplementedError, "this function must be overridden by subclasses"

    def __getattr__(self, name):
        return self.get(name)

    def split(self, *args, **kwargs):
        return str(self).split(*args, **kwargs)

    def items(self):
        raise NotImplementedError, "this function must be overridden by subclasses"

    def get(self, key, default=None):
        try:
            return self.__getitem__(key)
        except KeyError:
            if default == None:
                from checkbox.registries.none import NoneRegistry
                return NoneRegistry()
            else:
                return default

    def has_key(self, key):
        return key in self.keys()

    def iteritems(self):
        for k, v in self.items():
            yield k, v

    def iterkeys(self):
        from checkbox.registries.link import LinkRegistry

        for k, v in self.items():
            # Prevent returning links in a dict() context
            if not isinstance(v, LinkRegistry):
                yield k

    def keys(self):
        return list(self.iterkeys())

    def itervalues(self):
        from checkbox.registries.link import LinkRegistry

        for k, v in self.items():
            # Prevent returning links in a values() context
            if not isinstance(v, LinkRegistry):
                yield v

    def values(self):
        return list(self.itervalues())

    def clear(self):
        raise Exception, "Cannot call clear on registry."

    def setdefault(self, key, default=None):
        raise Exception, "Cannot call setdefault on registry."

    def __cmp__(self, foreign):
        local = set(self.items())
        foreign = set(foreign.items())
        if local == foreign:
            return 0
        elif local < foreign:
            return -1
        return 1

    def __contains__(self, key):
        return key in self.keys()

    def __len__(self):
        return len(self.keys())

    def __getitem__(self, key):
        for k, v in self.items():
            if k == key:
                return v

        raise KeyError

    def __setitem__(self, key, value):
        raise Exception, "Cannot set setitem on registry."

    def __delitem__(self, key):
        raise Exception, "Cannot call delitem on registry."

    def update(self, foreign):
        raise Exception, "Cannot call update on registry."


class RegistryManager(ComponentManager, Registry):
    """
    Registry manager which is essentially the root of the registry
    tree. The first level in this tree consists of the module names
    which have been loaded from the registries configuration parameter.
    """

    @cache
    def items(self):
        items = []
        registries = self._config.get_defaults().registries
        section_names = re.split(r"\s+", registries)
        for section_name in section_names:
            section = self.load_section(section_name)
            for name in section.get_names():
                module = section.load_module(name)
                items.append((name, module))

        return items


def registry_flatten(registry):
    from checkbox.registries.link import LinkRegistry

    def get_properties(properties, key, value):
        if isinstance(value, Registry):
            # Prevent looping over links
            if not isinstance(value, LinkRegistry):
                for dict_key, dict_value in value.items():
                    get_properties(properties,
                        ".".join([key, dict_key]), dict_value)
        else:
            properties[key] = value

    properties = {}
    for key, value in registry.items():
        get_properties(properties, key, value)

    return properties

def registry_eval(registry, source):
    try:
        return eval(source, {}, registry)
    except Exception:
        return False

def registry_eval_recursive(registry, source, mask=[False]):
    values = []

    value = registry_eval(registry, source)
    if type(value) in (bool, int) and value:
        values.append(registry)
        mask[0] = True
    elif type(value) is tuple and True in value:
        for i in range(len(value)):
            if value[i] is True or i >= len(mask):
                mask[i:i+1] = [value[i]]

        values.append(registry)

    for key, value in registry.items():
        if isinstance(value, Registry):
            values.extend(registry_eval_recursive(value, source, mask))

    return values