LabelProvider.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import json
  2. from os import path
  3. from pycs.interfaces.LabelProvider import LabelProvider as LabelProviderInterface
  4. class LabelProvider:
  5. """
  6. database class for label providers
  7. """
  8. def __init__(self, database, row):
  9. self.database = database
  10. self.identifier = row[0]
  11. self.name = row[1]
  12. self.description = row[2]
  13. self.root_folder = row[3]
  14. self.configuration_file = row[4]
  15. @property
  16. def configuration_path(self):
  17. return path.join(self.root_folder, self.configuration_file)
  18. def load(self) -> LabelProviderInterface:
  19. """
  20. load configuration.json and create an instance from the included code object
  21. :return: LabelProvider instance
  22. """
  23. # load configuration.json
  24. with open(self.configuration_path, 'r') as configuration_file:
  25. configuration = json.load(configuration_file)
  26. # load code
  27. code_path = path.join(self.root_folder, configuration['code']['module'])
  28. module_name = code_path.replace('/', '.').replace('\\', '.')
  29. class_name = configuration['code']['class']
  30. imported_module = __import__(module_name, fromlist=[class_name])
  31. class_attr = getattr(imported_module, class_name)
  32. # return instance
  33. return class_attr(self.root_folder, configuration)