annotate makeitso/cli.py @ 210:ab726b2f3143

arbitrary python requirements, the bad way
author Jeff Hammel <k0scist@gmail.com>
date Wed, 25 Mar 2015 12:09:15 -0700
parents aed8c4af5f26
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
57
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 command line parser for MakeItSo
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4
62
30100690ad3f display defaults with command line --help option
Jeff Hammel <jhammel@mozilla.com>
parents: 61
diff changeset
5 from template import Undefined
57
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 from optparse import OptionParser
074a32920f7c stub for a command-line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
8 class MakeItSoCLI(object):
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
9 """command line interface to a makeitso template"""
174
aed8c4af5f26 STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents: 122
diff changeset
10
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
11 def __init__(self, template_class):
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
12 self.template_class = template_class
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
13
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
14 def parser(self):
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
15 """
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
16 return a command line parser for the template
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
17 """
98
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
18 usage = getattr(self, 'usage', '%prog [options] output')
122
b2152efec89a get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents: 121
diff changeset
19 description = self.template_class.get_description()
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
20 parser = OptionParser(usage=usage, description=description)
60
da0d8c5c5157 what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents: 59
diff changeset
21
da0d8c5c5157 what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents: 59
diff changeset
22 # add the variables as options
da0d8c5c5157 what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents: 59
diff changeset
23 for variable in self.template_class.vars:
62
30100690ad3f display defaults with command line --help option
Jeff Hammel <jhammel@mozilla.com>
parents: 61
diff changeset
24 description = variable.description
77
059b02808efa ensure the description exists
Jeff Hammel <jhammel@mozilla.com>
parents: 68
diff changeset
25 if (variable.default is not None) and (variable.default is not Undefined) and description is not None:
62
30100690ad3f display defaults with command line --help option
Jeff Hammel <jhammel@mozilla.com>
parents: 61
diff changeset
26 description += ' [DEFAULT: %s]' % variable.default
60
da0d8c5c5157 what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents: 59
diff changeset
27 parser.add_option('--%s' % variable.name, dest=variable.name,
da0d8c5c5157 what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents: 59
diff changeset
28 default=variable.default,
62
30100690ad3f display defaults with command line --help option
Jeff Hammel <jhammel@mozilla.com>
parents: 61
diff changeset
29 help=description)
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
30 return parser
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
31
98
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
32 def get_variables(self, options):
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
33 """
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
34 return variables from (parsed) options
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
35 """
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
36 return dict([(key, value)
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
37 for key, value in options.__dict__.items()
121
d28cde6c942e do not use undefined defaults
Jeff Hammel <jhammel@mozilla.com>
parents: 120
diff changeset
38 if (not key.startswith('_')) and (value is not Undefined)])
98
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
39
95
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 85
diff changeset
40 def parse(self, args=None, parser=None, options=None):
85
cb6c54b1adf3 allow .parse() to be consumed
Jeff Hammel <jhammel@mozilla.com>
parents: 77
diff changeset
41
cb6c54b1adf3 allow .parse() to be consumed
Jeff Hammel <jhammel@mozilla.com>
parents: 77
diff changeset
42 # parse the command line
95
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 85
diff changeset
43 if not parser or not options:
85
cb6c54b1adf3 allow .parse() to be consumed
Jeff Hammel <jhammel@mozilla.com>
parents: 77
diff changeset
44 parser = self.parser()
95
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 85
diff changeset
45 options, args = parser.parse_args(args=args)
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
46
60
da0d8c5c5157 what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents: 59
diff changeset
47 # ensure output is given
da0d8c5c5157 what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents: 59
diff changeset
48 if len(args) != 1:
61
57f9b0349192 wait, its completely silly to print the variables since OptionParser already does that
Jeff Hammel <jhammel@mozilla.com>
parents: 60
diff changeset
49 parser.error("Please specify a single output destination")
60
da0d8c5c5157 what i really meant to do with variables
Jeff Hammel <jhammel@mozilla.com>
parents: 59
diff changeset
50
58
112bf081148c make a full CLI class for a single API template
Jeff Hammel <jhammel@mozilla.com>
parents: 57
diff changeset
51 # template variables
98
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
52 variables = self.get_variables(options)
174
aed8c4af5f26 STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents: 122
diff changeset
53
98
37f92ae8f999 separate out variable getting to its own function
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
54 # return the variables and the output
95
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 85
diff changeset
55 return variables, args[0]
174
aed8c4af5f26 STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents: 122
diff changeset
56
95
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 85
diff changeset
57 def __call__(self, *args):
174
aed8c4af5f26 STUB: makeitso/cli.py makeitso/script2package.py setup.py
Jeff Hammel <k0scist@gmail.com>
parents: 122
diff changeset
58 """interpolate template"""
95
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 85
diff changeset
59 variables, output = self.parse(list(args))
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 85
diff changeset
60 template = self.template_class(variables=variables)
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 85
diff changeset
61 template.substitute({}, output=output)