feature_spec.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import yaml
  15. import os
  16. from typing import List, Dict
  17. class FeatureSpec:
  18. def __init__(self, feature_spec, source_spec, channel_spec, metadata, base_directory):
  19. self.feature_spec: Dict = feature_spec
  20. self.source_spec: Dict = source_spec
  21. self.channel_spec: Dict = channel_spec
  22. self.metadata: Dict = metadata
  23. self.base_directory: str = base_directory
  24. @classmethod
  25. def from_yaml(cls, path):
  26. with open(path, 'r') as feature_spec_file:
  27. base_directory = os.path.dirname(path)
  28. feature_spec = yaml.safe_load(feature_spec_file)
  29. return cls.from_dict(feature_spec, base_directory=base_directory)
  30. @classmethod
  31. def from_dict(cls, source_dict, base_directory):
  32. return cls(base_directory=base_directory, **source_dict)
  33. def to_dict(self) -> Dict:
  34. attributes_to_dump = ['feature_spec', 'source_spec', 'channel_spec', 'metadata']
  35. return {attr: self.__dict__[attr] for attr in attributes_to_dump}
  36. def to_string(self):
  37. return yaml.dump(self.to_dict())
  38. def to_yaml(self, output_path=None):
  39. if not output_path:
  40. output_path = self.base_directory + '/feature_spec.yaml'
  41. with open(output_path, 'w') as output_file:
  42. print(yaml.dump(self.to_dict()), file=output_file)