Mercurial > hg > MakeItSo
annotate makeitso/python_package/{{package}}/{{main}}.py @ 207:04c5464355b8
monitor
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Tue, 27 Jan 2015 12:21:45 -0800 |
| parents | d9d7bfdb54db |
| children | ab726b2f3143 |
| 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 |
| 207 | 13 import time |
| 159 | 14 |
|
195
3cb66c9e5ce8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
188
diff
changeset
|
15 # module globals |
|
3cb66c9e5ce8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
188
diff
changeset
|
16 __all__ = ['main', 'Parser'] |
|
187
74f41d53b057
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
161
diff
changeset
|
17 here = os.path.dirname(os.path.realpath(__file__)) |
| 188 | 18 string = (str, unicode) |
|
187
74f41d53b057
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
161
diff
changeset
|
19 |
|
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
20 def ensure_dir(directory): |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
21 """ensure a directory exists""" |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
22 if os.path.exists(directory): |
| 205 | 23 if not os.path.isdir(directory): |
| 24 raise OSError("Not a directory: '{}'".format(directory)) | |
|
196
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 os.makedirs(directory) |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
27 return directory |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
28 |
| 188 | 29 |
| 30 class Parser(argparse.ArgumentParser): | |
| 31 """CLI option parser""" | |
|
198
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
32 def __init__(self, **kwargs): |
|
206
d9d7bfdb54db
http://stackoverflow.com/questions/4375327/python-argparse-preformatted-help-text
Jeff Hammel <k0scist@gmail.com>
parents:
205
diff
changeset
|
33 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) |
|
198
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
34 kwargs.setdefault('description', __doc__) |
|
be7e2e336d7a
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
197
diff
changeset
|
35 argparse.ArgumentParser.__init__(self, **kwargs) |
| 207 | 36 self.add_argument('--monitor', dest='monitor', |
| 37 type=float, metavar='SLEEP', | |
| 38 help="run in monitor mode") | |
|
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
39 self.options = None |
| 188 | 40 |
|
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
41 def parse_args(self, *args, **kw): |
| 197 | 42 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
|
43 self.validate(options) |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
44 self.options = options |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
45 return options |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
46 |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
47 def validate(self, options): |
|
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
48 """validate options""" |
| 79 | 49 |
|
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
149
diff
changeset
|
50 def main(args=sys.argv[1:]): |
| 188 | 51 """CLI""" |
|
130
d9201f911458
better description formatting
Jeff Hammel <jhammel@mozilla.com>
parents:
79
diff
changeset
|
52 |
| 139 | 53 # parse command line options |
| 197 | 54 parser = Parser() |
| 188 | 55 options = parser.parse_args(args) |
| 79 | 56 |
| 207 | 57 try: |
| 58 while True: | |
| 59 if options.monitor: | |
| 60 time.sleep(options.monitor) | |
| 61 else: | |
| 62 break | |
| 63 except KeyboardInterrupt: | |
| 64 pass | |
| 65 | |
| 79 | 66 if __name__ == '__main__': |
|
196
d25ca7099df8
STUB: makeitso/python_package/{{package}}/{{main}}.py
Jeff Hammel <k0scist@gmail.com>
parents:
195
diff
changeset
|
67 main() |
| 207 | 68 |
