/src/infi/dtypes/hctl/__init__.py

https://github.com/Infinidat/infi.dtypes.hctl · Python · 64 lines · 58 code · 6 blank · 0 comment · 7 complexity · c554fa62a399dd51f6716d8c1a998ee8 MD5 · raw file

  1. __import__("pkg_resources").declare_namespace(__name__)
  2. import sys
  3. from collections import namedtuple
  4. PY2 = sys.version_info[0] == 2
  5. string_type = basestring if PY2 else str
  6. class NamedTupleAddress(object):
  7. _TUPLE = None
  8. def __init__(self, *args, **kwargs):
  9. super(NamedTupleAddress, self).__init__()
  10. self._value = self._TUPLE(*args, **kwargs) #pylint: disable-msg=E1102
  11. def __eq__(self, other):
  12. if isinstance(other, type(self)):
  13. return self._value == other._value
  14. if isinstance(other, string_type):
  15. return self == type(self).from_string(other)
  16. return False
  17. def __ne__(self, other):
  18. return not (self == other)
  19. def __lt__(self, other):
  20. if not isinstance(other, type(self)):
  21. raise TypeError()
  22. return self._value < other._value
  23. def __le__(self, other):
  24. return self == other or self < other
  25. def __gt__(self, other):
  26. return not (self <= other)
  27. def __ge__(self, other):
  28. return not self < other
  29. def __iter__(self):
  30. return iter(self._value)
  31. def __hash__(self):
  32. return hash(str(self))
  33. @classmethod
  34. def from_string(cls, s):
  35. if not isinstance(s, string_type):
  36. raise ValueError(s)
  37. return cls(*map(int, s.split(":")))
  38. def __repr__(self):
  39. return "<{0}>".format(self)
  40. def __str__(self):
  41. return ":".join(map(str, self._value))
  42. class HCT(NamedTupleAddress):
  43. _TUPLE = namedtuple("HCT", tuple("hct"))
  44. def get_host(self):
  45. return self._value.h
  46. def get_channel(self):
  47. return self._value.c
  48. def get_target(self):
  49. return self._value.t
  50. def __getitem__(self, l):
  51. return HCTL(self._value.h, self._value.c, self._value.t, l)
  52. class HCTL(HCT):
  53. _TUPLE = namedtuple("HCTL", tuple("hctl"))
  54. def get_lun(self):
  55. return self._value.l
  56. @classmethod
  57. def from_hct_and_lun(cls, hct, lun):
  58. return cls(hct.get_host(), hct.get_channel(), hct.get_target(), lun)