provider_utils.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import importlib
  17. import pathlib
  18. from fate_flow.entity import ComponentProvider
  19. from fate_flow.utils.log_utils import getLogger
  20. LOGGER = getLogger()
  21. def get_provider_interface(provider: ComponentProvider):
  22. obj = get_provider_class_object(provider, "interface")
  23. for i in ('name', 'version', 'path'):
  24. setattr(obj, f'provider_{i}', getattr(provider, i))
  25. return obj
  26. def get_provider_model_paths(provider: ComponentProvider):
  27. model_import_path = get_provider_class_import_path(provider, "model")
  28. model_module_dir = pathlib.Path(provider.path).joinpath(*model_import_path[1:])
  29. return model_module_dir, model_import_path
  30. def get_provider_class_object(provider: ComponentProvider, class_name, module=False):
  31. class_path = get_provider_class_import_path(provider, class_name)
  32. if module:
  33. return importlib.import_module(".".join(class_path))
  34. else:
  35. return getattr(importlib.import_module(".".join(class_path[:-1])), class_path[-1])
  36. def get_provider_class_import_path(provider: ComponentProvider, class_name):
  37. return f"{pathlib.Path(provider.path).name}.{provider.class_path.get(class_name)}".split(".")