~ubuntu-branches/ubuntu/trusty/kiwi/trusty

« back to all changes in this revision

Viewing changes to kiwi/python.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-01-30 09:52:28 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070130095228-n0mkqlt82e74twqc
Tags: 1.9.12-0ubuntu1
* New upstream release.
* debian/patches/03_epyrun_use_local_modules.patch:
  - updated.
* debian/control:
  - moved python-support from Build-Depends to Build-Depends-Indep.

Show diffs side-by-side

added added

removed removed

Lines of Context:
108
108
        for k, v in kw.iteritems():
109
109
            setattr(self, k, v)
110
110
 
 
111
    def getattributes(self):
 
112
        """
 
113
        Fetches the attributes used to create this object
 
114
        @returns: a dictionary with attributes
 
115
        """
 
116
        return self._attrs
 
117
 
111
118
    def __repr__(self):
112
119
        return '<%s %s>' % (self.__class__.__name__,
113
120
                            ', '.join(
168
175
    _no_deprecation = old
169
176
    return retval
170
177
 
 
178
class enum(int):
 
179
    """
 
180
    enum is an enumered type implementation in python.
 
181
 
 
182
    To use it, define an enum subclass like this:
 
183
 
 
184
    >>> from kiwi.python import enum
 
185
    >>>
 
186
    >>> class Status(enum):
 
187
    >>>     OPEN, CLOSE = range(2)
 
188
    >>> Status.OPEN
 
189
    '<Status value OPEN>'
 
190
 
 
191
    All the integers defined in the class are assumed to be enums and
 
192
    values cannot be duplicated
 
193
    """
 
194
 
 
195
    __metaclass__ = ClassInittableMetaType
 
196
 
 
197
    #@classmethod
 
198
    def __class_init__(cls, ns):
 
199
        cls.names = {} # name -> enum
 
200
        cls.values = {} # value -> enum
 
201
 
 
202
        for key, value in ns.items():
 
203
            if isinstance(value, int):
 
204
                cls(value, key)
 
205
    __class_init__ = classmethod(__class_init__)
 
206
 
 
207
    #@classmethod
 
208
    def get(cls, value):
 
209
        """
 
210
        Lookup an enum by value
 
211
        @param value: the value
 
212
        """
 
213
        if not value in cls.values:
 
214
            raise ValueError("There is no enum for value %d" % (value,))
 
215
        return cls.values[value]
 
216
    get = classmethod(get)
 
217
 
 
218
    def __new__(cls, value, name):
 
219
        """
 
220
        @param value: value of the enum
 
221
        @param name: name of the enum
 
222
        """
 
223
        if name in cls.names:
 
224
            raise ValueError("There is already an enum called %s" % (name,))
 
225
 
 
226
        if value in cls.values:
 
227
            raise ValueError(
 
228
                "Error while creating enum %s of type %s, "
 
229
                "it has already been created as %s" % (
 
230
                value, cls.__name__, cls.values[value]))
 
231
 
 
232
        self = super(enum, cls).__new__(cls, value)
 
233
        self.name = name
 
234
 
 
235
        cls.values[value] = self
 
236
        cls.names[name] = self
 
237
        setattr(cls, name, self)
 
238
 
 
239
        return self
 
240
 
 
241
    def __str__(self):
 
242
        return '<%s value %s>' % (
 
243
            self.__class__.__name__, self.name)
 
244
    __repr__ = __str__
 
245
 
 
246
def all(seq):
 
247
    """
 
248
    @returns: True if all items in seq are True
 
249
    """
 
250
    for item in seq:
 
251
        if not item:
 
252
            return False
 
253
    return True
 
254
 
 
255
def any(seq):
 
256
    """
 
257
    @returns: True if any item in seq is True
 
258
    """
 
259
    for item in seq:
 
260
        if item:
 
261
            return True
 
262
    return False