Skip to content

Commit

Permalink
Update console scripts so it can run scripts without code editing
Browse files Browse the repository at this point in the history
Also match "up" and "down" as pdb
  • Loading branch information
gaogaotiantian committed Sep 7, 2020
1 parent 18b46e5 commit 631e6db
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 20 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ pip install wthell

## Usage

It's super easy to use wthell. Just import it and it's done!
It's super easy to use wthell. Just run you script using wthell instead of python

```
wthell your_script.py args1 args2
```

Or you can import wthell in your script and run your script normally

```python
import wthell
Expand All @@ -29,7 +35,7 @@ If there's an uncaught exception, you will enter an interactive shell like this:

Exception raised: <class 'Exception'> lol

back(b) -- go to outer frame | in(i) -- go to inner frame
up(u) -- go to outer frame | down(d) -- go to inner frame
clear(cl) -- clear the console | reset(r) -- back to trigger frame
continue(c) -- resume the program | ctrl+D -- quit

Expand All @@ -38,8 +44,8 @@ continue(c) -- resume the program | ctrl+D -- quit

You will be in the frame(function) that raised exceptions in the beginning.

* Type ```back``` to go to outer frame(its caller).
* Type ```in``` to go to inner frame(when you already go out).
* Type ```up``` to go to outer frame(its caller).
* Type ```down``` to go to inner frame(when you already go out).
* Type ```clear``` to clear the console prints
* Type ```reset``` to go back to the original frame that triggered wthell
* Type ```continue``` to resume the program
Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent"
],
entry_points={
"console_scripts": {
"wthell = wthell:main"
}
}
)
3 changes: 2 additions & 1 deletion src/wthell/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sys
from .wthell import WTHell
from .main import main


__version__ = "0.1.0"
__version__ = "0.1.1"

if "wth" not in locals():
wth = WTHell()
Expand Down
5 changes: 5 additions & 0 deletions src/wthell/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .main import main


if __name__ == '__main__':
main()
11 changes: 8 additions & 3 deletions src/wthell/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ def add_code_string(self, frame):
filename = frame.f_code.co_filename
firstlineno = frame.f_code.co_firstlineno

with open(filename, "r") as f:
lst = f.readlines()
try:
with open(filename, "r") as f:
lst = f.readlines()
except NotADirectoryError:
self.code_string = "Source file not available"
return

indent = -1
start = firstlineno
Expand All @@ -36,7 +40,8 @@ def add_code_string(self, frame):
while end < len(lst):
line = lst[end]
stripped_line = line.lstrip()
if len(line) - len(stripped_line) <= indent:
if len(line) - len(stripped_line) <= indent and \
not stripped_line.startswith("#"):
break
end += 1
for idx in range(start, end):
Expand Down
56 changes: 56 additions & 0 deletions src/wthell/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sys
import os
import argparse
import subprocess
import runpy
from .wthell import WTHell


def main():
parser = argparse.ArgumentParser()
parser.add_argument("command", nargs=argparse.REMAINDER)
parser.add_argument("--module", "-m", nargs="?", default=None)
options = parser.parse_args(sys.argv[1:])

if options.command:
command = options.command
else:
print("You need to specify python commands")
exit(1)

if options.module:
code = "run_module(modname, run_name='__main__')"
global_dict = {
"run_module": runpy.run_module,
"modname": options.module
}
sys.argv = [options.module] + command[:]
else:
file_name = command[0]
if not os.path.exists(file_name):
if sys.platform in ["linux", "linux2", "darwin"]:
p = subprocess.Popen(["which", file_name], stdout=subprocess.PIPE)
file_name = p.communicate()[0].decode("utf-8").strip()
if not file_name or not os.path.exists(file_name):
print("No such file as {}".format(file_name))
exit(1)
else:
print("No such file as {}".format(file_name))
exit(1)

code_string = open(file_name).read()
global_dict = {
"__name__": "__main__",
"__file__": file_name,
"__package__": None,
"__cached__": None
}
code = compile(code_string, file_name, "exec")
sys.path.insert(0, os.path.dirname(file_name))
sys.argv = command[:]

wth = WTHell()
wth._sys_excepthook = sys.excepthook
sys.excepthook = wth.excepthook
global_dict["wth"] = wth
exec(code, global_dict)
27 changes: 15 additions & 12 deletions src/wthell/wthell.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ def do_cmd(self, cmd):

args = cmd.split()

if args[0] == "back" or args[0] == "b":
self.do_back(args[1:])
elif args[0] == "in" or args[0] == "i":
self.do_in(args[1:])
if args[0] == "up" or args[0] == "u":
self.do_up(args[1:])
elif args[0] == "down" or args[0] == "d":
self.do_down(args[1:])
elif args[0] == "clear" or args[0] == "cl":
self.do_clear(args[1:])
elif args[0] == "reset" or args[0] == "r":
Expand All @@ -85,17 +85,17 @@ def do_cmd(self, cmd):

return True

def do_back(self, args):
def do_up(self, args):
if self.frame_idx == len(self.frames) - 1:
self.console.print("Already at root, can't go back anymore")
self.console.print("Already at root, can't go up anymore")
else:
self.frame_idx += 1
self.currentframe = self.frames[self.frame_idx]
self.show_console()

def do_in(self, args):
def do_down(self, args):
if self.frame_idx == 0:
self.console.print("Already at stack top, can't go in anymore")
self.console.print("Already at stack top, can't go down anymore")
else:
self.frame_idx -= 1
self.currentframe = self.frames[self.frame_idx]
Expand All @@ -116,9 +116,12 @@ def do_eval(self, s):
def dbg_console(self):
self.show_console()
while True:
cmd = input(">>> ")
if not self.do_cmd(cmd):
break
try:
cmd = input(">>> ")
if not self.do_cmd(cmd):
break
except EOFError:
exit(0)

def show_console(self):
console = self.console
Expand All @@ -133,7 +136,7 @@ def show_console(self):

def print_help(self):
console = self.console
console.print("back(b) -- go to outer frame | in(i) -- go to inner frame")
console.print("up(u) -- go to outer frame | down(d) -- go to inner frame")
console.print("clear(cl) -- clear the console | reset(r) -- back to trigger frame")
console.print("continue(c) -- resume the program | ctrl+D -- quit")
console.print()

0 comments on commit 631e6db

Please sign in to comment.