|
| 1 | +import collections |
| 2 | +import pathlib |
| 3 | +from typing import Dict |
| 4 | +from typing import List |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import yaml |
| 8 | + |
| 9 | +from . import renderer |
| 10 | + |
| 11 | +TYPES_INCLUDES = [ |
| 12 | + 'cstdint', |
| 13 | + 'fmt/core.h', |
| 14 | + 'optional', |
| 15 | + 'string', |
| 16 | +] |
| 17 | + |
| 18 | + |
| 19 | +def _get_template_includes(name: str, client_name: str, graph: Dict[str, List[str]]) -> List[str]: |
| 20 | + includes = { |
| 21 | + 'client.cpp': [ |
| 22 | + f'client/{client_name}/client.hpp', |
| 23 | + ], |
| 24 | + 'client.hpp': [ |
| 25 | + 'requests.hpp', |
| 26 | + 'responses.hpp', |
| 27 | + 'userver/chaotic/openapi/client/command_control.hpp', |
| 28 | + ], |
| 29 | + 'client_fwd.hpp': [], |
| 30 | + 'client_impl.cpp': [ |
| 31 | + f'client/{client_name}/client_impl.hpp', |
| 32 | + 'userver/chaotic/openapi/middlewares/follow_redirects_middleware.hpp', |
| 33 | + 'userver/chaotic/openapi/middlewares/qos_middleware.hpp', |
| 34 | + 'userver/components/component_context.hpp', |
| 35 | + 'userver/components/component_base.hpp', |
| 36 | + 'userver/yaml_config/merge_schemas.hpp', |
| 37 | + ], |
| 38 | + 'client_impl.hpp': [ |
| 39 | + f'client/{client_name}/client.hpp', |
| 40 | + 'userver/chaotic/openapi/client/config.hpp', |
| 41 | + 'userver/chaotic/openapi/middlewares/manager.hpp', |
| 42 | + 'userver/clients/http/client.hpp', |
| 43 | + 'userver/components/component_config.hpp', |
| 44 | + 'userver/yaml_config/schema.hpp', |
| 45 | + ], |
| 46 | + 'exceptions.cpp': [ |
| 47 | + f'client/{client_name}/exceptions.hpp', |
| 48 | + 'userver/clients/http/error_kind.hpp', |
| 49 | + ], |
| 50 | + 'exceptions.hpp': [ |
| 51 | + 'userver/chaotic/openapi/client/exceptions.hpp', |
| 52 | + ], |
| 53 | + 'component.cpp': [ |
| 54 | + f'client/{client_name}/component.hpp', |
| 55 | + 'userver/chaotic/openapi/client/config.hpp', |
| 56 | + 'userver/components/component_context.hpp', |
| 57 | + 'userver/clients/http/component.hpp', |
| 58 | + ], |
| 59 | + 'component.hpp': [ |
| 60 | + 'userver/components/component_base.hpp', |
| 61 | + 'userver/yaml_config/schema.hpp', |
| 62 | + f'client/{client_name}/client_impl.hpp', |
| 63 | + ], |
| 64 | + 'requests.cpp': [ |
| 65 | + f'client/{client_name}/requests.hpp', |
| 66 | + 'userver/chaotic/openapi/parameters_write.hpp', |
| 67 | + 'userver/formats/json/value_builder.hpp', |
| 68 | + 'userver/http/common_headers.hpp', |
| 69 | + 'userver/http/url.hpp', |
| 70 | + 'userver/clients/http/form.hpp', |
| 71 | + 'userver/chaotic/openapi/form.hpp', |
| 72 | + ], |
| 73 | + 'requests.hpp': [ |
| 74 | + 'string', |
| 75 | + 'variant', |
| 76 | + *[f'client/{client_name}/{dep}' for dep in graph], |
| 77 | + # TODO |
| 78 | + ], |
| 79 | + 'responses.cpp': [ |
| 80 | + f'client/{client_name}/responses.hpp', |
| 81 | + 'userver/clients/http/response.hpp', |
| 82 | + 'userver/formats/json/serialize.hpp', |
| 83 | + 'userver/http/common_headers.hpp', |
| 84 | + 'userver/http/content_type.hpp', |
| 85 | + 'userver/logging/log.hpp', |
| 86 | + ], |
| 87 | + 'responses.hpp': [ |
| 88 | + 'variant', |
| 89 | + f'client/{client_name}/exceptions.hpp', |
| 90 | + 'userver/chaotic/openapi/client/exceptions.hpp', |
| 91 | + *[f'client/{client_name}/{dep}' for dep in graph], |
| 92 | + # TODO |
| 93 | + ], |
| 94 | + } |
| 95 | + return includes[name] |
| 96 | + |
| 97 | + |
| 98 | +def trim_suffix(string: str, suffix: str) -> Optional[str]: |
| 99 | + if string.endswith(suffix): |
| 100 | + return string[: -len(suffix)] |
| 101 | + return None |
| 102 | + |
| 103 | + |
| 104 | +def extract_includes(name: str, path: pathlib.Path) -> Optional[List[str]]: |
| 105 | + with open(path) as ifile: |
| 106 | + content = yaml.safe_load(ifile) |
| 107 | + |
| 108 | + includes: List[str] = [] |
| 109 | + |
| 110 | + def visit(data) -> None: |
| 111 | + if isinstance(data, dict): |
| 112 | + for v in data.values(): |
| 113 | + visit(v) |
| 114 | + if '$ref' in data: |
| 115 | + ref = data['$ref'] |
| 116 | + ref = ref.split('#')[0] |
| 117 | + ref_fname = ref.rsplit('/', 1)[-1] |
| 118 | + if ref_fname: |
| 119 | + stem = ref_fname.rsplit('.', 1)[0] |
| 120 | + includes.append(f'client/{name}/{stem}.hpp') |
| 121 | + if 'x-taxi-cpp-type' in data: |
| 122 | + pass |
| 123 | + if 'x-usrv-cpp-type' in data: |
| 124 | + pass |
| 125 | + elif isinstance(data, list): |
| 126 | + for v in data: |
| 127 | + visit(v) |
| 128 | + |
| 129 | + if 'definitions' in content or 'components' in content or 'paths' in content: |
| 130 | + visit(content) |
| 131 | + return includes |
| 132 | + else: |
| 133 | + return None |
| 134 | + |
| 135 | + |
| 136 | +def include_graph(name: str, schemas_dir: pathlib.Path) -> Dict[str, List[str]]: |
| 137 | + result = {} |
| 138 | + for root, _, filenames in schemas_dir.walk(): |
| 139 | + for filename in filenames: |
| 140 | + filepath = pathlib.Path(root) / filename |
| 141 | + if filepath == schemas_dir / 'client.yaml' or filename == 'a.yaml': |
| 142 | + continue |
| 143 | + result[filepath.stem + '.hpp'] = extract_includes(name, filepath) |
| 144 | + |
| 145 | + return {key: result[key] for key in result if result[key] is not None} # type: ignore |
| 146 | + |
| 147 | + |
| 148 | +def get_includes(client_name: str, schemas_dir: str) -> Dict[str, List[str]]: |
| 149 | + graph = include_graph(client_name, pathlib.Path(schemas_dir)) |
| 150 | + |
| 151 | + output = collections.defaultdict(list) |
| 152 | + for name in renderer.TEMPLATE_NAMES: |
| 153 | + if name.endswith('.hpp'): |
| 154 | + rel_path = f'include/client/{client_name}/{name}' |
| 155 | + else: |
| 156 | + rel_path = f'src/client/{client_name}/{name}' |
| 157 | + output[rel_path] = _get_template_includes(name, client_name, graph) |
| 158 | + |
| 159 | + for file in graph: |
| 160 | + stem = pathlib.Path(file).stem |
| 161 | + output[f'include/client/{client_name}/{stem}_fwd.hpp'] = [] |
| 162 | + output[f'include/client/{client_name}/{stem}.hpp'] = [ |
| 163 | + f'client/{client_name}/{stem}_fwd.hpp', |
| 164 | + 'userver/chaotic/type_bundle_hpp.hpp', |
| 165 | + *TYPES_INCLUDES, |
| 166 | + *graph[file], |
| 167 | + ] |
| 168 | + |
| 169 | + return output |
| 170 | + |
| 171 | + |
| 172 | +def external_libraries(schemas_dir: str) -> List[str]: |
| 173 | + types = set() |
| 174 | + |
| 175 | + def visit(data) -> None: |
| 176 | + if isinstance(data, dict): |
| 177 | + for v in data.values(): |
| 178 | + visit(v) |
| 179 | + if 'x-taxi-cpp-type' in data: |
| 180 | + types.add(data['x-taxi-cpp-type']) |
| 181 | + if 'x-usrv-cpp-type' in data: |
| 182 | + types.add(data['x-usrv-cpp-type']) |
| 183 | + elif isinstance(data, list): |
| 184 | + for v in data: |
| 185 | + visit(v) |
| 186 | + |
| 187 | + for file in pathlib.Path(schemas_dir).rglob('*.yaml'): |
| 188 | + with open(file) as ifile: |
| 189 | + content = yaml.safe_load(ifile) |
| 190 | + visit(content) |
| 191 | + |
| 192 | + libraries = [] |
| 193 | + for type_ in types: |
| 194 | + libraries.append(type_.split('::')[0].replace('_', '-')) |
| 195 | + return libraries |
0 commit comments