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
322
323
|
import os
import time
from typing import cast, Tuple
from argparse import Namespace
from watchdog.events import FileSystemEventHandler
from .helper import loadMan, printHead
from .core import manager, WorkManager, WorkItem, WorkItemType
from .ui import SwitchState, color
from . import ReturnCode, shared, ui
__all__ = ["init", "new", "now", "pwd", "cd", "clear",
"getVersion", "shutdown", "run", "clean", "cls", "edit", "debug", "judge"]
cmds = ["init", "new", "now", "pwd", "cd", "clear",
"version", "run", "clean", "cls", "exit", "edit", "debug", "judge"]
def printFileModify(file: str)->None:
ui.console.write(color.useYellow("M"), file)
def printFileCreate(file: str)->None:
ui.console.write(color.useGreen("+"), file)
def printFileDelete(file: str)->None:
ui.console.write(color.useRed("-"), file)
def assertInited()->bool:
if not shared.man:
ui.console.error("Not have any ecr directory")
return False
return True
def init(args: Namespace)->ReturnCode: # pylint: disable=W0613
manager.initialize(cast(str, shared.cwd))
loadMan()
printHead()
return ReturnCode.OK if shared.man else ReturnCode.UNLOADED
def clear(args: Namespace)->ReturnCode: # pylint: disable=W0613
if not assertInited():
return ReturnCode.UNLOADED
tman: WorkManager = cast(WorkManager, shared.man)
if ui.console.confirm("Do you want to clear ALL?",
[SwitchState.OK, SwitchState.Cancel]) == SwitchState.OK:
manager.clear(tman.workingDirectory)
shared.man = None
return ReturnCode.OK
def now(args: Namespace)->ReturnCode:
if not assertInited():
return ReturnCode.UNLOADED
tman: WorkManager = cast(WorkManager, shared.man)
tman.setCurrent(args.file, args.dir)
return ReturnCode.OK
def new(args: Namespace)->ReturnCode:
if not assertInited():
return ReturnCode.UNLOADED
tman: WorkManager = cast(WorkManager, shared.man)
if not args.file and not tman.currentFile:
ui.console.write("Please set file first")
return ReturnCode.ERROR
file = args.file if args.file else cast(WorkItem, tman.currentFile).name
result = tman.newCode(tman.getWorkItem(
args.file, args.dir) if args.file else None)
if result:
tman.currentFile = result
printFileCreate(file)
if args.edit:
return edit(Namespace(file=file, now=False))
return ReturnCode.OK
else:
ui.console.error(f"Can't create file {file}")
return ReturnCode.ERROR
def edit(args: Namespace)->ReturnCode:
if not assertInited():
return ReturnCode.UNLOADED
tman: WorkManager = cast(WorkManager, shared.man)
if not args.file and not tman.currentFile:
ui.console.write("Please set file first")
return ReturnCode.ERROR
if not args.file and not tman.currentFile:
ui.console.write("Please set file first")
return ReturnCode.ERROR
file = args.file if args.file else cast(WorkItem, tman.currentFile).name
result = tman.edit(tman.getWorkItem(
args.file, args.dir) if args.file else None)
if result:
printFileModify(file)
if args.now:
return now(Namespace(file=file))
return ReturnCode.OK
else:
ui.console.error(f"Editing file error {file}")
return ReturnCode.ERROR
return ReturnCode.OK
class RunWatchEventHandler(FileSystemEventHandler):
def __init__(self, file, func):
self.func = func
self.file = file
self.state = False
def on_moved(self, event): # pylint: disable=W0235
super().on_moved(event)
# self.func()
# what = 'directory' if event.is_directory else 'file'
# logging.info("Moved %s: from %s to %s", what, event.src_path,event.dest_path)
def on_created(self, event): # pylint: disable=W0235
super().on_created(event)
def on_deleted(self, event): # pylint: disable=W0235
super().on_deleted(event)
# self.func()
def on_modified(self, event):
super().on_modified(event)
if not self.file or os.path.split(event.src_path)[-1] == self.file:
self.state = not self.state
if self.state: # one modify raise two event
self.func()
def getItem(tman: WorkManager, args: Namespace)->Tuple[WorkItem, str]:
if args.file:
item = tman.getWorkItem(
args.file, args.dir)
else:
item = tman.getWorkItem(
cast(WorkItem, tman.currentFile).name, cast(WorkItem, tman.currentFile).type == WorkItemType.Directory)
tman.currentFile = item
file = cast(WorkItem, item).name
return cast(WorkItem, item), file
def run(args: Namespace)->ReturnCode:
if not assertInited():
return ReturnCode.UNLOADED
tman: WorkManager = cast(WorkManager, shared.man)
if not args.file and not tman.currentFile:
ui.console.write("Please set file first")
return ReturnCode.ERROR
# pylint: disable=W0105
"""def func(ret): # for async
try:
ret.append(tman.execute(io=args.io, file=args.file))
except:
ret.append(False)
ret = []
new_thread = threading.Thread(target=func, args=(ret,))
new_thread.start()
while not tman.runner:
pass
while tman.runner != None and tman.runner.isRunning:
cmd = ui.console.read()
if cmd == "kill":
ui.console.write(cmd)
tman.runner.terminate()
new_thread.join()
elif tman.runner.canInput:
tman.runner.input(cmd)
result = ret[0] if len(ret) > 0 else False """
if not args.watch:
item, file = getItem(tman,args)
result = tman.execute(io=args.io, item=item)
if not result:
ui.console.error("Running failed")
return ReturnCode.RUNERR
return ReturnCode.OK
else:
file = args.file if args.file else cast(
WorkItem, tman.currentFile).name
def func():
item, file = getItem(tman,args)
ui.console.clear()
ui.console.info(f"Watching", end=" ")
printFileModify(file)
result = tman.execute(io=args.io, item=item)
if not result:
ui.console.error("Running failed")
from watchdog.observers import Observer
ui.console.info(f"Watching {file} (press ctrl+c to end)")
path = tman.workingDirectory if item.type == WorkItemType.File else item.path
event_handler = RunWatchEventHandler(
file if item.type == WorkItemType.File else None, func)
observer = Observer()
observer.schedule(event_handler, path, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
ui.console.info("Watching end.")
observer.join()
return ReturnCode.OK
def judge(args: Namespace)->ReturnCode:
if not assertInited():
return ReturnCode.UNLOADED
tman: WorkManager = cast(WorkManager, shared.man)
if not args.file and not tman.currentFile:
ui.console.write("Please set file first")
return ReturnCode.ERROR
if not args.watch:
item, file = getItem(tman,args)
result = tman.judge(reexecute=args.re,
item=item, judger=args.judger)
if not result:
ui.console.error("Judging failed")
return ReturnCode.JUDGEERR
else:
ui.console.ok("Judging passed")
return ReturnCode.OK
else:
file = args.file if args.file else cast(
WorkItem, tman.currentFile).name
def func():
item, file = getItem(tman,args)
ui.console.clear()
ui.console.info(f"Watching", end=" ")
printFileModify(file)
result = tman.judge(reexecute=args.re,
item=item, judger=args.judger)
if not result:
ui.console.error("Judging failed")
return ReturnCode.JUDGEERR
else:
ui.console.ok("Judging passed")
return ReturnCode.OK
from watchdog.observers import Observer
ui.console.info(f"Watching {file} (press ctrl+c to end)")
path = tman.workingDirectory if item.type == WorkItemType.File else item.path
event_handler = RunWatchEventHandler(
file if item.type == WorkItemType.File else None, func)
observer = Observer()
observer.schedule(event_handler, path, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
ui.console.info("Watching end.")
observer.join()
return ReturnCode.OK
def clean(args: Namespace)->ReturnCode: # pylint: disable=W0613
if not assertInited():
return ReturnCode.UNLOADED
tman: WorkManager = cast(WorkManager, shared.man)
tman.clean(rmHandler=printFileDelete)
return ReturnCode.OK
def shutdown(args: Namespace)->ReturnCode: # pylint: disable=W0613
return ReturnCode.EXIT
def pwd(args: Namespace)->ReturnCode: # pylint: disable=W0613
ui.console.write(shared.cwd)
return ReturnCode.OK
def getVersion(args: Namespace)->ReturnCode: # pylint: disable=W0613
ui.console.write("edl-cr", shared.version)
ui.console.write("Copyright (C) eXceediDeal")
ui.console.write(
"License Apache-2.0, Source https://github.com/eXceediDeaL/edl-coderunner")
return ReturnCode.OK
def cd(args: Namespace)->ReturnCode:
if not os.path.exists(args.path):
ui.console.error("No this directory")
return ReturnCode.ERROR
os.chdir(args.path)
shared.cwd = os.getcwd()
loadMan()
printHead()
return ReturnCode.OK
def cls(args: Namespace)->ReturnCode: # pylint: disable=W0613
ui.console.clear()
return ReturnCode.OK
def debug(args: Namespace) -> ReturnCode: # pylint: disable=W0613
import json
ui.console.write(json.dumps(
cast(WorkManager, shared.man).__dict__, default=str, indent=4))
return ReturnCode.OK
|