annotate textshaper/main.py @ 12:5b70bbff04b1

STUB: textshaper/main.py
author Jeff Hammel <k0scist@gmail.com>
date Sat, 25 Jan 2014 18:09:48 -0800
parents 71fb16088d54
children c3df7dccb02b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
4
26bb5e99f76c textshaper/main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
5 package to shape text blocks
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 """
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import optparse
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import os
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import subprocess
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import sys
3
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
12 import time
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
12
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
14 def info(content):
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
15 """gathers info about the content and returns a dict"""
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
16 lines = content.splitlines()
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
17 return {'lines': len(lines),
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
18 'chars': len(content),
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
19 'columns': max([len(line) for line in lines])
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
20 }
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
21
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
22 def display(content, keys=('lines', 'chars', 'columns'), hr='--'):
4
26bb5e99f76c textshaper/main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
23 print content
12
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
24 if keys:
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
25 _info = info(content)
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
26 print (hr)
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
27 print ('; '.join(['{}: {}'.format(key, _info[key])
5b70bbff04b1 STUB: textshaper/main.py
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
28 for key in keys])
4
26bb5e99f76c textshaper/main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
29
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 def add_options(parser):
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 """add options to the OptionParser instance"""
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
33 # TODO
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
34 # parser.add_option('-c', '--clip', '--copy', dest='copy_to_clipboard',
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
35 # help="copy to given program on exit")
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
36
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 def main(args=sys.argv[1:]):
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 # parse command line options
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 usage = '%prog [options] ...'
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 """description formatter for console script entry point"""
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 def format_description(self, description):
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 if description:
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 return description.strip() + '\n'
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 else:
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 return ''
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 options, args = parser.parse_args(args)
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50
3
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
51 # read from stdin
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
52 content = sys.stdin.read()
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
53
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
54 # print formatted content
4
26bb5e99f76c textshaper/main.py
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
55 display(content)
3
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
56
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
57 while True:
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58 time.sleep(1) # XXX
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
59
0
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 if __name__ == '__main__':
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 main()
53cfd6e1ad0f initial commit,
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62