Source code for tanium_kit.store

"""Dict-like object class."""
from __future__ import absolute_import, division, print_function, unicode_literals

import sys

_VERSION = sys.version_info
IS_PY2 = _VERSION[0] == 2
IS_PY3 = _VERSION[0] == 3

if IS_PY2:
    text_type = unicode  # noqa
elif IS_PY3:
    text_type = str


[docs]class Store(dict): """Dict-like object class.""" _HIDDEN = ["password"] _MAX_VAL_LEN = 40
[docs] def __init__(self, *args, **kwargs): """Dict-like object class.""" super(Store, self).__init__(*args, **kwargs) for name, value in kwargs.items(): try: super(Store, self).__setattr__(name, value) except: pass
[docs] def __str__(self): """str() method.""" ret = [] for k, v in sorted(self.items()): if text_type(k).startswith("_"): continue if isinstance(v, Store): vv = "Store len:{}".format(len(v.items())) else: vv = "**HIDDEN**" if k in self._HIDDEN else text_type(v) if len(vv) > self._MAX_VAL_LEN: vv = "{}..".format(vv[:self._MAX_VAL_LEN].replace("\n", "").strip()) m = "'{}': '{}'".format(k, vv) ret.append(m) ret = ", ".join(ret) ret = "{" + ret + "}" return ret
[docs] def __repr__(self): """repr() method.""" ret = [] for k, v in sorted(self.items()): if text_type(k).startswith("_"): continue if isinstance(v, Store): vv = "Store len:{}".format(len(v.items())) else: vv = "'**HIDDEN**'" if k in self._HIDDEN else repr(v) if len(vv) > self._MAX_VAL_LEN: vv = "{}..".format(vv[:self._MAX_VAL_LEN].replace("\n", "").strip()) if not vv.startswith("'"): vv = "'{}'".format(vv) m = str("'{}': {}".format(k, vv)) ret.append(m) ret = ", ".join(ret) ret = "{" + ret + "}" return ret
[docs] def __getattr__(self, name): """getattr() method.""" ret = None try: ret = super(Store, self).__getitem__(name) except: pass return ret
[docs] def __setattr__(self, name, value): """setattr() method.""" super(Store, self).__setitem__(name, value) try: super(Store, self).__setattr__(name, value) except: pass
[docs] def __delattr__(self, name): """delattr() method.""" if name in self.keys(): super(Store, self).__delitem__(name) super(Store, self).__delattr__(name)
[docs] def __getitem__(self, name): """GetItem.""" ret = None try: ret = super(Store, self).__getitem__(name) except: pass return ret
[docs] def __setitem__(self, name, value): """SetItem.""" super(Store, self).__setitem__(name, value) try: super(Store, self).__setattr__(name, value) except: pass
[docs] def __delitem__(self, name): """Die.""" if name in self.keys(): super(Store, self).__delitem__(name) super(Store, self).__delattr__(name)
[docs] def gets(self, name, default=None): """Get value of name, set to default if not exists.""" if name in self.keys(): ret = self.get(name) else: ret = default setattr(self, name, ret) return ret