Skip to content

Commit 3a1a983

Browse files
committed
support file watching
1 parent 03d350e commit 3a1a983

File tree

9 files changed

+98
-23
lines changed

9 files changed

+98
-23
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
PY = python
44
SHELL = powershell.exe
5-
override RARG += --wdir ../temp/debug
5+
override RARG += --dir ../temp/debug
66

77
build :
88
$(PY) setup.py sdist bdist_wheel

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Use command `ecr` to run edl-cr.
3030

3131
|Option|Description|
3232
|-|-|
33-
|`-w --wdir`|Set working directory|
33+
|`-d --dir`|Set working directory|
3434
|`-c --command`|Execute command just like in interactive mode|
3535

3636
## Interactive Mode
@@ -84,6 +84,9 @@ a.cpp> run b.cpp
8484

8585
# run a.cpp with file input and standard output
8686
a.cpp> run -io fs
87+
88+
# watch the file a.cpp and run auto
89+
a.cpp> run -w
8790
```
8891

8992
### Input and Output
@@ -125,7 +128,7 @@ These are builtin commands. You can use system command in `importedCommand` list
125128
|`new [file] [-e --edit]`|Create new code file|
126129
|`now [file]`|Change current file|
127130
|`edit [file] [-n --now]`|Edit code file|
128-
|`run [file] [-io --io]`|Run code file|
131+
|`run [file] [-io --io] [-w]`|Run code file|
129132
|`clean`|Clean temp files|
130133
|`pwd`|Print working directory|
131134
|`cd`|Change working directory|

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ twine
66
pytest-html
77
prompt_toolkit
88
click
9+
watchdog

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ def read(*parts):
1515

1616
setup(
1717
name="edl-cr",
18-
version="0.0.1.5",
18+
version="0.0.2",
1919
description="A CLI tool to run code",
2020
long_description=long_description,
2121
long_description_content_type="text/markdown",
2222

2323
license="Apache-2.0",
2424
classifiers=[
25-
"Development Status :: 2 - Pre-Alpha",
25+
"Development Status :: 3 - Alpha",
2626
"Programming Language :: Python :: 3.7",
2727
"License :: OSI Approved :: Apache Software License",
2828
],
@@ -53,6 +53,7 @@ def read(*parts):
5353
python_requires='>=3.7',
5454
install_requires=[
5555
"prompt_toolkit>=2.0.7",
56-
"click>=7.0"
56+
"click>=7.0",
57+
"watchdog>=0.9.0"
5758
]
5859
)

src/ecr/__main__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def getITParser():
6262
default=None, help="Change input and output")
6363
cmd_run.add_argument(
6464
"file", nargs="?", default=None, help="File name (only for this command)")
65+
cmd_run.add_argument("-w", "--watch", action="store_true",default=False,help="Watch the file and run auto till Ctrl-C")
6566
cmd_run.set_defaults(func=run)
6667

6768
cmd_edit = subpars.add_parser("edit", help="Edit code file")
@@ -174,15 +175,15 @@ def main(): # pragma: no cover
174175
prog="ecr", description="Code Runner")
175176
baseParser.add_argument("-v", "--version", default=False, action="store_true",
176177
help="Get version")
177-
baseParser.add_argument("-w", "--wdir", default=None,
178+
baseParser.add_argument("-d", "--dir", default=None,
178179
help="Set working directory")
179180
baseParser.add_argument(
180181
"-c", "--command", default=None, help="Execute command")
181182
baseCmd = baseParser.parse_args()
182183
if baseCmd.version:
183184
return getVersion(None).value
184-
if baseCmd.wdir != None:
185-
os.chdir(baseCmd.wdir)
185+
if baseCmd.dir != None:
186+
os.chdir(baseCmd.dir)
186187

187188
mainInit()
188189

@@ -201,7 +202,7 @@ def main(): # pragma: no cover
201202
break
202203
if executeCommand(oricmd) == ReturnCode.EXIT.value:
203204
break
204-
205+
205206
return 0
206207

207208

src/ecr/command.py

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@
77
import click
88
import threading
99
import time
10+
import watchdog
11+
from watchdog.events import FileSystemEventHandler
1012
from argparse import Namespace
1113
from .helper import loadMan, printHead
1214
from .core import manager
1315
from .ui import SwitchState, color
1416
from . import ReturnCode, shared, ui
1517

1618

19+
def printFileModify(file):
20+
ui.console.write(color.useYellow("M"), file)
21+
22+
23+
def printFileCreate(file):
24+
ui.console.write(color.useGreen("+"), file)
25+
26+
27+
def printFileDelete(file):
28+
ui.console.write(color.useRed("-"), file)
29+
30+
1731
def assertInited()->bool:
1832
if shared.man == None:
1933
ui.console.error("Not have any ecr directory")
@@ -53,7 +67,7 @@ def new(args):
5367
result = shared.man.newCode(file)
5468
if result:
5569
shared.man.currentFile = file
56-
ui.console.write(color.useGreen("+"), file)
70+
printFileCreate(file)
5771
if args.edit:
5872
return edit(Namespace(file=file, now=False))
5973
return ReturnCode.OK
@@ -73,7 +87,7 @@ def edit(args):
7387
file = shared.man.currentFile
7488
result = shared.man.edit(file)
7589
if result:
76-
ui.console.write(color.useYellow("M"), file)
90+
printFileModify(file)
7791
if args.now:
7892
return now(Namespace(file=file))
7993
return ReturnCode.OK
@@ -83,6 +97,34 @@ def edit(args):
8397
return ReturnCode.OK
8498

8599

100+
class RunWatchEventHandler(FileSystemEventHandler):
101+
def __init__(self, file, func):
102+
self.func = func
103+
self.file = file
104+
self.state = False
105+
106+
def on_moved(self, event):
107+
super().on_moved(event)
108+
# self.func()
109+
110+
# what = 'directory' if event.is_directory else 'file'
111+
# logging.info("Moved %s: from %s to %s", what, event.src_path,event.dest_path)
112+
113+
def on_created(self, event):
114+
super().on_created(event)
115+
116+
def on_deleted(self, event):
117+
super().on_deleted(event)
118+
# self.func()
119+
120+
def on_modified(self, event):
121+
super().on_modified(event)
122+
if os.path.split(event.src_path)[-1] == self.file:
123+
self.state = not self.state
124+
if self.state: # one modify raise two event
125+
self.func()
126+
127+
86128
def run(args):
87129
if not assertInited():
88130
return ReturnCode.UNLOADED
@@ -116,18 +158,44 @@ def run(args):
116158
117159
result = ret[0] if len(ret) > 0 else False"""
118160

119-
result = shared.man.execute(io=args.io, file=args.file)
161+
if not args.watch:
162+
result = shared.man.execute(io=args.io, file=args.file)
120163

121-
if not result:
122-
ui.console.error("Running failed")
123-
return ReturnCode.RUNERR
124-
return ReturnCode.OK
164+
if not result:
165+
ui.console.error("Running failed")
166+
return ReturnCode.RUNERR
167+
return ReturnCode.OK
168+
else:
169+
def func():
170+
ui.console.clear()
171+
ui.console.info(f"Watching", end=" ")
172+
printFileModify(file)
173+
result = shared.man.execute(io=args.io, file=args.file)
174+
if not result:
175+
ui.console.error("Running failed")
176+
177+
from watchdog.observers import Observer
178+
file = args.file if args.file != None else shared.man.currentFile
179+
path = shared.man.workingDirectory
180+
ui.console.info(f"Watching {file} (press ctrl+c to end)")
181+
event_handler = RunWatchEventHandler(file, func)
182+
observer = Observer()
183+
observer.schedule(event_handler, path, recursive=False)
184+
observer.start()
185+
try:
186+
while True:
187+
time.sleep(1)
188+
except KeyboardInterrupt:
189+
observer.stop()
190+
ui.console.info("Watching end.")
191+
observer.join()
192+
return ReturnCode.OK
125193

126194

127195
def clean(args):
128196
if not assertInited():
129197
return ReturnCode.UNLOADED
130-
shared.man.clean()
198+
shared.man.clean(rmHandler=printFileDelete)
131199
return ReturnCode.OK
132200

133201

src/ecr/core/defaultData.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from . import CIO_SISO
22

33
io = CIO_SISO
4-
timeLimit = 5
4+
timeLimit = 10
55
editor = "vim"
66

77
executors = {

src/ecr/core/manager.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,14 @@ def edit(self, file=None):
157157
except:
158158
return False
159159

160-
def clean(self):
160+
def clean(self, rmHandler=None):
161161
for file in os.listdir(self.workingDirectory):
162162
for pat in self.tempFileFilter:
163163
try:
164164
if pat == path.getFileExt(os.path.split(file)[-1]):
165-
os.remove(file)
166-
ui.console.write(color.useRed("-"), file)
165+
os.remove(os.path.join(self.workingDirectory, file))
166+
if rmHandler != None:
167+
rmHandler(file)
167168
break
168169
except:
169170
pass

src/ecr/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .core import WorkManager, path
22
from .ui import CLI
33

4-
version = "0.0.1.5"
4+
version = "0.0.2"
55

66
cwd = None
77

0 commit comments

Comments
 (0)