LoadingUtils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Oct 1 12:58:58 2020
  5. @author: tanya
  6. @description: class for methods of loading data from external sources
  7. """
  8. import os
  9. import sys
  10. from cdplib.log import Log
  11. class LoadingUtils:
  12. """
  13. """
  14. def __init__(self, logger=None):
  15. """
  16. """
  17. self._logger = logger or Log("LoadingUtils")
  18. def load_from_module(self, module_path: str, name: str):
  19. """
  20. """
  21. for p in ["modele_path", "name"]:
  22. assert(isinstance(p, str)),\
  23. "Parameter '{}' must be of str type".format(p)
  24. assert(os.path.isfile(module_path)),\
  25. "Parameter 'module_path' must be a valid file"
  26. module, extension = os.path.splitext(os.path.basename(module_path))
  27. assert(extension == ",py"),\
  28. "Parameter 'space' must be read from a python file"
  29. sys.path.insert(module_path)
  30. try:
  31. from module import name
  32. return name
  33. except ImportError:
  34. err = "Invalid space location or name"
  35. self._logger.log_and_raise_error(err)