annotate textshaper/indent.py @ 57:a2bbe406f570 default tip

which is no longer maintained; roll our mediocre own
author Jeff Hammel <k0scist@gmail.com>
date Mon, 20 Feb 2017 10:40:34 -0800
parents 88a69d587326
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 indentation of text blocks
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
24
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
7 import argparse
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import os
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import sys
31
23616399f36c make indent a @lines function and test
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
10 from .decorator import lines
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
31
23616399f36c make indent a @lines function and test
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
12 @lines
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 def indent(text, indentation=4, space=' ', strict=False):
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 """
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 indent a block of text
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 text -- lines of text to indent
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 indentation -- number of spaces to indent
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 space -- what to indent with
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 strict -- whether to enforce required whitespace for negative indentation
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 """
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 if not indentation:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 # nothing to do
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 return text
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 if indentation > 0:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 retval = [space * indentation + line for line in text]
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 else:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 # negative indentation
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 indentation = -indentation
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 retval = []
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 for line in text:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 prefix = line[:indentation]
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 for index, char in enumerate(prefix):
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 if not char == space:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 if strict:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 raise AssertionError("Found non-'%s' charcter at column %d for indentation -%d" % (space, index, indentation))
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 break
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 else:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 index = indentation
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 retval.append(line[index:])
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43
10
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
44 return retval
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
45
34
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
46 @lines
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
47 def deindent(text):
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
48 """strip lines"""
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
49 return [line.strip() for line in text]
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
50
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
51 ### CLI
10
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
52
24
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
53 def add_arguments(parser):
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
54 parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
55 default=sys.stdin)
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
56 parser.add_argument('-o', '--output', dest='output',
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
57 help="output file or stdout if not given")
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
58
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 24
diff changeset
59
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 def main(args=sys.argv[1:]):
34
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 31
diff changeset
61 """CLI"""
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62
10
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
63 # parse command line
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
64 description = """indent files or stdin if no files given"""
24
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
65 parser = argparse.Argument(description=__doc__)
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
66 add_arguments(parser)
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
67 options = parser.parse_args(args)
10
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
68
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
69 # process input
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
70 for f in _files():
24
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
71
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
72 # indent the text
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 24
diff changeset
73 indented = '\n'.join(indent(f))
24
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
74
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
75 # append to output
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
76 if options.output:
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
77 with open(options.output, 'a') as f:
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
78 f.write(indented)
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
79 else:
0930c6884f8a STUB: README.txt setup.py textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
80 sys.stdout.write(indented)
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 if __name__ == '__main__':
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 main()