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