airtimeinstance.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import os
  2. from os.path import join, basename, dirname
  3. from ..monitor.exceptions import NoConfigFile
  4. from ..monitor.pure import LazyProperty
  5. from ..monitor.config import MMConfig
  6. from ..monitor.owners import Owner
  7. from ..monitor.events import EventRegistry
  8. from ..monitor.listeners import FileMediator
  9. from api_clients.api_client import AirtimeApiClient
  10. # poor man's phantom types...
  11. class SignalString(str): pass
  12. class AirtimeInstance(object):
  13. """ AirtimeInstance is a class that abstracts away every airtime
  14. instance by providing all the necessary objects required to interact
  15. with the instance. ApiClient, configs, root_directory """
  16. @classmethod
  17. def root_make(cls, name, root):
  18. cfg = {
  19. 'api_client' : join(root, 'etc/airtime/api_client.cfg'),
  20. 'media_monitor' : join(root, 'etc/airtime/airtime.conf'),
  21. }
  22. return cls(name, root, cfg)
  23. def __init__(self,name, root_path, config_paths):
  24. """ name is an internal name only """
  25. for cfg in ['api_client','media_monitor']:
  26. if cfg not in config_paths: raise NoConfigFile(config_paths)
  27. elif not os.path.exists(config_paths[cfg]):
  28. raise NoConfigFile(config_paths[cfg])
  29. self.name = name
  30. self.config_paths = config_paths
  31. self.root_path = root_path
  32. def signal(self, sig):
  33. if isinstance(sig, SignalString): return sig
  34. else: return SignalString("%s_%s" % (self.name, sig))
  35. def touch_file_path(self):
  36. """ Get the path of the touch file for every instance """
  37. touch_base_path = self.mm_config['media-monitor']['index_path']
  38. touch_base_name = basename(touch_base_path)
  39. new_base_name = self.name + touch_base_name
  40. return join(dirname(touch_base_path), new_base_name)
  41. def __str__(self):
  42. return "%s,%s(%s)" % (self.name, self.root_path, self.config_paths)
  43. @LazyProperty
  44. def api_client(self):
  45. return AirtimeApiClient(config_path=self.config_paths['api_client'])
  46. @LazyProperty
  47. def mm_config(self):
  48. return MMConfig(self.config_paths['media_monitor'])
  49. # I'm well aware that I'm using the service locator pattern
  50. # instead of normal constructor injection as I should be.
  51. # It's recommended to rewrite this using proper constructor injection
  52. @LazyProperty
  53. def owner(self): return Owner()
  54. @LazyProperty
  55. def event_registry(self): return EventRegistry()
  56. @LazyProperty
  57. def file_mediator(self): return FileMediator()