Mercurial > hg > numerics
comparison kplot/main.py @ 0:48d0f28311a3
init
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Thu, 14 Aug 2014 16:26:16 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:48d0f28311a3 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 # -*- coding: utf-8 -*- | |
| 3 | |
| 4 """ | |
| 5 personal experiments in plotting | |
| 6 """ | |
| 7 | |
| 8 # imports | |
| 9 import argparse | |
| 10 import os | |
| 11 import subprocess | |
| 12 import sys | |
| 13 | |
| 14 # module globals | |
| 15 __all__ = ['main', 'Parser'] | |
| 16 here = os.path.dirname(os.path.realpath(__file__)) | |
| 17 string = (str, unicode) | |
| 18 | |
| 19 def ensure_dir(directory): | |
| 20 """ensure a directory exists""" | |
| 21 if os.path.exists(directory): | |
| 22 assert os.path.isdir(directory) | |
| 23 return directory | |
| 24 os.makedirs(directory) | |
| 25 return directory | |
| 26 | |
| 27 | |
| 28 class Parser(argparse.ArgumentParser): | |
| 29 """CLI option parser""" | |
| 30 def __init__(self, **kwargs): | |
| 31 kwargs.setdefault('description', __doc__) | |
| 32 argparse.ArgumentParser.__init__(self, **kwargs) | |
| 33 self.options = None | |
| 34 | |
| 35 def parse_args(self, *args, **kw): | |
| 36 options = argparse.ArgumentParser.parse_args(self, *args, **kw) | |
| 37 self.validate(options) | |
| 38 self.options = options | |
| 39 return options | |
| 40 | |
| 41 def validate(self, options): | |
| 42 """validate options""" | |
| 43 | |
| 44 def main(args=sys.argv[1:]): | |
| 45 """CLI""" | |
| 46 | |
| 47 # parse command line options | |
| 48 parser = Parser() | |
| 49 options = parser.parse_args(args) | |
| 50 | |
| 51 if __name__ == '__main__': | |
| 52 main() | |
| 53 |
