~roadmr/ubuntu/precise/checkbox/0.13.1

« back to all changes in this revision

Viewing changes to checkbox/properties.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2009-01-20 18:55:20 UTC
  • Revision ID: james.westby@ubuntu.com-20090120185520-s18m2hninrt53fki
Tags: 0.5
* New upstream version:
  * Added concept of hyper text view to display clickable links.
  * Added concept of properties to components.
  * Added pci information to launchpad report.
  * Added dmi information to launchpad report.
  * Added text area to keyboard test.
  * Removed sourcing of base postrm script.
  * Updated translations from Launchpad.
* Fixed handling of interrupt signal (LP: #327810)
* Fixed display of text in graphical interface (LP: #240374)
* Fixed support for regexes in blacklist and whitelist (LP: #327177)
* Fixed opening of subunit log file (LP: #325737)
* Fixed internet test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of Checkbox.
 
3
#
 
4
# Copyright 2008 Canonical Ltd.
 
5
#
 
6
# Checkbox is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation, either version 3 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# Checkbox is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
from checkbox.attribute import Attribute
 
20
from checkbox.variables import (BoolVariable, StringVariable,
 
21
    IntVariable, FloatVariable, ListVariable, VariableFactory, Variable,
 
22
    get_variable)
 
23
 
 
24
 
 
25
class Property(object):
 
26
 
 
27
    def __init__(self, variable_class=Variable, variable_kwargs={}):
 
28
        self._variable_class = variable_class
 
29
        self._variable_kwargs = variable_kwargs
 
30
 
 
31
    def __get__(self, obj, cls=None):
 
32
        if obj is None:
 
33
            return self._get_attribute(cls)
 
34
        if cls is None:
 
35
            cls = type(obj)
 
36
        attribute = self._get_attribute(cls)
 
37
        variable = get_variable(obj, attribute)
 
38
        return variable.get()
 
39
 
 
40
    def __set__(self, obj, value):
 
41
        cls = type(obj)
 
42
        attribute = self._get_attribute(cls)
 
43
        variable = get_variable(obj, attribute)
 
44
        variable.set(value)
 
45
 
 
46
    def _detect_name(self, used_cls):
 
47
        self_id = id(self)
 
48
        for cls in used_cls.__mro__:
 
49
            for attr, prop in cls.__dict__.items():
 
50
                if id(prop) == self_id:
 
51
                    return attr
 
52
        raise RuntimeError("Property used in an unknown class")
 
53
 
 
54
    def _get_attribute(self, cls):
 
55
        try:
 
56
            attribute = cls.__dict__["_checkbox_attributes"].get(self)
 
57
        except KeyError:
 
58
            cls._checkbox_attributes = {}
 
59
            attribute = None
 
60
 
 
61
        if attribute is None:
 
62
            name = self._detect_name(cls)
 
63
            attribute = PropertyAttribute(self, cls, name,
 
64
                self._variable_class, self._variable_kwargs)
 
65
            cls._checkbox_attributes[self] = attribute
 
66
 
 
67
        return attribute
 
68
 
 
69
 
 
70
class PropertyAttribute(Attribute):
 
71
 
 
72
    def __init__(self, prop, cls, name, variable_class, variable_kwargs):
 
73
        super(PropertyAttribute, self).__init__(name, cls,
 
74
            VariableFactory(variable_class, attribute=self, **variable_kwargs))
 
75
 
 
76
        self.cls = cls # Used by references
 
77
 
 
78
        # Copy attributes from the property to avoid one additional
 
79
        # function call on each access.
 
80
        for attr in ["__get__", "__set__"]:
 
81
            setattr(self, attr, getattr(prop, attr))
 
82
 
 
83
 
 
84
class PropertyType(Property):
 
85
 
 
86
    def __init__(self, **kwargs):
 
87
        kwargs["value"] = kwargs.pop("default", None)
 
88
        kwargs["value_factory"] = kwargs.pop("default_factory", None)
 
89
        super(PropertyType, self).__init__(self.variable_class, kwargs)
 
90
 
 
91
 
 
92
class Bool(PropertyType):
 
93
 
 
94
    variable_class = BoolVariable
 
95
 
 
96
 
 
97
class String(PropertyType):
 
98
 
 
99
    variable_class = StringVariable
 
100
 
 
101
 
 
102
class Int(PropertyType):
 
103
 
 
104
    variable_class = IntVariable
 
105
 
 
106
 
 
107
class Float(PropertyType):
 
108
 
 
109
    variable_class = FloatVariable
 
110
 
 
111
 
 
112
class List(PropertyType):
 
113
 
 
114
    variable_class = ListVariable
 
115
 
 
116
    def __init__(self, **kwargs):
 
117
        if "default" in kwargs:
 
118
            raise ValueError("'default' not allowed for List. "
 
119
                             "Use 'default_factory' instead.")
 
120
        type = kwargs.pop("type", None)
 
121
        if type is None:
 
122
            type = Property()
 
123
        kwargs["item_factory"] = VariableFactory(type._variable_class,
 
124
                                                 **type._variable_kwargs)
 
125
        super(List, self).__init__(**kwargs)