-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths_routes.py
More file actions
42 lines (37 loc) · 1.33 KB
/
Copy pathpaths_routes.py
File metadata and controls
42 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import json
def normalize_path(path):
while '//' in path:
path = path.replace('//', '/')
if path != '/' and path.endswith('/'):
path = path[:-1]
return path or '/'
def extract_routes(app):
routes = []
for route in app.routes:
if hasattr(route, 'methods'):
for method in route.methods:
if method in ['HEAD', 'OPTIONS']:
continue
routes.append({
'method': method,
'path': normalize_path(route.path)
})
routes.sort(key=lambda x: (x['path'], x['method']))
return routes
def generate_and_save_routes(app, port=5000, output_path='../exocore-web/models/routes.json'):
routes = extract_routes(app)
data = {
'port': port,
'routes': routes
}
output_dir = os.path.dirname(output_path)
if not os.path.exists(output_dir):
os.makedirs(output_dir, exist_ok=True)
print(f'[paths_routes.py] Created output directory: {output_dir}')
try:
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
print(f'[paths_routes.py] Successfully saved {len(routes)} routes to {output_path}')
except Exception as e:
print(f'[paths_routes.py] Error saving routes to {output_path}: {e}')