reco.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import dm
  3. import datetime
  4. import logging
  5. import logging.config
  6. import xdg.BaseDirectory
  7. APP_NAME = 'recofoo'
  8. LOG_INI = 'log.ini'
  9. DATA_PATH = xdg.BaseDirectory.save_data_path(APP_NAME)
  10. CONFIG_PATH = xdg.BaseDirectory.save_config_path(APP_NAME)
  11. TIMESTAMP_PATH = os.path.join(DATA_PATH, 'timestamp')
  12. def get_last_timestamp():
  13. if not os.path.exists(TIMESTAMP_PATH):
  14. return datetime.datetime.min
  15. with open(TIMESTAMP_PATH) as f:
  16. time = f.read()
  17. return datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%S')
  18. def update_timestamp(timestamp):
  19. with open(TIMESTAMP_PATH, 'w') as f:
  20. f.write(timestamp.strftime('%Y-%m-%dT%H:%M:%S'))
  21. def configure_logging():
  22. config_path = os.path.join(CONFIG_PATH, LOG_INI)
  23. log_path = LOG_INI if os.path.exists(LOG_INI) else None
  24. if not log_path:
  25. log_path = config_path if os.path.exists(config_path) else None
  26. if log_path:
  27. logging.config.fileConfig(log_path)
  28. else:
  29. logging.basicConfig(level=logging.INFO)
  30. class Application(object):
  31. def __init__(self):
  32. self.client = dm.Client()
  33. self.log = logging.getLogger(APP_NAME)
  34. def run(self):
  35. last_update = get_last_timestamp()
  36. for oid in self.client.get_object_ids(since=last_update):
  37. self.reconstruct(oid)
  38. now = datetime.datetime.now()
  39. update_timestamp(now)
  40. def reconstruct(self, oid):
  41. obj = self.client.get_object(oid)
  42. self.log.info("Reconstructing {}".format(obj.identifier))
  43. if __name__ == '__main__':
  44. configure_logging()
  45. Application().run()