Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
[slave-core] (#11) Added simple shell and module to parse arguments.
Browse files Browse the repository at this point in the history
Signed-off-by: JeyM1 <[email protected]>
  • Loading branch information
JeyM1 committed Sep 25, 2020
1 parent 57e7c77 commit a4816dd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/slave_core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import slave_core.config as config
from slave_core.connection import *
from slave_core.slave_plugin import *
from slave_core.pathfinder_shell import PathfinderShell

logging.basicConfig(level=logging.DEBUG)

Expand All @@ -15,9 +16,13 @@

def main():
logging.info('Starting...')
dotenv.load_dotenv(dotenv_path=config.ENV_PATH)
logging.debug(os.getenv('HELLO'))
plugin.run_plugin('test')

prompt = PathfinderShell()
prompt.cmdloop('Starting shell...')

# dotenv.load_dotenv(dotenv_path=config.ENV_PATH)
# logging.debug(os.getenv('HELLO'))
# plugin.run_plugin('test')

return 0

Expand Down
51 changes: 51 additions & 0 deletions src/slave_core/pathfinder_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import cmd
import argparse
import tempfile
from slave_core.slave_plugin import *


class ArgparseCmdWrapper:
def __init__(self, parser):
"""Init decorator with an argparse parser to be used in parsing cmd-line options"""
self.parser = parser
self.help_msg = ""

def __call__(self, f):
"""Decorate 'f' to parse 'line' and pass options to decorated function"""
if not self.parser: # If no parser was passed to the decorator, get it from 'f'
self.parser = f(None, None, None, True)

def wrapped_f(*args):
line = args[1].split()
try:
parsed = self.parser.parse_args(line)
except SystemExit:
return
f(*args, parsed=parsed)

return wrapped_f


class PathfinderShell(cmd.Cmd):
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
prompt = '> '

def do_hello(self, args):
"""Says hello. If you provide a name, it will greet you with it."""
if len(args) == 0:
name = 'stranger'
else:
name = args
print(f"Hello, {name}")

__render_parser = argparse.ArgumentParser(prog="render", description="Runs plugin python script for rendering.")
__render_parser.add_argument('--test', help="test help")

@ArgparseCmdWrapper(parser=__render_parser)
def do_render(self, line, parsed):
"""Runs plugin python script."""

print(parsed)

def help_render(self):
self.__render_parser.print_help()

0 comments on commit a4816dd

Please sign in to comment.