1
1

LabelProviderUtil.py 966 B

12345678910111213141516171819202122232425262728
  1. from json import load
  2. from os import path
  3. from pycs.interfaces.LabelProvider import LabelProvider
  4. def load_from_root_folder(root_folder: str) -> LabelProvider:
  5. """
  6. load configuration.json and create an instance from the included code object
  7. :param root_folder: path to label provider root folder
  8. :return: LabelProvider instance
  9. """
  10. # load configuration.json
  11. configuration_path = path.join(root_folder, 'configuration.json')
  12. with open(configuration_path, 'r') as configuration_file:
  13. configuration = load(configuration_file)
  14. # load code
  15. code_path = path.join(root_folder, configuration['code']['module'])
  16. module_name = code_path.replace('/', '.').replace('\\', '.')
  17. class_name = configuration['code']['class']
  18. imported_module = __import__(module_name, fromlist=[class_name])
  19. class_attr = getattr(imported_module, class_name)
  20. # return instance
  21. return class_attr(root_folder, configuration)