Browse Source

Refactored commando

main
Lakshmi Vyasarajan 14 years ago
parent
commit
d69180ad43
5 changed files with 64 additions and 95 deletions
  1. +15
    -17
      hyde/cli.py
  2. +3
    -4
      hyde/commando.py
  3. +43
    -71
      hyde/engine.py
  4. +2
    -2
      hyde/hyde.py
  5. +1
    -1
      hyde/tests/test_commando.py

+ 15
- 17
hyde/cli.py View File

@@ -11,20 +11,18 @@ def main():
"""
The main function called by hyde executable
"""
import sys
print sys.argv
parser = argparse.ArgumentParser(description='hyde - a python static website generator',
epilog='Use %(prog)s {command} -h to get help on individual commands')
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument('-s', '--sitepath', action='store', default='.', help="Location of the hyde site")
subcommands = parser.add_subparsers(title="Hyde commands",
description="Entry points for hyde")
init_command = subcommands.add_parser('init', help='Create a new hyde site')
init_command.set_defaults(run=init)
init_command.add_argument('-t', '--template', action='store', default='basic', dest='template',
help='Overwrite the current site if it exists')
init_command.add_argument('-f', '--force', action='store_true', default=False, dest='force',
help='Overwrite the current site if it exists')
args = parser.parse_args()
args.run(args)

# parser = argparse.ArgumentParser(description='hyde - a python static website generator',
# epilog='Use %(prog)s {command} -h to get help on individual commands')
# parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
# parser.add_argument('-s', '--sitepath', action='store', default='.', help="Location of the hyde site")
# subcommands = parser.add_subparsers(title="Hyde commands",
# description="Entry points for hyde")
# init_command = subcommands.add_parser('init', help='Create a new hyde site')
# init_command.set_defaults(run=init)
# init_command.add_argument('-t', '--template', action='store', default='basic', dest='template',
# help='Overwrite the current site if it exists')
# init_command.add_argument('-f', '--force', action='store_true', default=False, dest='force',
# help='Overwrite the current site if it exists')
# args = parser.parse_args()
# args.run(args)
#

hyde/command_line.py → hyde/commando.py View File

@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
"""
A nice declarative interface for Argument parser
Declarative interface for argparse
"""
from argparse import ArgumentParser, Namespace
from argparse import ArgumentParser
from collections import namedtuple

__all__ = [
@@ -93,8 +93,7 @@ class param(metarator):

class Application(object):
"""
Bare bones base class for command line applications. Hides the
meta programming complexities.
Barebones base class for command line applications.
"""
__metaclass__ = CommandLine


+ 43
- 71
hyde/engine.py View File

@@ -1,76 +1,48 @@
# -*- coding: utf-8 -*-
def init(args):
print args.sitepath
print args.force
print args.template
# Ensure sitepath is okay (refer to force parameter)
# Find template by looking at the paths
# 1. Environment Variable
# 2. Hyde Data Directory
# Throw exception on failure
# Do not delete the site path, just overwrite existing files
"""
Implements the hyde entry point commands
"""
import sys
from commando import Application, command, subcommand, param
from version import __version__

def gen(args): pass

def serve(args): pass
class Engine(Application):
"""
The Hyde Application
"""

@command(description='hyde - a python static website generator',
epilog='Use %(prog)s {command} -h to get help on individual commands')
@param('-v', '--version', action='version', version='%(prog)s ' + __version__)
@param('-s', '--sitepath', action='store', default='.', help="Location of the hyde site")
def main(self, params):
"""
Will not be executed. A sub command is required. This function exists to provide
common parameters for the subcommands and some generic stuff like version and
metadata
"""
pass

@subcommand('init', help='Create a new hyde site')
@param('-t', '--template', action='store', default='basic', dest='template',
help='Overwrite the current site if it exists')
@param('-f', '--force', action='store_true', default=False, dest='overwrite',
help='Overwrite the current site if it exists')
def init(self, params):
"""
The initialize command. Creates a new site from the template at the given
sitepath.
"""
print params.sitepath
print params.template
print params.overwrite

def start(self):
"""
main()
"""
args = self.parse(sys.argv[1:])
self.run(args)

from version import __version__

# """
# Implements the hyde entry point commands
# """
# class Command(object):
# """
# Base class for hyde commands
# """
# def __init__(self, **kwargs):
# super(Command, self).__init__(epilog='Use %(prog)s {command} -h to get help on individual commands', **kwargs)
# self.subcommands = None
#
# def compose(self, commands, **kwargs):
# self.subcommands = self.add_subparsers(**kwargs)
# for command in commands:
# self.subcommands.add_parser(command)
#
# def run(self, args=None):
# """
# Executes the command
# """
# options = {}
# options.update(self.defaults)
# if args:
# options.update(args)
# self.execute(options)
#
# def execute(self):
# """
# Abstract method for the derived classes
# """
# abstract
#
# class HydeCommand(Command):
# """
# The parent command object.
# """
# def __init__(self, **kwargs):
# super(HydeCommand, self).__init__(**kwargs)
# self.add_argument('--version', action='version', version='%(prog)s ' + __version__)
# self.add_argument('-s', '--sitepath', action='store', default='.', help="Location of the hyde site")
#
#
# class Initializer(Command):
# """
# Represents the `hyde init` command
# """
# def __init__(self, parent, **kwargs):
# super(Initializer, self).__init__(**kwargs)
# init_command.add_argument('-t', '--template', action='store', default='basic', dest='template',
# help='Overwrite the current site if it exists')
# init_command.add_argument('-f', '--force', action='store_true', default=False, dest='force',
# help='Overwrite the current site if it exists')
#
# def run(self):
# """
#
# """
# pass

+ 2
- 2
hyde/hyde.py View File

@@ -3,7 +3,7 @@
"""
The hyde executable
"""
from cli import main
from engine import Engine

if __name__ == "__main__":
main()
Engine().start()

hyde/tests/test_command_line.py → hyde/tests/test_commando.py View File

@@ -6,7 +6,7 @@ Use nose
"""

from contextlib import nested
from hyde.command_line import Application, command, subcommand, param
from hyde.commando import Application, command, subcommand, param
from util import trap_exit_pass, trap_exit_fail
from mock import Mock, patch
try:

Loading…
Cancel
Save