Mercurial > hg > MakeItSo
annotate makeitso/python_package/{{package}}/{{main}}.py @ 202:2d009a183d53
STUB: makeitso/python_package/tests/test_{{package}}.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 12 Aug 2014 14:50:50 -0700 |
parents | be7e2e336d7a |
children | 0a991b8fe839 |
rev | line source |
---|---|
79 | 1 #!/usr/bin/env python |
159 | 2 # -*- coding: utf-8 -*- |
79 | 3 |
4 """ | |
5 {{description}} | |
6 """ | |
7 | |
187
74f41d53b057
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
161
diff
changeset
|
8 # imports |
188 | 9 import argparse |
159 | 10 import os |
11 import subprocess | |
79 | 12 import sys |
159 | 13 |
195
3cb66c9e5ce8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
188
diff
changeset
|
14 # module globals |
3cb66c9e5ce8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
188
diff
changeset
|
15 __all__ = ['main', 'Parser'] |
187
74f41d53b057
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
161
diff
changeset
|
16 here = os.path.dirname(os.path.realpath(__file__)) |
188 | 17 string = (str, unicode) |
187
74f41d53b057
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
161
diff
changeset
|
18 |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
19 def ensure_dir(directory): |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
20 """ensure a directory exists""" |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
21 if os.path.exists(directory): |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
22 assert os.path.isdir(directory) |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
23 return directory |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
24 os.makedirs(directory) |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
25 return directory |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
26 |
188 | 27 |
28 class Parser(argparse.ArgumentParser): | |
29 """CLI option parser""" | |
198
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
30 def __init__(self, **kwargs): |
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
31 kwargs.setdefault('description', __doc__) |
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
32 argparse.ArgumentParser.__init__(self, **kwargs) |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
33 self.options = None |
188 | 34 |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
35 def parse_args(self, *args, **kw): |
197 | 36 options = argparse.ArgumentParser.parse_args(self, *args, **kw) |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
37 self.validate(options) |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
38 self.options = options |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
39 return options |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
40 |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
41 def validate(self, options): |
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
42 """validate options""" |
79 | 43 |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
149
diff
changeset
|
44 def main(args=sys.argv[1:]): |
188 | 45 """CLI""" |
130
d9201f911458
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
79
diff
changeset
|
46 |
139 | 47 # parse command line options |
197 | 48 parser = Parser() |
188 | 49 options = parser.parse_args(args) |
79 | 50 |
51 if __name__ == '__main__': | |
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
52 main() |