|
| 1 | +from collections.abc import Hashable |
| 2 | +from functools import reduce |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import yaml |
| 7 | + |
| 8 | +from ..exceptions import FirstResolverError |
| 9 | +from ..exceptions import FirstYAMLReaderError |
| 10 | + |
| 11 | + |
| 12 | +class YAMLReader: |
| 13 | + """ |
| 14 | + Open OpenAPI specification from yaml file. The specification from multiple files is supported. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, path: Path): |
| 18 | + self.path = path |
| 19 | + self.root_file_name = self.path.name |
| 20 | + self.store = {} |
| 21 | + |
| 22 | + @staticmethod |
| 23 | + def _yaml_to_dict(path: Path) -> dict: |
| 24 | + with open(path) as f: |
| 25 | + s = yaml.safe_load(f) |
| 26 | + return s |
| 27 | + |
| 28 | + def add_file_to_store(self, ref: str) -> None: |
| 29 | + try: |
| 30 | + file_path, node_path = ref.split('#/') |
| 31 | + except (AttributeError, ValueError): |
| 32 | + raise FirstYAMLReaderError(f'"$ref" with value <{ref}> is not valid.') |
| 33 | + |
| 34 | + if file_path and file_path not in self.store: |
| 35 | + path_to_spec_file = Path(self.path.parent, file_path) |
| 36 | + |
| 37 | + try: |
| 38 | + self.store[file_path] = self._yaml_to_dict(path_to_spec_file) |
| 39 | + except FileNotFoundError: |
| 40 | + raise FirstYAMLReaderError(f'No such file or directory: <{file_path}>') |
| 41 | + |
| 42 | + def search_file(self, obj: dict or list) -> None: |
| 43 | + if isinstance(obj, dict): |
| 44 | + ref = obj.get('$ref') |
| 45 | + if ref: |
| 46 | + self.add_file_to_store(ref) |
| 47 | + else: |
| 48 | + for _, v in obj.items(): |
| 49 | + self.search_file(v) |
| 50 | + |
| 51 | + elif isinstance(obj, list): |
| 52 | + for item in obj: |
| 53 | + self.search_file(item) |
| 54 | + |
| 55 | + else: |
| 56 | + return |
| 57 | + |
| 58 | + def load(self) -> 'YAMLReader': |
| 59 | + root_file = self._yaml_to_dict(self.path) |
| 60 | + self.store[self.root_file_name] = root_file |
| 61 | + self.search_file(root_file) |
| 62 | + return self |
| 63 | + |
| 64 | + |
| 65 | +class Resolver: |
| 66 | + def __init__(self, yaml_reader: YAMLReader): |
| 67 | + self.yaml_reader = yaml_reader |
| 68 | + self.resolved_spec = None |
| 69 | + |
| 70 | + def _get_schema_via_local_ref(self, file_path: str, node_path: str) -> dict: |
| 71 | + keys = node_path.split('/') |
| 72 | + |
| 73 | + def get_value_of_key_from_dict(source_dict: dict, key: Hashable) -> Any: |
| 74 | + return source_dict[key] |
| 75 | + |
| 76 | + try: |
| 77 | + return reduce(get_value_of_key_from_dict, keys, self.yaml_reader.store[file_path]) |
| 78 | + except KeyError: |
| 79 | + raise FirstResolverError(f'No such path: "{node_path}"') |
| 80 | + |
| 81 | + def _get_schema(self, root_file_path: str, ref: str) -> Any: |
| 82 | + try: |
| 83 | + file_path, node_path = ref.split('#/') |
| 84 | + except (AttributeError, ValueError): |
| 85 | + raise FirstResolverError( |
| 86 | + f'"$ref" with value <{ref}> is not valid in file <{root_file_path}>' |
| 87 | + ) |
| 88 | + |
| 89 | + if file_path and node_path: |
| 90 | + obj = self._get_schema_via_local_ref(file_path, node_path) |
| 91 | + |
| 92 | + elif node_path and not file_path: |
| 93 | + obj = self._get_schema_via_local_ref(root_file_path, node_path) |
| 94 | + |
| 95 | + else: |
| 96 | + raise NotImplementedError |
| 97 | + |
| 98 | + return obj |
| 99 | + |
| 100 | + def _resolving_all_refs(self, file_path: str, obj: Any) -> Any: |
| 101 | + if isinstance(obj, dict): |
| 102 | + ref = obj.get('$ref', ...) |
| 103 | + if ref is not ...: |
| 104 | + obj = self._resolving_all_refs(file_path, self._get_schema(file_path, ref)) |
| 105 | + else: |
| 106 | + for key, value in obj.items(): |
| 107 | + obj[key] = self._resolving_all_refs(file_path, value) |
| 108 | + |
| 109 | + if isinstance(obj, list): |
| 110 | + objs = [] |
| 111 | + for item_obj in obj: |
| 112 | + objs.append(self._resolving_all_refs(file_path, item_obj)) |
| 113 | + obj = objs |
| 114 | + |
| 115 | + return obj |
| 116 | + |
| 117 | + def resolving(self) -> 'Resolver': |
| 118 | + root_file_path = self.yaml_reader.root_file_name |
| 119 | + root_spec = self.yaml_reader.store[root_file_path] |
| 120 | + self.resolved_spec = self._resolving_all_refs(root_file_path, root_spec) |
| 121 | + return self |
| 122 | + |
| 123 | + |
| 124 | +def load_from_yaml(path: Path) -> Resolver: |
| 125 | + yaml_reader = YAMLReader(path).load() |
| 126 | + resolved_obj = Resolver(yaml_reader).resolving() |
| 127 | + return resolved_obj.resolved_spec |
0 commit comments