1
1
import argparse
2
- import dataclasses
3
2
import os
4
3
import pathlib
5
- import re
6
4
import sys
7
5
from typing import Any
8
6
from typing import Callable
9
7
from typing import Dict
10
8
from typing import List
11
- from typing import Optional
12
9
13
10
import yaml
14
11
17
14
from chaotic .front import parser as front_parser
18
15
from chaotic .front import ref_resolver
19
16
from chaotic .front import types
20
-
21
-
22
- @dataclasses .dataclass (init = False )
23
- class NameMapItem :
24
- pattern : re .Pattern
25
- dest : str # string for str.format()
26
-
27
- def __init__ (self , data : str ):
28
- groups = data .split ('=' )
29
- if len (groups ) != 2 :
30
- raise Exception (f'-n arg must contain "=" ({ data } )' )
31
-
32
- pattern , dest = groups
33
- self .pattern = re .compile (pattern )
34
- self .dest = dest
35
-
36
- def match (self , data : str ) -> Optional [str ]:
37
- match = self .pattern .fullmatch (data ) # pylint: disable=no-member
38
- if match :
39
- return self .dest .format (* match .groups ())
40
- return None
17
+ from chaotic .front .parser import NameMapItem
41
18
42
19
43
20
def parse_args () -> argparse .Namespace :
@@ -60,6 +37,13 @@ def parse_args() -> argparse.Namespace:
60
37
help = 'full filepath to virtual filepath mapping' ,
61
38
)
62
39
40
+ parser .add_argument (
41
+ '--plain-object-path-map' ,
42
+ type = NameMapItem ,
43
+ action = 'append' ,
44
+ help = 'plain object filepath to in-file path' ,
45
+ )
46
+
63
47
parser .add_argument (
64
48
'-u' ,
65
49
'--userver' ,
@@ -174,9 +158,17 @@ def traverse_dfs(path: str, data: Any):
174
158
175
159
def extract_schemas_to_scan (
176
160
inp : dict , name_map : List [NameMapItem ],
161
+ fname : str , plain_object_path_map : List [NameMapItem ],
177
162
) -> Dict [str , Any ]:
178
163
schemas = []
179
164
165
+ if plain_object_path_map is not None and ('type' in inp ) and inp ['type' ] == 'object' :
166
+ for item in plain_object_path_map :
167
+ path = item .match (fname )
168
+ if path is not None :
169
+ schemas .append ((path , inp ))
170
+ return dict (schemas )
171
+
180
172
gen = traverse_dfs ('/' , inp )
181
173
ok_ = None
182
174
while True :
@@ -201,6 +193,7 @@ def read_schemas(
201
193
name_map ,
202
194
file_map ,
203
195
dependencies : List [types .ResolvedSchemas ] = [],
196
+ plain_object_path_map : List [NameMapItem ] = None ,
204
197
) -> types .ResolvedSchemas :
205
198
config = front_parser .ParserConfig (erase_prefix = erase_path_prefix )
206
199
rr = ref_resolver .RefResolver ()
@@ -210,11 +203,12 @@ def read_schemas(
210
203
with open (fname ) as ifile :
211
204
data = yaml .load (ifile , Loader = yaml .CLoader )
212
205
213
- scan_objects = extract_schemas_to_scan (data , name_map )
206
+ scan_objects = extract_schemas_to_scan (data , name_map , fname , plain_object_path_map )
214
207
215
208
vfilepath = vfilepath_from_filepath (fname , file_map )
216
209
parser = front_parser .SchemaParser (
217
210
config = config , full_filepath = fname , full_vfilepath = vfilepath ,
211
+ plain_object_path_map = plain_object_path_map ,
218
212
)
219
213
for path , obj in rr .sort_json_types (
220
214
scan_objects , erase_path_prefix ,
@@ -246,7 +240,11 @@ def main() -> None:
246
240
args = parse_args ()
247
241
248
242
schemas = read_schemas (
249
- args .erase_path_prefix , args .file , args .name_map , args .file_map ,
243
+ erase_path_prefix = args .erase_path_prefix ,
244
+ filepaths = args .file ,
245
+ name_map = args .name_map ,
246
+ file_map = args .file_map ,
247
+ plain_object_path_map = args .plain_object_path_map ,
250
248
)
251
249
cpp_name_func = generate_cpp_name_func (
252
250
args .name_map , args .erase_path_prefix ,
0 commit comments