-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_v5python.py
More file actions
112 lines (95 loc) · 3.77 KB
/
convert_v5python.py
File metadata and controls
112 lines (95 loc) · 3.77 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
import json
import os
import sys
from datetime import datetime
def convert_v5python(input_file):
# Load the JSON data from the input file
with open(input_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Extract the Python code from 'textContent'
code = data.get('textContent', '')
if not code:
raise ValueError("No 'textContent' found in the input file.")
# Determine the project name from the file name
base_name = os.path.basename(input_file)
name = base_name.replace('.v5python', '')
# Ensure the name is not empty
if not name:
name = 'UnnamedProject'
project_dir = name
# Create the project directory and subdirectories
os.makedirs(os.path.join(project_dir, 'src'), exist_ok=True)
os.makedirs(os.path.join(project_dir, '.vscode'), exist_ok=True)
# Write the Python code to src/main.py
main_py_path = os.path.join(project_dir, 'src', 'main.py')
with open(main_py_path, 'w', encoding='utf-8') as f:
f.write(code)
# Prepare the vex_project_settings.json content
# Prompt the user for the slot number
while True:
try:
slot_input = input("Enter slot number (1-8): ").strip()
slot = int(slot_input)
if 1 <= slot <= 8:
break
else:
print("Slot number must be between 1 and 8.")
except ValueError:
print("Please enter a valid integer.")
sdk_version = 'V5_1_0_1_25'
creation_date = datetime.now().strftime("%m/%d/%Y, %I:%M:%S %p")
settings = {
"extension": {
"version": "0.7.2025041600",
"json": 2
},
"project": {
"name": name,
"description": "",
"creationDate": creation_date,
"platform": "V5",
"language": "python",
"slot": slot,
"sdkVersion": sdk_version,
"python": {
"main": "src/main.py"
}
}
}
# Write the settings to .vscode/vex_project_settings.json
settings_path = os.path.join(project_dir, '.vscode', 'vex_project_settings.json')
with open(settings_path, 'w', encoding='utf-8') as f:
json.dump(settings, f, indent=4)
# Write the extensions.json file
extensions = {
"recommendations": [
"ms-python.python"
]
}
extensions_path = os.path.join(project_dir, '.vscode', 'extensions.json')
with open(extensions_path, 'w', encoding='utf-8') as f:
json.dump(extensions, f, indent=4)
# Write the settings.json file
stub_path = os.path.join(os.path.expanduser('~'), 'AppData', 'Roaming', 'Code', 'User', 'globalStorage', 'vexrobotics.vexcode', 'sdk', 'python', 'V5', 'V5_1_0_1_25', 'vexv5', 'stubs')
workspace_settings = {
"python.analysis.stubPath": stub_path,
"python.analysis.diagnosticMode": "workspace",
"python.analysis.typeCheckingMode": "basic"
}
settings_json_path = os.path.join(project_dir, '.vscode', 'settings.json')
with open(settings_json_path, 'w', encoding='utf-8') as f:
json.dump(workspace_settings, f, indent=4)
print(f"Project '{name}' created successfully in directory '{project_dir}'.")
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python convert_v5python.py <input_file.v5python>")
sys.exit(1)
input_file = sys.argv[1]
if not os.path.isfile(input_file):
print(f"Error: File '{input_file}' does not exist.")
sys.exit(1)
try:
convert_v5python(input_file)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)