I'm working on a program that would allow me to control multiple browsers through command line interfaces, performing substitutions on input fields. It needs to be able to save substitution history ('payload') and eventually redo it some time later. What do you think of this structure?
class CommandLine:
def __init__(self):
self.browser = Browser()
self.browser.navigate_to('target page')
#<--- simple commands --->
def _clear(self):
pass
def _save(self):
pass
def _show_history(self):
pass
def _load(self):
pass
#<--- commands with arguments --->
def _substitute_regex(self, cmd):
# cmd == 'r [^ ]* [^ ]*'
pattern, _, replacement = cmd[2:].partition(' ')
pass
def _substitute(self, cmd)
pass
def goto(self, cmd):
pass
#<--- interpreter --->
def execute(self, cmd):
for command, func in CommandLine.commands:
if cmd == command:
func(self)
return
for pattern, func in CommandLine.commands_withargs:
if re.search(pattern, cmd):
func(self, cmd)
return
commands = [
('clear', _clear),
('save', _save),
('show history', _show_history),
('load', _load),
# ...
]
commands_withargs = [
('r [^ ]* [^ ]*', _substitute_regex),
('s [^ ]* [^ ]*', _substitute),
('goto [0-9]*', _goto),
# ...
]
first_interface = CommandLine()
second_interface = CommandLine()
first_interface.execute('load')
second_interface.execute('load')
second_interface.execute('r [A-Z][a-z]*[0-9] new_value')
# ...