models.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. class Data:
  3. def match_structure(self, entry):
  4. # if arbitrary XML resembles our data structures
  5. # (which it does if it exhibits the same keys)
  6. entry_keys = entry.keys()
  7. for key in self.keys:
  8. # specified keys not allowed to be missing
  9. if key not in entry_keys:
  10. return False
  11. # at this point, we process the actual content as well
  12. # so as to avoid restreaming for actual processing
  13. payload = entry[key]
  14. # this checks for any post processing methods to apply to payload
  15. if self.post[key] != 0:
  16. payload = self.post[key](payload)
  17. # collecting that data
  18. self.data[key] = payload
  19. return True
  20. def __str__(self):
  21. str_repr = ""
  22. for key in self.keys:
  23. str_repr += "{k}: {v}\n".format(k=key, v=self.data[key])
  24. return str_repr
  25. def post_process_status(payload):
  26. if not isinstance(payload, str):
  27. return payload
  28. return payload.lower()
  29. def post_process_comments(payload):
  30. if payload is None:
  31. return ''
  32. if not isinstance(payload, str):
  33. return payload
  34. return payload.replace('=== MAL Tags ===\n', '')
  35. class Summary(Data):
  36. def __init__(self):
  37. self.data = {}
  38. # done as a way to "generically" add post processing methods
  39. # like below for anime->my_comments
  40. self.post = { 'user_name': 0, 'user_export_type': 0, 'user_total_anime': 0, 'user_total_watching': 0, 'user_total_completed': 0, 'user_total_onhold': 0, 'user_total_dropped': 0, 'user_total_plantowatch': 0 }
  41. self.keys = self.post.keys()
  42. class Anime(Data):
  43. def __init__(self):
  44. self.data = {}
  45. self.post = { 'series_title': 0, 'series_episodes': 0, 'my_watched_episodes': 0, 'my_start_date': 0, 'my_finish_date': 0, 'my_score': 0, 'my_status': post_process_status, 'my_comments': post_process_comments }
  46. self.keys = self.post.keys()