annotate textshaper/corename.py @ 45:ccbdc00d4f0a

stub
author Jeff Hammel <k0scist@gmail.com>
date Tue, 12 May 2015 21:21:04 -0700
parents 8addd6e12b29
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 echo core name of files
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import sys
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 # module globals
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 __all__ = ['main', 'Parser']
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def ensure_dir(directory):
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 """ensure a directory exists"""
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 if os.path.exists(directory):
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 if not os.path.isdir(directory):
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 raise OSError("Not a directory: '{}'".format(directory))
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 return directory
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 os.makedirs(directory)
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 return directory
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 class Parser(argparse.ArgumentParser):
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 """CLI option parser"""
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 def __init__(self, **kwargs):
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 kwargs.setdefault('description', __doc__)
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 argparse.ArgumentParser.__init__(self, **kwargs)
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 self.add_argument('files', nargs='*',
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 help="files, else read from stdin")
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 self.options = None
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 def parse_args(self, *args, **kw):
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 self.validate(options)
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 self.options = options
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 return options
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 def validate(self, options):
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 """validate options"""
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 def main(args=sys.argv[1:]):
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 """CLI"""
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 # parse command line options
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 parser = Parser()
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 options = parser.parse_args(args)
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 if not options.files:
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 options.files = sys.stdin.read().strip().split()
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 corenames = [os.path.splitext(os.path.basename(f))[0]
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 for f in options.files]
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 print (" ".join(corenames))
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 if __name__ == '__main__':
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 main()
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61
8addd6e12b29 corename
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62