Skip to content

Commit c58a36c

Browse files
authored
Merge pull request #27 from rocky/command-setup-DRY
Command setup dry
2 parents 232cca6 + ba496e7 commit c58a36c

25 files changed

Lines changed: 447 additions & 440 deletions

__pkginfo__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
install_requires = [
7272
"columnize >= 0.3.10",
7373
"nose>=1.0.0, <= 1.3.7",
74-
"pyficache >= 2.0.1",
74+
"pyficache >= 2.0.1, < 2.1.0",
7575
"pygments %s" % pygments_version,
7676
"spark_parser >= 1.8.9, <1.9.0",
7777
"tracer >= 0.3.2",

trepan/processor/cmdproc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ def populate_commands_pip(self, Mcommand):
10251025
pass
10261026
return cmd_instances
10271027

1028+
# This is the most-used way of adding commands
10281029
def populate_commands_easy_install(self, Mcommand):
10291030
"""
10301031
Add files in filesystem to self.commands.
Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2009, 2013, 2015, 2018-2019 Rocky Bernstein
2+
# Copyright (C) 2009, 2013, 2015, 2018-2020 Rocky Bernstein
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
@@ -13,15 +13,15 @@
1313
#
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
import os
16+
1717
from getopt import getopt, GetoptError
1818

1919
# Our local modules
20-
from trepan.processor.command import base_cmd as Mbase_cmd
20+
from trepan.processor.command.base_cmd import DebuggerCommand
2121
from trepan.lib import stack as Mstack
2222

2323

24-
class BacktraceCommand(Mbase_cmd.DebuggerCommand):
24+
class BacktraceCommand(DebuggerCommand):
2525
"""**backtrace** [*options*] [*count*]
2626
2727
Print backtrace of all stack frames, or innermost *count* frames.
@@ -57,107 +57,111 @@ class BacktraceCommand(Mbase_cmd.DebuggerCommand):
5757
`frame`, `locals`, `global`, `deparse`, `list`.
5858
"""
5959

60-
aliases = ('bt', 'where')
61-
category = 'stack'
62-
min_args = 0
63-
max_args = 4
64-
name = os.path.basename(__file__).split('.')[0]
65-
need_stack = True
66-
short_help = 'Print backtrace of stack frames'
60+
aliases = ("bt", "where")
61+
short_help = "Print backtrace of stack frames"
62+
63+
DebuggerCommand.setup(locals(), category="stack", max_args=4, need_stack=True)
6764

6865
def run(self, args):
6966

7067
try:
71-
opts, args = getopt(args[1:], "hfds",
72-
"help deparse full source".split())
68+
opts, args = getopt(args[1:], "hfds", "help deparse full source".split())
7369
except GetoptError as err:
7470
# print help information and exit:
7571
print(str(err)) # will print something like "option -a not recognized"
7672
return
7773

78-
bt_opts = {'width': self.settings['width']}
74+
bt_opts = {"width": self.settings["width"]}
7975
for o, a in opts:
8076
if o in ("-h", "--help"):
81-
self.proc.commands['help'].run(['help', 'backtrace'])
77+
self.proc.commands["help"].run(["help", "backtrace"])
8278
return
8379
elif o in ("-d", "--deparse"):
84-
bt_opts['deparse'] = True
80+
bt_opts["deparse"] = True
8581
elif o in ("-f", "--full"):
86-
bt_opts['full'] = True
82+
bt_opts["full"] = True
8783
elif o in ("-s", "--source"):
88-
bt_opts['source'] = True
84+
bt_opts["source"] = True
8985
else:
9086
self.errmsg("unhandled option '%s'" % o)
9187
pass
9288

93-
9489
if len(args) > 0:
9590
at_most = len(self.proc.stack)
9691
if at_most == 0:
9792
self.errmsg("Stack is empty.")
9893
return False
99-
min_value = - at_most + 1
100-
count = self.proc.get_int(args[0], min_value = min_value,
101-
cmdname = 'backtrace',
102-
default=0, at_most = at_most)
103-
if count is None: return False
94+
min_value = -at_most + 1
95+
count = self.proc.get_int(
96+
args[0],
97+
min_value=min_value,
98+
cmdname="backtrace",
99+
default=0,
100+
at_most=at_most,
101+
)
102+
if count is None:
103+
return False
104104
if count < 0:
105-
count = at_most - count
105+
count = at_most - count
106106
pass
107-
elif 0 == count: count = None
107+
elif 0 == count:
108+
count = None
108109
else:
109110
count = None
110111
pass
111112

112113
if not self.proc.curframe:
113114
self.errmsg("No stack.")
114115
return False
115-
Mstack.print_stack_trace(self.proc, count,
116-
color=self.settings['highlight'],
117-
opts=bt_opts)
116+
Mstack.print_stack_trace(
117+
self.proc, count, color=self.settings["highlight"], opts=bt_opts
118+
)
118119
return False
119120

120121
pass
121122

122-
if __name__ == '__main__':
123+
124+
if __name__ == "__main__":
123125
from trepan.processor import cmdproc
124126
from trepan import debugger
125-
d = debugger.Debugger()
126-
cp = d.core.processor
127-
command = BacktraceCommand(cp)
128-
command.run(['backtrace', 'wrong', 'number', 'of', 'args'])
127+
128+
d = debugger.Debugger()
129+
cp = d.core.processor
130+
command = BacktraceCommand(cp)
131+
command.run(["backtrace", "wrong", "number", "of", "args"])
129132

130133
def nest_me(cp, command, i):
131134
import inspect
135+
132136
if i > 1:
133137
cp.curframe = inspect.currentframe()
134-
cp.stack, cp.curindex = cmdproc.get_stack(cp.curframe, None, None,
135-
cp)
136-
print('-' * 10)
137-
command.run(['backtrace'])
138-
print('-' * 10)
139-
command.run(['backtrace', '1'])
138+
cp.stack, cp.curindex = cmdproc.get_stack(cp.curframe, None, None, cp)
139+
print("-" * 10)
140+
command.run(["backtrace"])
141+
print("-" * 10)
142+
command.run(["backtrace", "1"])
140143
else:
141-
nest_me(cp, command, i+1)
144+
nest_me(cp, command, i + 1)
142145
return
143146

144147
def ignore_me(cp, command, i):
145-
print('=' * 10)
148+
print("=" * 10)
146149
nest_me(cp, command, 1)
147-
print('=' * 10)
150+
print("=" * 10)
148151
cp.core.add_ignore(ignore_me)
149152
nest_me(cp, command, 1)
150153
return
154+
151155
cp.forget()
152-
command.run(['backtrace'])
153-
print('-' * 10)
156+
command.run(["backtrace"])
157+
print("-" * 10)
154158
ignore_me(cp, command, 1)
155-
command.run(['backtrace', '1'])
156-
print('-' * 10)
157-
command.run(['backtrace', '-1'])
158-
print('-' * 10)
159-
command.run(['backtrace', '3'])
160-
print('-' * 10)
161-
command.run(['backtrace', '-2'])
162-
print('-' * 10)
159+
command.run(["backtrace", "1"])
160+
print("-" * 10)
161+
command.run(["backtrace", "-1"])
162+
print("-" * 10)
163+
command.run(["backtrace", "3"])
164+
print("-" * 10)
165+
command.run(["backtrace", "-2"])
166+
print("-" * 10)
163167
pass

trepan/processor/command/base_cmd.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class DebuggerCommand:
3636

3737
category = 'misc'
3838

39+
@staticmethod
40+
def setup(l, category="misc", min_args=0, max_args=None,
41+
need_stack=False):
42+
l["name"] =l["__module__"].split(".")[-1]
43+
l["category"] = category
44+
l["min_args"] = min_args
45+
l["max_args"] = max_args
46+
l["need_stack"] = need_stack
47+
return
48+
3949
def __init__(self, proc):
4050
"""proc contains the command processor object that this
4151
command is invoked through. A debugger field gives access to

trepan/processor/command/base_submgr.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
import inspect, re, sys, importlib
1717

18-
from trepan.processor.command import base_cmd as Mbase_cmd
19-
from trepan.processor import subcmd as Msubcmd
20-
from trepan.lib import complete as Mcomplete
18+
from trepan.processor.command.base_cmd import DebuggerCommand
19+
from trepan.processor.subcmd import Subcmd
20+
from trepan.lib.complete import complete_token, complete_token_with_next
2121

2222

2323
def abbrev_stringify(name, min_abbrev):
@@ -33,7 +33,7 @@ def capitalize(s):
3333
pass
3434

3535

36-
class SubcommandMgr(Mbase_cmd.DebuggerCommand):
36+
class SubcommandMgr(DebuggerCommand):
3737

3838
category = "status"
3939
min_args = 0
@@ -46,14 +46,14 @@ def __init__(self, proc, name=None):
4646
has to be setcmds ('set' + 'cmds') for subcommand completion
4747
to work."""
4848

49-
Mbase_cmd.DebuggerCommand.__init__(self, proc)
49+
DebuggerCommand.__init__(self, proc)
5050

5151
# Name is set in testing
5252
if name is None:
5353
name = self.__module__.split(".")[-1]
5454
self.__class__.name = name
5555

56-
self.cmds = Msubcmd.Subcmd(name, self)
56+
self.cmds = Subcmd(name, self)
5757
self.name = name
5858
self._load_debugger_subcommands(name)
5959
self.proc = proc
@@ -183,11 +183,11 @@ def help(self, args):
183183
# found we just return +arg+.
184184
# FIXME: Not used any more?
185185
def complete(self, prefix):
186-
return Mcomplete.complete_token(self.subcmds.subcmds.keys(), prefix)
186+
return complete_token(self.subcmds.subcmds.keys(), prefix)
187187

188188
def complete_token_with_next(self, prefix):
189189
# from trepan.api import debug; debug()
190-
result = Mcomplete.complete_token_with_next(self.cmds.subcmds, prefix)
190+
result = complete_token_with_next(self.cmds.subcmds, prefix)
191191
return result
192192

193193
def run(self, args):

trepan/processor/command/break.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2009-2010, 2013-2015, 2017-2018 Rocky Bernstein
3+
# Copyright (C) 2009-2010, 2013-2015, 2017-2018, 2020 Rocky Bernstein
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
66
# the Free Software Foundation, either version 3 of the License, or
@@ -13,15 +13,14 @@
1313
#
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
import os
1716

1817
# Our local modules
19-
from trepan.processor.command import base_cmd as Mbase_cmd
18+
from trepan.processor.command.base_cmd import DebuggerCommand
2019
from trepan.processor import cmdbreak as Mcmdbreak
21-
from trepan.processor import complete as Mcomplete
20+
from trepan.processor.complete import complete_break_linenumber
2221

2322

24-
class BreakCommand(Mbase_cmd.DebuggerCommand):
23+
class BreakCommand(DebuggerCommand):
2524
"""**break** [*location*] [if *condition*]]
2625
2726
Sets a breakpoint, i.e. stopping point just before the
@@ -64,54 +63,55 @@ class BreakCommand(Mbase_cmd.DebuggerCommand):
6463
6564
`info break`, `tbreak`, `condition` and `help syntax location`."""
6665

67-
aliases = ('b', 'break!', 'b!')
68-
category = 'breakpoints'
69-
min_args = 0
70-
max_args = None
71-
name = os.path.basename(__file__).split('.')[0]
72-
need_stack = True
73-
short_help = 'Set breakpoint at specified line or function'
66+
aliases = ("b", "break!", "b!")
67+
short_help = "Set breakpoint at specified line or function"
7468

75-
complete= Mcomplete.complete_break_linenumber
69+
DebuggerCommand.setup(locals(), category="breakpoints", need_stack=True)
70+
71+
complete = complete_break_linenumber
7672

7773
def run(self, args):
78-
force = True if args[0][-1] == '!' else False
74+
force = True if args[0][-1] == "!" else False
7975

80-
(func, filename, lineno,
81-
condition) = Mcmdbreak.parse_break_cmd(self.proc, args)
76+
(func, filename, lineno, condition) = Mcmdbreak.parse_break_cmd(self.proc, args)
8277
if not (func == None and filename == None):
83-
Mcmdbreak.set_break(self, func, filename, lineno, condition,
84-
False, args, force=force)
78+
Mcmdbreak.set_break(
79+
self, func, filename, lineno, condition, False, args, force=force
80+
)
8581
return
8682

87-
if __name__ == '__main__':
83+
84+
if __name__ == "__main__":
85+
8886
def doit(cmd, a):
89-
cmd.current_command = ' '.join(a)
87+
cmd.current_command = " ".join(a)
9088
print(Mcmdbreak.parse_break_cmd(cmd.proc, a))
9189

9290
import sys
9391
from trepan import debugger as Mdebugger
92+
9493
d = Mdebugger.Trepan()
9594
command = BreakCommand(d.core.processor)
9695
command.proc.frame = sys._getframe()
9796
command.proc.setup()
9897

99-
doit(command, [' '])
100-
doit(command, ['10'])
101-
doit(command, [__file__ + ':10'])
98+
doit(command, [" "])
99+
doit(command, ["10"])
100+
doit(command, [__file__ + ":10"])
102101

103102
def foo():
104-
return 'bar'
105-
doit(command, ['foo'])
106-
doit(command, ['os.path'])
107-
doit(command, ['os.path', '5+1'])
108-
doit(command, ['os.path.join'])
109-
doit(command, ['if', 'True'])
110-
doit(command, ['foo', 'if', 'True'])
111-
doit(command, ['os.path:10', 'if', 'True'])
112-
command.run(['break'])
113-
command.run(['break', 'command.run'])
114-
command.run(['break', '10'])
115-
command.run(['break', __file__ + ':10'])
116-
command.run(['break', 'foo'])
103+
return "bar"
104+
105+
doit(command, ["foo"])
106+
doit(command, ["os.path"])
107+
doit(command, ["os.path", "5+1"])
108+
doit(command, ["os.path.join"])
109+
doit(command, ["if", "True"])
110+
doit(command, ["foo", "if", "True"])
111+
doit(command, ["os.path:10", "if", "True"])
112+
command.run(["break"])
113+
command.run(["break", "command.run"])
114+
command.run(["break", "10"])
115+
command.run(["break", __file__ + ":10"])
116+
command.run(["break", "foo"])
117117
pass

0 commit comments

Comments
 (0)