Mercurial > hg > decoupage
comparison decoupage/create_index.py @ 93:bdb9e39abd84
make a proper python script for discovery and extension
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 21 Aug 2016 19:27:10 -0700 |
parents | create_index_ini.sh@55719bcdc2c2 |
children | 450aff4c97e3 |
comparison
equal
deleted
inserted
replaced
92:c5895d87c65e | 93:bdb9e39abd84 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 """ | |
4 create index.ini file from directory listings | |
5 """ | |
6 | |
7 #ls -1 | while read line; do echo "${line} = ${line}"; done > index.ini | |
8 | |
9 # imports | |
10 import argparse | |
11 import os | |
12 import sys | |
13 | |
14 | |
15 class CreateIndex(object): | |
16 """ | |
17 decoupage directory index .ini creation | |
18 """ | |
19 # TODO: maybe this should inherit or otherwise extend | |
20 # some more abstract Index class | |
21 | |
22 def __init__(self, directory): | |
23 assert os.path.isdir(directory) | |
24 self.directory = directory | |
25 | |
26 def __str__(self): | |
27 lines = ['{item}={item}'.format(item=item) | |
28 for item in sorted(os.listdir(self.directory))] | |
29 return '\n'.join(lines) + '\n' | |
30 | |
31 | |
32 def main(args=sys.argv[1:]): | |
33 """CLI""" | |
34 | |
35 # parse command line | |
36 parser = argparse.ArgumentParser(description=__doc__) | |
37 parser.add_argument('-d', '--directory', | |
38 default=os.getcwd(), | |
39 help="directory to create index for (current working directory by default)") | |
40 options = parser.parse_args(args) | |
41 | |
42 print CreateIndex(options.directory) | |
43 | |
44 | |
45 if __name__ == '__main__': | |
46 main() |