Permalink
Browse files

Move Cheetah off the imported optik parser in favor of the Python bui…

…ltin optparse module

Signed-off-by: R. Tyler Ballance <tyler@slide.com>
  • Loading branch information...
1 parent 80b7f49 commit 6f21865845f3b12aa2fd7727309770c27e3447cc R. Tyler Ballance committed Mar 30, 2009
View
@@ -11,7 +11,6 @@
'Cheetah.Tests',
'Cheetah.Tools',
'Cheetah.Utils',
- 'Cheetah.Utils.optik',
]
classifiers = [line.strip() for line in '''\
#Development Status :: 4 - Beta
View
@@ -18,11 +18,11 @@
import getopt, glob, os, pprint, re, shutil, sys
import cPickle as pickle
+from optparse import OptionParser
from Cheetah.Version import Version
from Cheetah.Template import Template, DEFAULT_COMPILER_SETTINGS
from Cheetah.Utils.Misc import mkdirsWithPyInitFiles
-from Cheetah.Utils.optik import OptionParser
optionDashesRE = re.compile( R"^-{1,2}" )
moduleNameRE = re.compile( R"^[a-zA-Z_][a-zA-Z_0-9]*$" )
@@ -54,7 +54,7 @@ def __repr__(self):
class MyOptionParser(OptionParser):
- standard_option_list = [] # We use commands for Optik's standard options.
+ standard_option_list = []
def error(self, msg):
"""Print our usage+error page."""
@@ -108,36 +108,10 @@ def usage(usageMessage, errorMessage="", out=sys.stderr):
Run "cheetah options" for the list of valid options.
"""
-HELP_PAGE2 = """\
-OPTIONS FOR "compile" AND "fill":
----------------------------------
- --idir DIR, --odir DIR : input/output directories (default: current dir)
- --iext EXT, --oext EXT : input/output filename extensions
- (default for compile: tmpl/py, fill: tmpl/html)
- -R : recurse subdirectories looking for input files
- --debug : print lots of diagnostic output to standard error
- --env : put the environment in the searchList
- --flat : no destination subdirectories
- --nobackup : don't make backups
- --pickle FILE : unpickle FILE and put that object in the searchList
- --stdout, -p : output to standard output (pipe)
- --settings : a string representing the compiler settings to use
- e.g. --settings='useNameMapper=False,useFilters=False'
- This string is eval'd in Python so it should contain
- valid Python syntax.
- --templateAPIClass : a string representing a subclass of
- Cheetah.Template:Template to use for compilation
-
- --parallel : compile or fill templates in parallel, e.g.
- --parallel 4
-
-Run "cheetah help" for the main help screen.
-"""
-
##################################################
## CheetahWrapper CLASS
-class CheetahWrapper:
+class CheetahWrapper(object):
MAKE_BACKUPS = True
BACKUP_SUFFIX = ".bak"
_templateClass = None
@@ -150,6 +124,7 @@ def __init__(self):
self.pathArgs = None
self.sourceFiles = []
self.searchList = []
+ self.parser = None
##################################################
## MAIN ROUTINE
@@ -192,24 +167,24 @@ def parseOpts(self, args):
C, D, W = self.chatter, self.debug, self.warn
self.isCompile = isCompile = self.command[0] == 'c'
defaultOext = isCompile and ".py" or ".html"
- parser = MyOptionParser()
- pao = parser.add_option
- pao("--idir", action="store", dest="idir", default="")
- pao("--odir", action="store", dest="odir", default="")
- pao("--iext", action="store", dest="iext", default=".tmpl")
- pao("--oext", action="store", dest="oext", default=defaultOext)
- pao("-R", action="store_true", dest="recurse", default=False)
- pao("--stdout", "-p", action="store_true", dest="stdout", default=False)
- pao("--debug", action="store_true", dest="debug", default=False)
- pao("--env", action="store_true", dest="env", default=False)
- pao("--pickle", action="store", dest="pickle", default="")
- pao("--flat", action="store_true", dest="flat", default=False)
- pao("--nobackup", action="store_true", dest="nobackup", default=False)
- pao("--settings", action="store", dest="compilerSettingsString", default=None)
- pao("--templateAPIClass", action="store", dest="templateClassName", default=None)
- pao("--parallel", action="store", type="int", dest="parallel", default=1)
-
- self.opts, self.pathArgs = opts, files = parser.parse_args(args)
+ self.parser = MyOptionParser()
+ pao = self.parser.add_option
+ pao("--idir", action="store", dest="idir", default='', help='Input directory (defaults to current directory)')
+ pao("--odir", action="store", dest="odir", default="", help='Output directory (defaults to current directory)')
+ pao("--iext", action="store", dest="iext", default=".tmpl", help='File input extension (defaults: compile: .tmpl, fill: .tmpl)')
+ pao("--oext", action="store", dest="oext", default=defaultOext, help='File output extension (defaults: compile: .py, fill: .html)')
+ pao("-R", action="store_true", dest="recurse", default=False, help='Recurse through subdirectories looking for input files')
+ pao("--stdout", "-p", action="store_true", dest="stdout", default=False, help='Verbosely print informational messages to stdout')
+ pao("--debug", action="store_true", dest="debug", default=False, help='Print diagnostic/debug information to stderr')
+ pao("--env", action="store_true", dest="env", default=False, help='Pass the environment into the search list')
+ pao("--pickle", action="store", dest="pickle", default="", help='Unpickle FILE and pass it through in the search list')
+ pao("--flat", action="store_true", dest="flat", default=False, help='Do not build destination subdirectories')
+ pao("--nobackup", action="store_true", dest="nobackup", default=False, help='Do not make backup files when generating new ones')
+ pao("--settings", action="store", dest="compilerSettingsString", default=None, help='String of compiler settings to pass through, e.g. --settings="useNameMapper=False,useFilters=False"')
+ pao("--templateAPIClass", action="store", dest="templateClassName", default=None, help='Name of a subclass of Cheetah.Template.Template to use for compilation, e.g. MyTemplateClass')
+ pao("--parallel", action="store", type="int", dest="parallel", default=1, help='Compile/fill templates in parallel, e.g. --parallel=4')
+
+ self.opts, self.pathArgs = opts, files = self.parser.parse_args(args)
D("""\
cheetah compile %s
Options are
@@ -252,7 +227,7 @@ def help(self):
usage(HELP_PAGE1, "", sys.stdout)
def options(self):
- usage(HELP_PAGE2, "", sys.stdout)
+ return self.parser.print_help()
def test(self):
# @@MO: Ugly kludge.
@@ -1,2 +0,0 @@
-.cvsignore
-*.pyc
@@ -1,32 +0,0 @@
-"""optik
-
-A powerful, extensible, and easy-to-use command-line parser for Python.
-
-By Greg Ward <gward@python.net>
-
-See http://optik.sourceforge.net/
-
-Cheetah modifications: added "Cheetah.Utils.optik." prefix to
- all intra-Optik imports.
-"""
-
-# Copyright (c) 2001 Gregory P. Ward. All rights reserved.
-# See the README.txt distributed with Optik for licensing terms.
-
-__revision__ = "$Id: __init__.py,v 1.2 2002/09/12 06:56:51 hierro Exp $"
-
-__version__ = "1.3"
-
-
-# Re-import these for convenience
-from Cheetah.Utils.optik.option import Option
-from Cheetah.Utils.optik.option_parser import \
- OptionParser, SUPPRESS_HELP, SUPPRESS_USAGE, STD_HELP_OPTION
-from Cheetah.Utils.optik.errors import OptionValueError
-
-
-# Some day, there might be many Option classes. As of Optik 1.3, the
-# preferred way to instantiate Options is indirectly, via make_option(),
-# which will become a factory function when there are many Option
-# classes.
-make_option = Option
View
@@ -1,52 +0,0 @@
-"""optik.errors
-
-Exception classes used by Optik.
-"""
-
-__revision__ = "$Id: errors.py,v 1.1 2002/08/24 17:10:06 hierro Exp $"
-
-# Copyright (c) 2001 Gregory P. Ward. All rights reserved.
-# See the README.txt distributed with Optik for licensing terms.
-
-# created 2001/10/17 GPW (from optik.py)
-
-
-class OptikError (Exception):
- def __init__ (self, msg):
- self.msg = msg
-
- def __str__ (self):
- return self.msg
-
-
-class OptionError (OptikError):
- """
- Raised if an Option instance is created with invalid or
- inconsistent arguments.
- """
-
- def __init__ (self, msg, option):
- self.msg = msg
- self.option_id = str(option)
-
- def __str__ (self):
- if self.option_id:
- return "option %s: %s" % (self.option_id, self.msg)
- else:
- return self.msg
-
-class OptionConflictError (OptionError):
- """
- Raised if conflicting options are added to an OptionParser.
- """
-
-class OptionValueError (OptikError):
- """
- Raised if an invalid option value is encountered on the command
- line.
- """
-
-class BadOptionError (OptikError):
- """
- Raised if an invalid or ambiguous option is seen on the command-line.
- """
Oops, something went wrong.

0 comments on commit 6f21865

Please sign in to comment.