-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_manager.py
More file actions
46 lines (37 loc) · 1.67 KB
/
config_manager.py
File metadata and controls
46 lines (37 loc) · 1.67 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
import json
import os
import subprocess
CONFIG_FILENAME = ".coder-config.json"
def is_python_project(project_dir="."):
for root, _, files in os.walk(project_dir):
for filename in files:
if filename.endswith(".py"):
return True
return False
def get_working_compile_command(project_dir="."):
cmds = [("python", "-m", "compileall"), ("python3", "-m", "compileall")]
for cmd in cmds:
command = list(cmd) + [project_dir]
try:
if subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).returncode == 0:
return " ".join(cmd)
except Exception:
continue
return None
def load_config(project_dir="."):
path = os.path.join(project_dir, CONFIG_FILENAME)
if os.path.exists(path):
with open(path, "r") as f:
return json.load(f)
return None
def create_config(project_dir="."):
if is_python_project(project_dir):
compile_command = get_working_compile_command(project_dir)
file_types = [ft.strip() for ft in input("Enter comma-separated file extensions (default .py): ").split(",") if ft.strip()] or ['.py']
print("Python project detected. Automatically creating .coder-config.json.")
else:
compile_command = input("Enter the compile command for your language: ").strip()
file_types = [ft.strip() for ft in input("Enter comma-separated file extensions (e.g., .py, .c, .java): ").split(",") if ft.strip()]
config = {"compile_command": compile_command, "file_types": file_types}
with open(os.path.join(project_dir, CONFIG_FILENAME), "w") as f: json.dump(config, f, indent=2)
return config