Skip to content

Commit

Permalink
Update RestObject to allow dictionary style access
Browse files Browse the repository at this point in the history
  • Loading branch information
dramich authored and Craig Jellick committed Sep 21, 2018
1 parent a1d5f26 commit e3762d5
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions rancher.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,13 @@ def wrapped(*args, **kw):
return wrapped


class RestObject:
class RestObject(object):
def __init__(self):
pass

@staticmethod
def _is_public(k, v):
return k not in ['links', 'actions', 'id', 'type'] and not callable(v)

def __str__(self):
return self.__repr__()

def _as_table(self):
if not hasattr(self, 'type'):
return str(self.__dict__)
data = [('Type', 'Id', 'Name', 'Value')]
for k, v in self.items():
if self._is_public(k, v):
if v is None:
v = 'null'
if v is True:
v = 'true'
if v is False:
v = 'false'
v = str(v)
if TRIM and len(v) > 70:
v = v[0:70] + '...'
data.append((self.type, self.id, str(k), v))

return indent(data, hasHeader=True, prefix='| ', postfix=' |',
wrapfunc=lambda x: str(x))

def _is_list(self):
return 'data' in self.__dict__ and isinstance(self.data, list)

def __repr__(self):
data = {}
for k, v in self.__dict__.items():
Expand All @@ -115,6 +88,9 @@ def __getattr__(self, k):
return getattr(self.data, k)
return getattr(self.__dict__, k)

def __getitem__(self, key):
return self.__dict__[key]

def __iter__(self):
if self._is_list():
return iter(self.data)
Expand All @@ -135,6 +111,33 @@ def __len__(self):
data[k] = v
return len(data)

@staticmethod
def _is_public(k, v):
return not callable(v)

def _as_table(self):
if not hasattr(self, 'type'):
return str(self.__dict__)
data = [('Type', 'Id', 'Name', 'Value')]
for k, v in self.items():
if self._is_public(k, v):
if v is None:
v = 'null'
if v is True:
v = 'true'
if v is False:
v = 'false'
v = str(v)
if TRIM and len(v) > 70:
v = v[0:70] + '...'
data.append((self.type, self.id, str(k), v))

return indent(data, hasHeader=True, prefix='| ', postfix=' |',
wrapfunc=lambda x: str(x))

def _is_list(self):
return 'data' in self.__dict__ and isinstance(self.data, list)

def data_dict(self):
data = {}
for k, v in self.__dict__.items():
Expand Down Expand Up @@ -658,11 +661,11 @@ def indent(rows, hasHeader=False, headerChar='-', delim=' | ', justify='left',
# closure for breaking logical rows to physical, using wrapfunc
def rowWrapper(row):
newRows = [wrapfunc(item).split('\n') for item in row]
return [[substr or '' for substr in item] for item in list(*newRows)] # NOQA
return [[substr or '' for substr in item] for item in list(newRows)] # NOQA
# break each logical row into one or more physical ones
logicalRows = [rowWrapper(row) for row in rows]
# columns of physical rows
columns = list(*reduce(operator.add, logicalRows))
columns = list(reduce(operator.add, logicalRows))
# get the maximum of each column by the string length of its items
maxWidths = [max([len(str(item)) for item in column])
for column in columns]
Expand Down

0 comments on commit e3762d5

Please sign in to comment.