ppvm.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. class FileEntry:
  2. def __init__(self, type, label, filename, meta):
  3. self.type = type
  4. self.filename = filename
  5. self.label = label
  6. self.metadata = meta
  7. @staticmethod
  8. def from_string(string):
  9. parts = string.split(":", 1)
  10. type = parts[0]
  11. parts = parts[1].split(",", 2)
  12. label = parts[0].strip()
  13. filename = parts[1].strip()
  14. meta = {}
  15. for property in parts[2].split("\";"):
  16. if(property == ""):
  17. continue
  18. data = property.split("=\"")
  19. meta[data[0].strip()] = data[1].strip()
  20. return FileEntry(type, label, filename, meta)
  21. def __str__(self) -> str:
  22. meta = ""
  23. for key in self.metadata:
  24. meta += " {}=\"{}\";".format(key, self.metadata[key])
  25. return "{}: {}, {},{}".format(self.type, self.label, self.filename, meta)
  26. class Ppvm:
  27. def __init__(self, files, metadata):
  28. self.files = files
  29. self.metadata = metadata
  30. @staticmethod
  31. def from_string(string: str):
  32. lines = string.split("\n")
  33. if lines[0] != "PPVM":
  34. raise Exception("Not a PPVM string")
  35. line_count = 1
  36. metadata = {}
  37. while True:
  38. line = lines[line_count]
  39. line_count += 1
  40. if(line == ""):
  41. break
  42. parts = line.split(":", 1)
  43. metadata[parts[0]] = parts[1].strip()
  44. entries = []
  45. while len(lines) > line_count:
  46. line = lines[line_count]
  47. line_count += 1
  48. if(line == ""):
  49. continue
  50. entries.append(FileEntry.from_string(line))
  51. return Ppvm(entries, metadata)
  52. def __str__(self) -> str:
  53. string = "PPVM\n"
  54. for key in self.metadata:
  55. string += "{}: {}\n".format(key, self.metadata[key])
  56. string += "\n"
  57. for file in self.files:
  58. string += "{}\n".format(file)
  59. string += "\n"
  60. return string