Skip to content
This repository has been archived by the owner on May 9, 2018. It is now read-only.

Commit

Permalink
helper for defining subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
mgax committed Oct 27, 2016
1 parent ebe346d commit f94adf8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
37 changes: 11 additions & 26 deletions hoover_script.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys
import os
import argparse
import random
import math
import subprocess
Expand All @@ -9,6 +8,7 @@
from contextlib import contextmanager
from textwrap import dedent
from urllib.request import urlretrieve
from parser import HooverParser

VIRTUALENV_URL = 'https://github.com/pypa/virtualenv/raw/master/virtualenv.py'
SETUPTOOLS_URL = 'https://pypi.python.org/packages/8a/1f/e2e14f0b98d0b6de6c3fb4e8a3b45d3b8907783937c497cb53539c0d2b19/setuptools-28.6.1-py2.py3-none-any.whl'
Expand Down Expand Up @@ -173,8 +173,8 @@ def configure(args):
def execv(args):
os.execv(args[0], args)

def run_webserver(args):
parser = argparse.ArgumentParser(description="Run webserver")
def webserver(args):
parser = HooverParser(description="Run webserver")
parser.add_argument('server', choices=['search', 'snoop'])
(options, extra_args) = parser.parse_known_args(args)

Expand All @@ -188,39 +188,24 @@ def run_webserver(args):
os.chdir(str(home / 'snoop'))
execv([waitress] + extra_args + ['snoop.site.wsgi:application'])

def run_snoop(args):
def snoop(args):
py = str(home / 'venvs' / 'snoop' / 'bin' / 'python')
manage_py = str(home / 'snoop' / 'manage.py')
execv([py, manage_py] + args)

def run_search(args):
def search(args):
py = str(home / 'venvs' / 'search' / 'bin' / 'python')
manage_py = str(home / 'search' / 'manage.py')
execv([py, manage_py] + args)

def main():
parser = argparse.ArgumentParser(description="Hoover setup")
parser.add_argument('cmd',
choices=['bootstrap', 'configure', 'webserver', 'snoop', 'search'])
parser = HooverParser(description="Hoover setup")
parser.add_subcommands('cmd', [
bootstrap, configure,
webserver, snoop, search,
])
(options, extra_args) = parser.parse_known_args()

if options.cmd == 'bootstrap':
bootstrap([])
return

if options.cmd == 'configure':
configure([])
return

if options.cmd == 'webserver':
run_webserver(extra_args)
return

if options.cmd == 'snoop':
run_snoop([])

if options.cmd == 'search':
run_search([])
options.cmd(extra_args)

if __name__ == '__main__':
main()
15 changes: 15 additions & 0 deletions parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import argparse

class HooverParser(argparse.ArgumentParser):

def add_subcommands(self, name, subcommands):
subcommands_map = {c.__name__: c for c in subcommands}

class SubcommandAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, name, subcommands_map[values])

self.add_argument(name,
choices=[c.__name__ for c in subcommands],
action=SubcommandAction,
)

0 comments on commit f94adf8

Please sign in to comment.