-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_pb_file.py
executable file
·67 lines (59 loc) · 2.27 KB
/
create_pb_file.py
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
#!/usr/bin/env python3
# Created by ChatGPT from
# The prompt requested a Python script to:
# Input and Output:
# * Read keywords and their replacement values from a file (VARFILE).
# * Substitute those keywords in an input file (permission-boundary-sample.json) with their corresponding values.
# * Write the modified content to an output file (OUTFILE).
#
# Additional Features:
# * Remove all text starting with // on any line.
# * Remove blank lines left behind after processing but preserve other whitespace.
# * Perform substitutions only for whole-word matches of the keywords.
#
# Execution:
# * Accept VARFILE and OUTFILE as command-line arguments.
# * Ensure robust error handling for missing files.
# * The final script meets these requirements while ensuring compatibility and clarity. Let me know if further details are needed!
import sys
import re
# Ensure correct number of arguments
if len(sys.argv) != 3:
print("Usage: python3 script.py INFILE VARFILE OUTFILE")
sys.exit(1)
INFILE = "permission-boundary-sample.json"
VARFILE = sys.argv[1]
OUTFILE = sys.argv[2]
# Load substitutions from VARFILE
substitutions = {}
try:
with open(VARFILE, 'r') as varfile:
for line in varfile:
line = line.strip()
if "=" in line:
key, value = map(str.strip, line.split("=", 1))
substitutions[key] = value
except FileNotFoundError:
print(f"Error: VARFILE '{VARFILE}' not found.")
sys.exit(1)
# Perform substitutions
try:
with open(INFILE, 'r') as infile, open(OUTFILE, 'w') as outfile:
blank_line = False
for line in infile:
# Remove comments starting with //
line = re.sub(r"//.*", "", line)
if not line.strip():
if not blank_line:
outfile.write("\n") # Write a single blank line
blank_line = True
continue
blank_line = False
for key, value in substitutions.items():
# Replace whole words matching the key
line = re.sub(rf"\b{re.escape(key)}\b", value, line)
outfile.write(line)
except FileNotFoundError:
print(f"Error: INFILE '{INFILE}' not found.")
sys.exit(1)
print(f"Substitution complete. Output written to '{OUTFILE}'.")