-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
313 lines (233 loc) · 9.07 KB
/
app.py
File metadata and controls
313 lines (233 loc) · 9.07 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
import os
import json
import importlib.util
import inspect
import traceback
from flask import Flask, request, jsonify, render_template, send_file
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# --- Configuration & Paths ---
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PLUGIN_DIR = os.path.join(BASE_DIR, 'plugins')
HISTORY_DIR = os.path.join(BASE_DIR, 'history')
SESSION_FILE = os.path.join(BASE_DIR, 'last_session.json')
CONFIG_FILE = os.path.join(BASE_DIR, 'config_info.json')
DEFAULT_WS = os.path.join(BASE_DIR, 'workspaces')
HISTORY_META_FILE = os.path.join(HISTORY_DIR, 'meta.json')
MAX_HISTORY = 50
# Ensure necessary directories exist on startup
for path in [PLUGIN_DIR, DEFAULT_WS, HISTORY_DIR]:
if not os.path.exists(path):
os.makedirs(path)
def get_config():
"""Reads the workspace configuration, falling back to default if missing."""
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, 'r') as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
pass
return {"workspace": DEFAULT_WS}
def read_history_meta():
"""Returns history metadata: {pointer, count}"""
if os.path.exists(HISTORY_META_FILE):
try:
with open(HISTORY_META_FILE, 'r') as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
pass
return {"pointer": -1, "count": 0}
def write_history_meta(meta):
with open(HISTORY_META_FILE, 'w') as f:
json.dump(meta, f)
def history_path(index):
return os.path.join(HISTORY_DIR, f'session_{index:04d}.json')
# --- Routes ---
@app.route('/')
def hello_world():
return render_template("index.html")
@app.route('/get_workspace', methods=['GET'])
def get_workspace_route():
return jsonify(get_config())
@app.route('/set_workspace', methods=['POST'])
def set_workspace():
path = request.json.get('path')
if not path:
return jsonify({"error": "No path provided"}), 400
config = {"workspace": path}
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=4)
return jsonify(config)
@app.route('/list_workspaces', methods=['GET'])
def list_workspaces():
current = get_config()['workspace']
parent = os.path.dirname(current)
try:
options = [os.path.join(parent, d) for d in os.listdir(parent)
if os.path.isdir(os.path.join(parent, d))]
return jsonify(options[:15])
except Exception:
return jsonify([DEFAULT_WS])
# --- Session Save / Load ---
@app.route('/save', methods=['POST'])
def save_config():
"""Saves the current sequence to last_session.json (autosave target)."""
scripts = request.json
with open(SESSION_FILE, 'w') as f:
json.dump(scripts, f, indent=4)
return jsonify({"status": "success"})
@app.route('/save_as', methods=['POST'])
def save_as():
"""Saves sequence to a named file inside the workspace."""
data = request.json
filename = data.get('filename', '').strip()
scripts = data.get('scripts', [])
if not filename:
return jsonify({"error": "No filename provided"}), 400
# Sanitize: only allow .json extension, no path traversal
basename = os.path.basename(filename)
if not basename.endswith('.json'):
basename += '.json'
workspace = get_config()['workspace']
dest = os.path.join(workspace, basename)
with open(dest, 'w') as f:
json.dump(scripts, f, indent=4)
return jsonify({"status": "success", "saved_to": dest, "filename": basename})
@app.route('/load', methods=['GET'])
def load_config():
"""Loads the last autosaved sequence."""
if os.path.exists(SESSION_FILE):
try:
with open(SESSION_FILE, 'r') as f:
return jsonify(json.load(f))
except (json.JSONDecodeError, IOError):
return jsonify([])
return jsonify([])
@app.route('/load_file', methods=['POST'])
def load_file():
"""Loads a sequence from a named file in the workspace."""
filename = request.json.get('filename', '').strip()
if not filename:
return jsonify({"error": "No filename provided"}), 400
basename = os.path.basename(filename)
workspace = get_config()['workspace']
path = os.path.join(workspace, basename)
if not os.path.exists(path):
return jsonify({"error": f"File not found: {basename}"}), 404
try:
with open(path, 'r') as f:
return jsonify(json.load(f))
except (json.JSONDecodeError, IOError) as e:
return jsonify({"error": str(e)}), 500
# --- History (Undo/Redo) ---
@app.route('/history/push', methods=['POST'])
def history_push():
"""
Pushes a new state onto the history stack.
Truncates any redo-forward states (new action clears redo).
Caps total history at MAX_HISTORY entries (circular replacement).
"""
scripts = request.json
meta = read_history_meta()
# New action clears redo: everything after current pointer is gone
new_pointer = meta["pointer"] + 1
# Cap at MAX_HISTORY using modulo for circular behavior
file_index = new_pointer % MAX_HISTORY
with open(history_path(file_index), 'w') as f:
json.dump(scripts, f, indent=4)
meta["pointer"] = new_pointer
meta["count"] = min(new_pointer + 1, MAX_HISTORY)
write_history_meta(meta)
return jsonify({"status": "success", "pointer": new_pointer, "count": meta["count"]})
@app.route('/history/undo', methods=['GET'])
def history_undo():
"""Returns the state one step back from the current pointer."""
meta = read_history_meta()
pointer = meta["pointer"]
if pointer <= 0:
return jsonify({"error": "Nothing to undo", "at_start": True}), 400
target = pointer - 1
file_index = target % MAX_HISTORY
path = history_path(file_index)
if not os.path.exists(path):
return jsonify({"error": "History file missing"}), 500
with open(path, 'r') as f:
state = json.load(f)
meta["pointer"] = target
write_history_meta(meta)
return jsonify({"state": state, "pointer": target, "count": meta["count"]})
@app.route('/history/redo', methods=['GET'])
def history_redo():
"""Returns the state one step forward from the current pointer."""
meta = read_history_meta()
pointer = meta["pointer"]
count = meta["count"]
if pointer >= count - 1:
return jsonify({"error": "Nothing to redo", "at_end": True}), 400
target = pointer + 1
file_index = target % MAX_HISTORY
path = history_path(file_index)
if not os.path.exists(path):
return jsonify({"error": "History file missing"}), 500
with open(path, 'r') as f:
state = json.load(f)
meta["pointer"] = target
write_history_meta(meta)
return jsonify({"state": state, "pointer": target, "count": meta["count"]})
@app.route('/history/status', methods=['GET'])
def history_status():
"""Returns current undo/redo availability."""
meta = read_history_meta()
return jsonify({
"can_undo": meta["pointer"] > 0,
"can_redo": meta["pointer"] < meta["count"] - 1,
"pointer": meta["pointer"],
"count": meta["count"]
})
# --- Plugins & Execution ---
@app.route('/list_plugins', methods=['GET'])
def list_plugins():
return jsonify(list(get_plugin_functions().keys()))
@app.route('/execute', methods=['POST'])
def execute_scripts():
data = request.json
filename = data.get('filename')
scripts = data.get('scripts', [])
workspace = get_config()['workspace']
target_path = os.path.join(workspace, filename)
if not os.path.exists(target_path):
return jsonify({"error": f"File not found: {target_path}"}), 404
try:
with open(target_path, 'r') as f:
lines = f.readlines()
available_plugins = get_plugin_functions()
for step in scripts:
key = step.get('pluginKey')
if key in available_plugins:
lines = available_plugins[key](lines)
with open(target_path, 'w') as f:
f.writelines(lines)
return jsonify({"status": "success", "message": "File processed successfully"})
except Exception as e:
return jsonify({"error": str(e), "trace": traceback.format_exc()}), 500
def get_plugin_functions():
"""Dynamically loads Python functions from the plugins folder."""
plugins = {}
if not os.path.exists(PLUGIN_DIR):
return plugins
for filename in os.listdir(PLUGIN_DIR):
if filename.endswith('.py') and filename != '__init__.py':
module_name = filename[:-3]
path = os.path.join(PLUGIN_DIR, filename)
try:
spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for name, func in inspect.getmembers(module, inspect.isfunction):
plugins[f"{module_name}.{name}"] = func
except Exception as e:
print(f"Failed to load plugin {filename}: {e}")
return plugins
if __name__ == '__main__':
app.run(debug=True, port=5000)