-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_tool.py
More file actions
90 lines (75 loc) · 2.79 KB
/
sync_tool.py
File metadata and controls
90 lines (75 loc) · 2.79 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import eel
import os
import re
import json
# Initialize Eel -> 'web' folder
eel.init('web')
INPUT_DIR = "input"
OUTPUT_DIR = "output"
def extract_data(file_path):
"""
Extracts KEY: VALUE pairs from both JSON and JS/TS files.
"""
data_map = {}
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
try:
# Try parsing as JSON
return json.loads(content)
except json.JSONDecodeError:
# Advanced regex to capture KEY: 'Value' (multiline supported)
# Supports both single and double quotes
pattern = r'([A-Z][A-Z0-9_]+)\s*:\s*(?P<quote>[\'"])(.*?)(?P=quote)'
matches = re.finditer(pattern, content, re.DOTALL)
for match in matches:
key = match.group(1)
value = match.group(3).strip()
data_map[key] = value
return data_map
@eel.expose
def get_files():
"""Return a list of files in the INPUT_DIR folder."""
os.makedirs(INPUT_DIR, exist_ok=True)
extensions = ('.json', '.js', '.ts')
return [f for f in os.listdir(INPUT_DIR) if f.endswith(extensions)]
@eel.expose
def upload_file(filename, content):
"""Receive a file and save it into the input folder"""
try:
os.makedirs(INPUT_DIR, exist_ok=True)
file_path = os.path.join(INPUT_DIR, filename)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
return True
except Exception as e:
print(f"Error saving file: {e}")
return False
@eel.expose
def run_sync(source_file):
"""Sync logic"""
try:
os.makedirs(OUTPUT_DIR, exist_ok=True)
files = get_files()
all_data = {f: extract_data(os.path.join(INPUT_DIR, f)) for f in files}
source_data = all_data[source_file]
results = []
for target_file in files:
if target_file == source_file: continue
target_keys = set(all_data[target_file].keys())
missing_keys = set(source_data.keys()) - target_keys
if missing_keys:
out_path = os.path.join(OUTPUT_DIR, f"missing_in_{target_file}.txt")
with open(out_path, 'w', encoding='utf-8') as f:
f.write(f"// MISSING IN {target_file}\n\n")
for k in sorted(missing_keys):
f.write(f" {k}: '{source_data[k]}',\n")
results.append({"file": target_file, "status": f"Created helper with {len(missing_keys)} keys"})
else:
results.append({"file": target_file, "status": "Already synced"})
return results
except Exception as e:
return str(e)
@eel.expose
def open_output_folder():
os.startfile(os.path.abspath(OUTPUT_DIR))
eel.start('index.html', size=(950, 850))