Skip to content

Commit 78fdff4

Browse files
authored
Merge pull request #536 from necusjz/improve-spec-module-loading
perf: reduce module loading time
2 parents 96aee05 + 205a59a commit 78fdff4

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/aaz_dev/swagger/model/specs/_typespec_helper.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,43 +45,51 @@ def find_data_plane_entry_files(cls, folder):
4545

4646
@classmethod
4747
def _parse_main_tsp(cls, path):
48-
def expand(import_path):
49-
with open(import_path, "r", encoding="utf-8") as f:
50-
lines = f.readlines()
48+
def load_file_lines(path):
49+
with open(path, "r", encoding="utf-8") as f:
50+
return f.readlines()
5151

52-
base = os.path.dirname(import_path)
52+
def parse_by_line(lines):
53+
namespace, is_mgmt_plane = None, False
54+
for line in lines:
55+
if line.startswith("@armProviderNamespace"):
56+
is_mgmt_plane = True
57+
58+
if line.startswith("namespace "):
59+
assert namespace is None
60+
namespace = re.match(r"^namespace\s+([A-Za-z0-9.]+)", line).group(1)
61+
# armProviderNamespace will always be appeared before namespace
62+
break
63+
64+
return namespace, is_mgmt_plane
65+
66+
def expand(path):
67+
base = os.path.dirname(path)
5368

5469
content = []
5570
for line in lines:
56-
if match := import_pattern.findall(line):
71+
if match := re.compile(r'^import "([^"]+)"').findall(line):
5772
rel_path = match[0]
5873
abs_path = os.path.abspath(os.path.join(base, rel_path))
5974

60-
if os.path.isfile(abs_path): # expand first level only; otherwise, will impact performance
75+
if os.path.isfile(abs_path): # expand first level only; otherwise, may have circular reference
6176
with open(abs_path, "r", encoding="utf-8") as f:
6277
content.append("".join(f.readlines()))
63-
6478
else:
6579
content.append(line)
6680
else:
6781
content.append(line)
6882

6983
return "".join(content).split("\n")
7084

71-
import_pattern = re.compile(r'^import "([^"]+)"')
85+
lines = load_file_lines(path)
86+
if any("@armProviderNamespace" in line for line in lines):
87+
namespace, is_mgmt_plane = parse_by_line(lines)
7288

73-
is_mgmt_plane = False
74-
namespace = None
89+
else:
90+
namespace, is_mgmt_plane = parse_by_line(expand(path))
7591

76-
for line in expand(path):
77-
if line.startswith("@armProviderNamespace"):
78-
is_mgmt_plane = True
79-
if line.startswith("namespace "):
80-
assert namespace is None
81-
namespace = re.match(r"^namespace\s+([A-Za-z0-9.]+)", line).group(1)
82-
# armProviderNamespace will always be appeared before namespace
83-
break
8492
if namespace is None:
85-
# logger.warning("Failed to parse main tsp file: %s namespace is not exist.", path)
8693
return None, None
94+
8795
return namespace, is_mgmt_plane

0 commit comments

Comments
 (0)