Skip to content

Commit

Permalink
Implemented commands.
Browse files Browse the repository at this point in the history
- Each individual pyhoe command is implemented in it's own file.
- Changed template to templates. The templates directory will
  hold several templates in the future.
  • Loading branch information
Brian Gesiak committed Feb 10, 2012
1 parent 63994b2 commit a3f387b
Show file tree
Hide file tree
Showing 23 changed files with 151 additions and 56 deletions.
12 changes: 9 additions & 3 deletions builder.py
Expand Up @@ -4,7 +4,7 @@
from os.path import join
import shutil

TEMPLATE_DIR = "template"
TEMPLATE_DIR = "templates"

class ProjectBuilder(object):
"""
Expand All @@ -13,8 +13,14 @@ def __init__(self):
"""docstring for __init__"""
pass

def build(self, project_name, python_exe=None, skip_confirmation=False):
def build(
self,
project_name,
template,
python_exe=None,
skip_confirmation=False
):
print "Building project: %s" % project_name
if python_exe: print "Using %s" % python_exe
print "Skip confirmation: %s" % str(skip_confirmation)
shutil.copytree(TEMPLATE_DIR, project_name)
shutil.copytree(join(TEMPLATE_DIR, template), project_name)
1 change: 1 addition & 0 deletions cmdline.py
Expand Up @@ -33,6 +33,7 @@ def execute(self):
self.clean_args()
build_args = {
"project_name": self.args["project_name"],
"template": self.args["template"],
"skip_confirmation": self.args["yes_to_all"]
}
if self.args["python_exe"]:
Expand Down
53 changes: 53 additions & 0 deletions pyhoe.py
@@ -0,0 +1,53 @@
import sys
import types
import argparse
from pyhoe import commands as cmds
from pyhoe.commands import *

COMMANDS = list((
cmds.__dict__.get(c) for c in dir(cmds) if isinstance(
cmds.__dict__.get(c), types.ModuleType
)
))

def available_command_names():
"""
Returns a list of command names in pyhoe.commands.
"""
return [c.__name__.split(".")[-1] for c in COMMANDS]


if __name__ == "__main__":
# Instantiate argument parser, add arguments.
parser = argparse.ArgumentParser(
prog="pyhoe",
description = (
"Provides several convenient functions for developing "
"Python packages and scripts using best practices."
)
)
parser.add_argument(
"action",
choices = available_command_names(),
help = "Choose an action for pyhoe to perform."
)
parser.add_argument(
"options",
nargs = "*",
help = (
"Add any option flags or positional arguments for "
"the specified action. You can see options by simply "
"entering the action name."
)
)

# Print help if no args provided.
if len(sys.argv) <= 1:
parser.print_help()
sys.exit(0) # TODO - Perhaps exit with error?

# Pass remaining args to appropriate function.
args = vars(parser.parse_args(sys.argv[1:]))
for c in COMMANDS:
if args["action"] == c.__name__.split(".")[-1]:
c.execute(sys.argv[2:])
File renamed without changes.
9 changes: 9 additions & 0 deletions pyhoe/commands/__init__.py
@@ -0,0 +1,9 @@
from os import listdir
from os.path import dirname, splitext
import re

r = re.compile("^[a-zA-Z0-9]*\.py$")

__all__ = [
splitext(m)[0]for m in listdir(dirname(__file__)) if r.match(m)
]
79 changes: 79 additions & 0 deletions pyhoe/commands/startproject.py
@@ -0,0 +1,79 @@
#!/usr/bin/env python

import os, sys
import argparse
from cmdline import CommandLineDelegator

# FIXME - Place this somewhere
TEMPLATE_DIR = "templates"

def default_temaplate():
"""
Returns a string representing the default template directory.
"""
default = "package"
if default in os.listdir(TEMPLATE_DIR):
return default
else:
return os.listdir(TEMPLATE_DIR)[0]


def parse_cl_args(sysargs):
"""
Uses argparse to handle user input.
Returns a __dict__ representation of
the parsed arguments.
"""
parser = argparse.ArgumentParser(
prog="pyhoe startproject",
description = (
"Initializes a Python project using established best practices."
)
)
parser.add_argument(
"project_name",
help = "The name of the project."
)
parser.add_argument(
"-c", "--cucumber",
action = "store_true",
help = "Use freshen for behavior-driven development."
)
parser.add_argument(
"-p", "--python",
dest = "python_exe",
help = (
"The Python interpreter to use, e.g.: "
"--python=python2.6 will use the Python "
"2.6 interpreter to create any new virtual "
"environments."
),
)
parser.add_argument(
"-t", "--template",
choices = os.listdir(TEMPLATE_DIR),
default = default_temaplate(),
help = "The template to use for your project."
)
parser.add_argument(
"-y", "--yes-to-all",
action = "store_true",
help = (
"Skip all confirmation and use recommended settings."
)
)
return vars(parser.parse_args(sysargs))


def execute(args=None):
"""
Parses sys.args or args parameter and executes
the parsed values using CommandLineDelegator.
"""
a = args or sys.argv
delegator = CommandLineDelegator(parse_cl_args(a))
delegator.execute()


if __name__ == "__main__":
execute()
42 changes: 0 additions & 42 deletions startproject.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 0 additions & 11 deletions util/delegator.py

This file was deleted.

0 comments on commit a3f387b

Please sign in to comment.