__init__.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. from pathlib import Path
  18. import inspect
  19. def get_proto_buffer_class(buffer_name):
  20. package_base_path = Path(__file__).absolute().parent.parent.parent
  21. package_path = Path(__file__).absolute().parent.joinpath("generated")
  22. for f in package_path.glob("*.py"):
  23. module_rel_path = package_path.joinpath(f.stem).relative_to(package_base_path)
  24. module_path = f"{module_rel_path}".replace("/", ".")
  25. proto_module = importlib.import_module(module_path)
  26. for name, obj in inspect.getmembers(proto_module):
  27. if inspect.isclass(obj) and name == buffer_name:
  28. return obj
  29. raise ModuleNotFoundError(buffer_name)
  30. def parse_pb_buffer(pb_name, pb_buffer):
  31. pb_object = get_proto_buffer_class(pb_name)()
  32. pb_object.ParseFromString(pb_buffer)
  33. return pb_object
  34. def deserialize_models(model_input):
  35. for model_type, models in model_input.items():
  36. for cpn_name, cpn_models in models.items():
  37. for model_name, (pb_name, pb_buffer) in cpn_models.items():
  38. model_input[model_type][cpn_name][model_name] = parse_pb_buffer(pb_name, pb_buffer)