-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
321 lines (246 loc) · 10.6 KB
/
manage.py
File metadata and controls
321 lines (246 loc) · 10.6 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python3
import os
import json
import argparse
import tempfile
import subprocess
import sys
from pathlib import Path
def get_user_input_with_editor(initial_content=""):
"""Get user input using the default editor."""
with tempfile.NamedTemporaryFile(suffix=".tmp", mode="w+", delete=False) as temp:
temp.write(initial_content)
temp.flush()
temp_path = temp.name
# Get default editor from environment or fall back to vim/nano
editor = os.environ.get('EDITOR', 'vim' if os.system('which vim > /dev/null 2>&1') == 0 else 'nano')
try:
subprocess.check_call([editor, temp_path])
except subprocess.CalledProcessError:
print(f"Error opening editor {editor}. Please set the EDITOR environment variable.")
os.unlink(temp_path)
return None
with open(temp_path, 'r') as temp:
content = temp.read()
os.unlink(temp_path)
return content
def generate_editor_template(test_data=None):
"""Generate a template for the editor, optionally pre-populated with test data."""
if test_data is None:
# Default empty template
return """\
# Edit the test details below. Lines starting with # will be ignored.
# Save and close the editor when done.
name: Test name here
# For input expression, write it between the START and END markers below
# You can use multiple lines for complex TokenScript expressions
# START_INPUT
Your TokenScript expression here
# END_INPUT
# For expected output, write it between the START and END markers below
# START_EXPECTED_OUTPUT
Expected result here
# END_EXPECTED_OUTPUT
expectedOutputType: Number # or String, Boolean, etc.
inline: true # Set to true for inline, false for script interpretation
# Context (variables) section - use JSON format
# Remove the {} and add your context variables if needed, e.g.:
# {
# "x": 3,
# "y": "hello"
# }
context: {}
"""
else:
# Pre-populate template with existing test data
# Convert the context back to JSON string with pretty formatting
context_str = json.dumps(test_data.get("context", {}), indent=2)
# Handle the expected output type
output_type = test_data.get("exceptedOutputType", "") or test_data.get("expectedOutputType", "")
# Handle the inline field
inline_val = test_data.get("inline", True)
# Create template with existing data
return f"""\
# Edit the test details below. Lines starting with # will be ignored.
# Save and close the editor when done.
name: {test_data.get("name", "Test name here")}
# For input expression, write it between the START and END markers below
# You can use multiple lines for complex TokenScript expressions
# START_INPUT
{test_data.get("input", "Your TokenScript expression here")}
# END_INPUT
# For expected output, write it between the START and END markers below
# START_EXPECTED_OUTPUT
{test_data.get("expectedOutput", "Expected result here")}
# END_EXPECTED_OUTPUT
expectedOutputType: {output_type} # or String, Boolean, etc.
inline: {str(inline_val).lower()} # Set to true for inline, false for script interpretation
# Context (variables) section - use JSON format
context: {context_str}
"""
def parse_editor_content(user_input):
"""Parse the content from the editor."""
test_data = {}
context_json = "{}"
# Extract multi-line input
sections = {
"input": {"start": "# START_INPUT", "end": "# END_INPUT"},
"expectedOutput": {"start": "# START_EXPECTED_OUTPUT", "end": "# END_EXPECTED_OUTPUT"}
}
lines = user_input.split('\n')
current_section = None
section_content = []
for line in lines:
line_stripped = line.strip()
# Check if line starts a section
for section, markers in sections.items():
if line_stripped == markers["start"]:
current_section = section
section_content = []
break
elif line_stripped == markers["end"]:
if current_section:
# Join the collected lines and assign to test_data
section_text = '\n'.join(section_content).strip()
test_data[current_section] = section_text
current_section = None
break
# If we're in a section, collect the content
if current_section and not line_stripped.startswith("#") and not line_stripped in [
sections[current_section]["start"], sections[current_section]["end"]]:
section_content.append(line)
# Process regular key-value lines outside of sections
if not current_section and ":" in line and not line.strip().startswith('#'):
key, value = line.split(":", 1)
key = key.strip()
value = value.strip()
if key == "expectedOutputType":
output_type = value.split("#")[0].strip() # Remove any comments
test_data[key] = output_type
elif key == "inline":
# Accept true/false as bool
test_data[key] = value.split("#")[0].strip().lower() == "true"
elif key not in test_data and key != "context" and key not in sections:
test_data[key] = value
if key == "context":
context_json = value
# Parse context JSON
try:
test_data["context"] = json.loads(context_json)
except json.JSONDecodeError:
print("Error parsing context JSON. Please check the format.")
sys.exit(1)
return test_data
def find_test_files():
"""Find all test files recursively in the tests directory."""
test_files = []
tests_dir = Path("tests")
if not tests_dir.exists() or not tests_dir.is_dir():
return []
for path in tests_dir.glob("**/*.json"):
test_files.append(str(path))
return test_files
def select_test_to_edit(test_files):
"""Display a menu of test files to edit."""
if not test_files:
print("No test files found.")
sys.exit(1)
print("Available test files:")
for i, path in enumerate(test_files, 1):
print(f"{i}. {path}")
while True:
try:
choice = input("\nSelect a test to edit (number) or 'q' to quit: ")
if choice.lower() == 'q':
sys.exit(0)
index = int(choice) - 1
if 0 <= index < len(test_files):
return test_files[index]
else:
print("Invalid selection. Please try again.")
except ValueError:
print("Please enter a valid number.")
def create_or_edit_test(edit_mode=False, test_path=None):
"""Create a new test or edit an existing one."""
test_data = None
if edit_mode:
if not test_path:
test_files = find_test_files()
test_path = select_test_to_edit(test_files)
print(f"Editing test file: {test_path}")
try:
with open(test_path, 'r') as f:
test_data = json.load(f)
except (json.JSONDecodeError, FileNotFoundError) as e:
print(f"Error opening or parsing test file: {e}")
sys.exit(1)
template = generate_editor_template(test_data)
action_desc = "Editing"
else:
template = generate_editor_template()
action_desc = "Creating"
print(f"{action_desc} a TokenScript compliance test")
print("Opening editor to input test details...")
user_input = get_user_input_with_editor(template)
if not user_input:
print("No input provided. Exiting.")
sys.exit(1)
test_data = parse_editor_content(user_input)
# Determine the output path
if edit_mode and test_path:
test_file_path = test_path
else:
# Get the output location for new tests
parser = argparse.ArgumentParser(description='Create a TokenScript compliance test')
parser.add_argument('--category', default='math', help='Test category (subdirectory under tests/)')
parser.add_argument('--filename', help='Filename for the test (without .json extension)')
args = parser.parse_args()
if not args.filename:
filename = test_data.get("name", "new_test").lower().replace(" ", "_")
args.filename = filename
# Ensure the filename has .json extension
if not args.filename.endswith('.json'):
args.filename += '.json'
# Create the directory if it doesn't exist
test_dir = Path('tests') / args.category
test_dir.mkdir(parents=True, exist_ok=True)
# Full path for the test file
test_file_path = test_dir / args.filename
# Ask for confirmation if file exists
if test_file_path.exists():
confirm = input(f"File {test_file_path} already exists. Overwrite? [y/N]: ")
if not confirm.lower().startswith('y'):
print("Operation cancelled.")
sys.exit(0)
# Fix the misspelled key if exists in edited content
if "exceptedOutputType" in test_data and "expectedOutputType" not in test_data:
test_data["expectedOutputType"] = test_data.pop("exceptedOutputType")
# Write the test to file
with open(test_file_path, 'w') as f:
json.dump(test_data, f, indent=4)
print(f"Test successfully saved at: {test_file_path}")
def main():
"""Main function to parse arguments and call appropriate functions."""
parser = argparse.ArgumentParser(description='Create or edit TokenScript compliance tests')
# Add arguments for the main parser (for default create mode)
parser.add_argument('--category', default='math', help='Test category (subdirectory under tests/)')
parser.add_argument('--filename', help='Filename for the test (without .json extension)')
# Add subparsers for explicit create and edit modes
subparsers = parser.add_subparsers(dest='mode', help='Mode of operation')
# Create parser
create_parser = subparsers.add_parser('create', help='Create a new test')
create_parser.add_argument('--category', default='math', help='Test category (subdirectory under tests/)')
create_parser.add_argument('--filename', help='Filename for the test (without .json extension)')
# Edit parser
edit_parser = subparsers.add_parser('edit', help='Edit an existing test')
edit_parser.add_argument('--path', help='Path to the test file to edit')
# Parse arguments
args = parser.parse_args()
# Handle the command based on mode
if args.mode == 'edit':
create_or_edit_test(edit_mode=True, test_path=args.path)
else:
# Both default mode and explicit 'create' mode use these arguments
create_or_edit_test(edit_mode=False)
if __name__ == '__main__':
main()