From 7ef52c68574b54333c44c5be739049deac3a28ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Wed, 10 Oct 2012 18:34:21 -0300 Subject: Data loader --- lib/commandmanager.py | 37 +++++++++++++++++++++++++++++++++ lib/completionmanager.py | 9 ++++++++ lib/exceptions.py | 8 +++++++ lib/outputmanager.py | 41 ++++++++++++++++++++++++++++++++++++ pywhoisd_dataloader.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 lib/commandmanager.py create mode 100644 lib/completionmanager.py create mode 100644 lib/exceptions.py create mode 100644 lib/outputmanager.py create mode 100755 pywhoisd_dataloader.py diff --git a/lib/commandmanager.py b/lib/commandmanager.py new file mode 100644 index 0000000..3a06f99 --- /dev/null +++ b/lib/commandmanager.py @@ -0,0 +1,37 @@ +from lib.exceptions import CmdNotFoundException + +class Command: + def __init__(self): + pass + + def execute(self, loader, params): + pass + + def usage(self, cmd_name): + return cmd_name + +class AdminCommand(Command): + pass + +class NetworkCommand(Command): + pass + +class DomainCommand(Command): + pass + +class CommandManager: + def __init__(self): + self.cmds = { + 'admin' : AdminCommand(), + 'network': NetworkCommand(), + 'domain' : DomainCommand(), + } + + def find(self, cmd): + if cmd in self.cmds: + return self.cmds[cmd] + else: + raise CmdNotFoundException(cmd + 'is not a valid command.') + + def commands(self): + return self.cmds.keys() diff --git a/lib/completionmanager.py b/lib/completionmanager.py new file mode 100644 index 0000000..fe6f099 --- /dev/null +++ b/lib/completionmanager.py @@ -0,0 +1,9 @@ +class CompletionManager: + def __init__(self): + pass + + def generate_commands(self): + pass + + def generate_parameters(self): + pass diff --git a/lib/exceptions.py b/lib/exceptions.py new file mode 100644 index 0000000..63c7475 --- /dev/null +++ b/lib/exceptions.py @@ -0,0 +1,8 @@ +class CmdNotFoundException(Exception): + pass + +class CommandException(Exception): + def __init__(self, message, print_usage=True): + Exception.__init__(self, message) + self.print_usage = print_usage + diff --git a/lib/outputmanager.py b/lib/outputmanager.py new file mode 100644 index 0000000..d9d4af1 --- /dev/null +++ b/lib/outputmanager.py @@ -0,0 +1,41 @@ +#!/usr/bin/python3 +import sys + +class OutputManager: + def __init__(self): + + self.echo_output = True + + def line_break(self): + if self.echo_output: + sys.stdout.write('\n') + sys.stdout.flush() + + return self + + def error(self, string): + if self.echo_output: + self._erase_line() + string = '[-] ' + string + self.last_line_length = len(string) + + sys.stdout.write(string) + sys.stdout.flush() + + return self + + def normal(self, string): + if self.echo_output: + self._erase_line() + self.last_line_length = len(string) + sys.stdout.write(string) + sys.stdout.flush() + + return self + + def _erase_line(self): + sys.stdout.write('\r' + ' ' * self.last_line_length + '\r') + return self + + + diff --git a/pywhoisd_dataloader.py b/pywhoisd_dataloader.py new file mode 100755 index 0000000..7b80ef5 --- /dev/null +++ b/pywhoisd_dataloader.py @@ -0,0 +1,54 @@ +#!/usr/bin/python3 +import signal, sys + +from lib.outputmanager import OutputManager +from lib.commandmanager import CommandManager +from lib.completionmanager import CompletionManager + +from lib.exceptions import CmdNotFoundException, CommandException + +class Manager: + def __init__(self): + self.loader = None + self.completer = CompletionManager() + + def start(self): + try: + signal.signal(signal.SIGINT, signal.default_int_handler) + while True: + try: + try: + line = input('% ') + except KeyboardInterrupt: + output_manager.line_break() + continue + + cmd_name = line.strip().split(' ') + if len(cmd_name) > 0 and len(cmd_name[0]) > 0: + cmd = cmd_manager.find(cmd_name[0]) + cmd.execute(self.loader, line[1:] if len(line) > 1 else []) + + except CommandException as err: + output_manager.error(str(err)).linebreak() + if err.print_usage: + output_manager.normal('Usage: {0}'.format(cmd.usage(line[0]))).line_break() + + except CmdNotFoundException as err: + output_manager.error('Error: {0}'.format(str(err))).linebreak() + + except EOFError: + output_manager.line_break() + exit(0) + except: + raise + +if __name__ == '__main__': + try: + manager = Manager() + cmd_manager = CommandManager() + output_manager = OutputManager() + manager.start() + + except: # TODO: Improve exceptions handling + print("Unexpected error:", sys.exc_info()[0]) + raise -- cgit v1.2.3