| 1 |
class ViewFields(object): |
| 2 |
""" |
| 3 |
Used to dynamically create a field dictionary used with the |
| 4 |
RunstatView class |
| 5 |
""" |
| 6 |
|
| 7 |
def __init__(self): |
| 8 |
self._fields = dict() |
| 9 |
|
| 10 |
def _prockvargs(self, field, name, **kvargs): |
| 11 |
if not len(kvargs): |
| 12 |
return |
| 13 |
field[name].update(kvargs) |
| 14 |
|
| 15 |
@property |
| 16 |
def end(self): |
| 17 |
return self._fields |
| 18 |
|
| 19 |
def str(self, name, xpath=None, **kvargs): |
| 20 |
"""field is a string""" |
| 21 |
if xpath is None: |
| 22 |
xpath = name |
| 23 |
field = {name: {"xpath": xpath}} |
| 24 |
self._prockvargs(field, name, **kvargs) |
| 25 |
self._fields.update(field) |
| 26 |
return self |
| 27 |
|
| 28 |
def astype(self, name, xpath=None, astype=int, **kvargs): |
| 29 |
""" |
| 30 |
field string value will be passed to function :astype: |
| 31 |
|
| 32 |
This is typically used to do simple type conversions, |
| 33 |
but also works really well if you set :astype: to |
| 34 |
a function that does a basic converstion like look |
| 35 |
at the value and change it to a True/False. For |
| 36 |
example: |
| 37 |
|
| 38 |
astype=lambda x: True if x == 'enabled' else False |
| 39 |
""" |
| 40 |
if xpath is None: |
| 41 |
xpath = name |
| 42 |
field = {name: {"xpath": xpath, "astype": astype}} |
| 43 |
self._prockvargs(field, name, **kvargs) |
| 44 |
self._fields.update(field) |
| 45 |
return self |
| 46 |
|
| 47 |
def int(self, name, xpath=None, **kvargs): |
| 48 |
"""field is an integer""" |
| 49 |
return self.astype(name, xpath, int, **kvargs) |
| 50 |
|
| 51 |
def flag(self, name, xpath=None, **kvargs): |
| 52 |
""" |
| 53 |
field is a flag, results in True/False if the xpath element exists or |
| 54 |
not. Model this as a boolean type <bool> |
| 55 |
""" |
| 56 |
return self.astype(name, xpath, bool, **kvargs) |
| 57 |
|
| 58 |
def group(self, name, xpath=None, **kvargs): |
| 59 |
""" |
| 60 |
field is an apply group, results in value of group attr if the xpath |
| 61 |
element has the associated group attribute. |
| 62 |
""" |
| 63 |
xpath = "./{}/@group".format(xpath) |
| 64 |
return self.astype(name, xpath, str, **kvargs) |
| 65 |
|
| 66 |
def table(self, name, table): |
| 67 |
"""field is a RunstatTable""" |
| 68 |
self._fields.update({name: {"table": table}}) |
| 69 |
return self |