| 1 |
class CMDView(object): |
| 2 |
""" |
| 3 |
View is the base-class that makes extracting values from XML |
| 4 |
data appear as objects with attributes. |
| 5 |
""" |
| 6 |
|
| 7 |
KEY = "name" |
| 8 |
KEY_ITEMS = [] |
| 9 |
COLUMNS = {} |
| 10 |
FILTERS = None |
| 11 |
FIELDS = {} |
| 12 |
REGEX = {} |
| 13 |
EXISTS = {} |
| 14 |
GROUPS = None |
| 15 |
TITLE = None |
| 16 |
EVAL = {} |
| 17 |
|
| 18 |
# ------------------------------------------------------------------------- |
| 19 |
# PROPERTIES |
| 20 |
# ------------------------------------------------------------------------- |
| 21 |
|
| 22 |
@property |
| 23 |
def T(self): |
| 24 |
"""return the Table instance for the View""" |
| 25 |
return self._table |
| 26 |
|
| 27 |
@property |
| 28 |
def D(self): |
| 29 |
"""return the Device instance for this View""" |
| 30 |
return self.T.D |
| 31 |
|
| 32 |
@property |
| 33 |
def xml(self): |
| 34 |
"""returns the XML associated to the item""" |
| 35 |
return self._xml |
| 36 |
|
| 37 |
# ------------------------------------------------------------------------- |
| 38 |
# METHODS |
| 39 |
# ------------------------------------------------------------------------- |
| 40 |
|
| 41 |
def keys(self): |
| 42 |
"""list of view keys, i.e. field names""" |
| 43 |
return self.FIELDS.keys() |
| 44 |
|
| 45 |
def values(self): |
| 46 |
"""list of view values""" |
| 47 |
return [getattr(self, field) for field in self.keys()] |
| 48 |
|
| 49 |
def items(self): |
| 50 |
"""list of tuple(key,value)""" |
| 51 |
return zip(self.keys(), self.values()) |
| 52 |
|
| 53 |
def asview(self, view_cls): |
| 54 |
"""create a new View object for this item""" |
| 55 |
return view_cls(self._table, self._xml) |
| 56 |
|
| 57 |
# ------------------------------------------------------------------------- |
| 58 |
# OVERLOADS |
| 59 |
# ------------------------------------------------------------------------- |
| 60 |
|
| 61 |
def __repr__(self): |
| 62 |
"""returns the name of the View with the associate item name""" |
| 63 |
return "%s:%s" % (self.__class__.__name__, self.name) |
| 64 |
|
| 65 |
def __getitem__(self, name): |
| 66 |
""" |
| 67 |
allow the caller to extract field values using :obj['name']: |
| 68 |
the same way they would do :obj.name: |
| 69 |
""" |
| 70 |
return getattr(self, name) |