Access python dicts using path strings (mainly just doing this to l2submodule)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

71 Zeilen
2.2KB

  1. class PathError(Exception):
  2. def __init__(self, path):
  3. self.path = path
  4. super().__init__(self.path)
  5. class PathNotFoundError(PathError):
  6. pass
  7. class MissingRequirementError(PathError):
  8. pass
  9. class NotDictError(PathError):
  10. pass
  11. class Config:
  12. def __init__(self, data, delimiter = "/", defaults = {}, requirements = [], **misc_options):
  13. self.data = data
  14. self.delimiter = delimiter
  15. self.strict_subscript = misc_options.get("strict_subscript", False)
  16. self.clobber_subscript = misc_options.get("clobber_subscript", False)
  17. for r in requirements:
  18. if not self.get(r): raise MissingRequirementError(r)
  19. for d in defaults:
  20. try:
  21. self.get(d, strict = True)
  22. except PathNotFoundError:
  23. self.set(d, defaults[d])
  24. def split_path(self, path):
  25. return path.split(self.delimiter)
  26. def get(self, path, fallback = None, strict = False):
  27. elementsAll = self.split_path(path)
  28. def g(elems = elementsAll, obj = self.data):
  29. if not elems or not elems[0] or type(obj) != dict: return obj
  30. head, tail = elems[0], elems[1:]
  31. if strict and head not in obj:
  32. raise PathNotFoundError(path)
  33. return g(tail, obj.get(head, fallback))
  34. return g()
  35. def set(self, path, value, clobber = False):
  36. elementsAll = self.split_path(path)
  37. def s(elems = elementsAll, obj = self.data):
  38. head, tail = elems[0], elems[1:]
  39. # YANDEV MODE ACTIVATE!!!
  40. if not tail:
  41. obj[head] = value
  42. else:
  43. if head not in obj or type(obj[head]) != dict:
  44. if clobber:
  45. obj[head] = {}
  46. else:
  47. raise NotDictError(self.delimiter.join(elementsAll[:-len(tail)]))
  48. s(tail, obj[head])
  49. s()
  50. def __getitem__(self, path):
  51. return self.get(path, self.strict_subscript)
  52. def __setitem__(self, path, value):
  53. self.set(path, value, self.clobber_subscript)