Plex plugin to to play various online streams (mostly Latvian).

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import inspect
  2. import os
  3. from unittest import TestCase
  4. from abc import ABCMeta
  5. import bootstrap
  6. from mock import mock
  7. from plex import BasicRequest, FrameworkCore, PreferenceManager, Runtime
  8. class PlexTestCase(TestCase):
  9. __metaclass__ = ABCMeta
  10. module_name = None
  11. mock = mock
  12. def __init__(self, methodName='runTest'):
  13. TestCase.__init__(self, methodName)
  14. # Get bundle and channel info for framework initialization.
  15. self.bundle_directory = bootstrap.bundle_directory
  16. self.config = bootstrap.config
  17. self.framework_directory = bootstrap.framework_directory
  18. # Autodetect module name if not defined explicitly.
  19. if self.module_name is None:
  20. self.module_name = self.__module__
  21. if self.module_name[-5:] == '_test':
  22. self.module_name = self.module_name[:-5]
  23. def tearDown(self):
  24. # Call teardown methods automatically if not called from the child classes.
  25. if hasattr(self, '_core'):
  26. self.teardown_framework()
  27. def setup_framework(self):
  28. # Setup framework core object, either call this from `setUp` method or it'll be called when first accessing
  29. # the object's `core` property.
  30. self._initialize_framework(self.bundle_directory, self.framework_directory, self.config)
  31. def _initialize_framework(self, bundle_path, framework_dir, config):
  32. # Check if the framework has already been initialized.
  33. if hasattr(self, '_core'):
  34. raise RuntimeError('Plex Framework already initialized.')
  35. # Create Framework core object instance.
  36. self._core = FrameworkCore(bundle_path, framework_dir, config)
  37. # Force wait for shared code sandbox loaded event.
  38. self._core.services.shared_code_sandbox
  39. # Initialize framework code and start plugin (same as in framework's bootstrap method `run`).
  40. if not self._core.load_code():
  41. raise RuntimeError('Error loading bundle code.')
  42. self._core.start()
  43. default_prefs = {k: v.default_value for k, v in self._core.sandbox.preferences.get()._prefs.items()}
  44. self._core.sandbox.preferences = PreferenceManager(default_prefs)
  45. def teardown_framework(self):
  46. # Tear down framework core object, call this from `tearDown` method.
  47. if hasattr(self, '_core'):
  48. self._core.runtime.taskpool.shutdown()
  49. self._core.runtime.check_threads()
  50. del self._core
  51. @property
  52. def core(self):
  53. # Initialize framework automatically if not yet done.
  54. if not hasattr(self, '_core'):
  55. self.setup_framework()
  56. return self._core
  57. @property
  58. def environment(self):
  59. return self.core.sandbox.environment
  60. @property
  61. def module(self):
  62. return self.environment[self.module_name]
  63. @property
  64. def networking(self):
  65. return self.core.networking
  66. @property
  67. def preferences(self):
  68. return self.core.sandbox.preferences.get()
  69. @property
  70. def shared_code_environment(self):
  71. return self.core.services.shared_code_sandbox.environment
  72. def get_file_contents(self, filename):
  73. # Read file placed in a subdirectory named after the testcase class name.
  74. test_path = os.path.dirname(inspect.getfile(self.__class__))
  75. with open('%s/%s/%s' % (test_path, self.__class__.__name__, filename), 'r') as content_file:
  76. content = content_file.read()
  77. return content
  78. def request(self, path, headers={}, method='GET', body=''):
  79. # Make a request to the channel code.
  80. request = BasicRequest(path, headers, method, body)
  81. return self.core.runtime.handle_request(request)
  82. class PlexCall():
  83. def __init__(self):
  84. # Get bundle and channel info for framework initialization.
  85. self.bundle_directory = bootstrap.bundle_directory
  86. self.config = bootstrap.config
  87. self.framework_directory = bootstrap.framework_directory
  88. def tearDown(self):
  89. # Call teardown methods automatically if not called from the child classes.
  90. if hasattr(self, '_core'):
  91. self.teardown_framework()
  92. def setup_framework(self):
  93. # Setup framework core object, either call this from `setUp` method or it'll be called when first accessing
  94. # the object's `core` property.
  95. self._initialize_framework(self.bundle_directory, self.framework_directory, self.config)
  96. def _initialize_framework(self, bundle_path, framework_dir, config):
  97. # Check if the framework has already been initialized.
  98. if hasattr(self, '_core'):
  99. raise RuntimeError('Plex Framework already initialized.')
  100. # Create Framework core object instance.
  101. self._core = FrameworkCore(bundle_path, framework_dir, config)
  102. # Force wait for shared code sandbox loaded event.
  103. self._core.services.shared_code_sandbox
  104. # Initialize framework code and start plugin (same as in framework's bootstrap method `run`).
  105. if not self._core.load_code():
  106. raise RuntimeError('Error loading bundle code.')
  107. self._core.start()
  108. # TODO ielasa no xml faila prefences
  109. default_prefs = {k: v.default_value for k, v in self._core.sandbox.preferences.get()._prefs.items()}
  110. self._core.sandbox.preferences = PreferenceManager(default_prefs)
  111. def teardown_framework(self):
  112. # Tear down framework core object, call this from `tearDown` method.
  113. if hasattr(self, '_core'):
  114. self._core.runtime.taskpool.shutdown()
  115. self._core.runtime.check_threads()
  116. del self._core
  117. @property
  118. def core(self):
  119. # Initialize framework automatically if not yet done.
  120. if not hasattr(self, '_core'):
  121. self.setup_framework()
  122. return self._core
  123. @property
  124. def environment(self):
  125. return self.core.sandbox.environment
  126. @property
  127. def networking(self):
  128. return self.core.networking
  129. @property
  130. def preferences(self):
  131. return self.core.sandbox.preferences.get()
  132. @property
  133. def shared_code_environment(self):
  134. return self.core.services.shared_code_sandbox.environment
  135. def request(self, path, headers={}, method='GET', body=''):
  136. # Make a request to the channel code.
  137. request = BasicRequest(path, headers, method, body)
  138. return self.core.runtime.handle_request(request)