-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_ids.py
More file actions
99 lines (80 loc) · 3.4 KB
/
Copy pathreplace_ids.py
File metadata and controls
99 lines (80 loc) · 3.4 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
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""Fill placeholder IDs in Persona n8n workflow files before importing."""
import argparse
import json
import sys
from pathlib import Path
PLACEHOLDERS = {
"dt_users": "{{DT_USERS}}",
"dt_sessions": "{{DT_SESSIONS}}",
"dt_login_attempts":"{{DT_LOGIN_ATTEMPTS}}",
"workflow_db": "{{WORKFLOW_DB}}",
}
def replace_in_value(value, replacements):
if isinstance(value, dict):
return {k: replace_in_value(v, replacements) for k, v in value.items()}
if isinstance(value, list):
return [replace_in_value(item, replacements) for item in value]
if isinstance(value, str):
for old, new in replacements.items():
value = value.replace(old, new)
return value
return value
def process_file(path, replacements, output=None):
with open(path) as f:
data = json.load(f)
data = replace_in_value(data, replacements)
result = json.dumps(data, indent=2, ensure_ascii=False)
dest = Path(output) if output else path
dest.parent.mkdir(parents=True, exist_ok=True)
with open(dest, "w") as f:
f.write(result)
remaining = [ph for ph in PLACEHOLDERS.values() if ph in result]
if remaining:
print(f" Warning: unfilled placeholders in {dest.name}: {', '.join(remaining)}", file=sys.stderr)
else:
print(f" OK: {dest}", file=sys.stderr)
def main():
parser = argparse.ArgumentParser(
description="Fill placeholder IDs in Persona n8n workflow JSON files.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
placeholders used in the JSON files:
{{DT_USERS}} → --dt-users
{{DT_SESSIONS}} → --dt-sessions
{{DT_LOGIN_ATTEMPTS}} → --dt-login-attempts
{{WORKFLOW_DB}} → --workflow-db (Persona DB workflow ID)
example:
python replace_ids.py \\
--dt-users oU88XZmCfb87Oxbn \\
--dt-sessions gLuygoEITlAypAmJ \\
--dt-login-attempts 4jjAebbyEijIfBx3 \\
--workflow-db coEXen1CzEdEG3hH \\
persona.json "Persona DB.json"
""",
)
parser.add_argument("files", nargs="+", metavar="file", help="n8n JSON workflow file(s) to update")
parser.add_argument("--dt-users", metavar="ID", help="Users datatable ID")
parser.add_argument("--dt-sessions", metavar="ID", help="Sessions datatable ID")
parser.add_argument("--dt-login-attempts", metavar="ID", help="Login attempts datatable ID")
parser.add_argument("--workflow-db", metavar="ID", help="Persona DB workflow ID")
parser.add_argument("--output-dir", metavar="DIR", help="Write results to this directory instead of editing in place")
args = parser.parse_args()
replacements = {}
mapping = {
"dt_users": args.dt_users,
"dt_sessions": args.dt_sessions,
"dt_login_attempts": args.dt_login_attempts,
"workflow_db": args.workflow_db,
}
for key, value in mapping.items():
if value:
replacements[PLACEHOLDERS[key]] = value
if not replacements:
parser.error("Provide at least one ID argument (--dt-users, --dt-sessions, --dt-login-attempts, --workflow-db)")
for path_str in args.files:
path = Path(path_str)
output = str(Path(args.output_dir) / path.name) if args.output_dir else None
process_file(path, replacements, output=output)
if __name__ == "__main__":
main()