LabelProvider.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from pycs import db
  2. from pycs.database.base import NamedBaseModel
  3. class LabelProvider(NamedBaseModel):
  4. """
  5. database class for label providers
  6. """
  7. description = db.Column(db.String)
  8. root_folder = db.Column(db.String, nullable=False, unique=True)
  9. # relationships to other models
  10. projects = db.relationship("Project", backref="label_provider", lazy=True)
  11. @property
  12. def configuration_path(self):
  13. return path.join(self.root_folder, self.configuration_file)
  14. def load(self) -> LabelProviderInterface:
  15. """
  16. load configuration.json and create an instance from the included code object
  17. :return: LabelProvider instance
  18. """
  19. # load configuration.json
  20. with open(self.configuration_path, 'r') as configuration_file:
  21. configuration = json.load(configuration_file)
  22. # load code
  23. code_path = path.join(self.root_folder, configuration['code']['module'])
  24. module_name = code_path.replace('/', '.').replace('\\', '.')
  25. class_name = configuration['code']['class']
  26. imported_module = __import__(module_name, fromlist=[class_name])
  27. class_attr = getattr(imported_module, class_name)
  28. # return instance
  29. return class_attr(self.root_folder, configuration)