annotate lemuriformes/main.py @ 4:9575c4a925c1

[setup] correct various dependencies + scripts
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 11:49:47 -0800
parents 7a4a073ee13e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 ReSTful API consumer in python
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import subprocess
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import sys
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import time
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 # python requirements
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 # (key, value) = (module, PyPI name)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 requirements = ()
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 for module, package in requirements:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 try:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 globals()[module] = __import__(module)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 except ImportError:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 # install requirement and try again
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 subprocess.check_call(['pip', 'install', package])
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 args = [sys.executable] + sys.argv
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 os.execl(sys.executable, *args)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 # module globals
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 __all__ = ['main', 'Parser']
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 here = os.path.dirname(os.path.realpath(__file__))
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 try:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 # python 2
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 string = (str, unicode)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 except NameError:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 # python 3
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 string = (str, )
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 def ensure_dir(directory):
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 """ensure a directory exists"""
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 if os.path.exists(directory):
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 if not os.path.isdir(directory):
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 raise OSError("Not a directory: '{}'".format(directory))
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 return directory
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 os.makedirs(directory)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 return directory
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 class Parser(argparse.ArgumentParser):
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 """CLI option parser"""
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 def __init__(self, **kwargs):
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 kwargs.setdefault('description', __doc__)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 argparse.ArgumentParser.__init__(self, **kwargs)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 self.add_argument('--monitor', dest='monitor',
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 type=float, metavar='SLEEP',
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 help="run in monitor mode")
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 self.options = None
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 def parse_args(self, *args, **kw):
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 self.validate(options)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 self.options = options
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 return options
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 def validate(self, options):
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 """validate options"""
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 def main(args=sys.argv[1:]):
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 """CLI"""
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 # parse command line options
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 parser = Parser()
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 options = parser.parse_args(args)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 try:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 while True:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 if options.monitor:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 time.sleep(options.monitor)
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 else:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 break
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 except KeyboardInterrupt:
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 pass
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88 if __name__ == '__main__':
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89 main()
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90
7a4a073ee13e initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91