padmanager.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import json
  2. import config
  3. import os
  4. import sys
  5. class _PadManager:
  6. def __init__(self):
  7. self.pads={}
  8. self.list=[]
  9. for file in os.listdir(config.PAD_DIRECTORY):
  10. if file.lower().endswith(".json") or file.lower().endswith(".conf"):
  11. x=self.test_pad(file)
  12. if x:
  13. self.pads[x] = file
  14. self.list.append(x)
  15. def test_pad(self, file):
  16. name = None
  17. try:
  18. with open(os.path.join(config.PAD_DIRECTORY, file)) as f:
  19. content=f.read()
  20. x=json.loads(content)
  21. if x and "name" in x and x["name"]:
  22. return x["name"]
  23. except:
  24. pass
  25. return None
  26. def read_pad(self, padname):
  27. file=os.path.join(config.PAD_DIRECTORY, self.pads[padname])
  28. with open(file) as f:
  29. return json.loads(f.read())
  30. class PadManager:
  31. __pm = _PadManager()
  32. @staticmethod
  33. def read_pad(name):
  34. return PadManager.__pm.read_pad(name)
  35. @staticmethod
  36. def pad_list():
  37. return PadManager.__pm.list