annotate textshaper/indent.py @ 21:e6f680d25d63

migrate url2txt
author Jeff Hammel <k0scist@gmail.com>
date Sun, 23 Feb 2014 00:45:06 -0800
parents 0d0db0d79bfd
children 0930c6884f8a
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
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import optparse
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
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 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
12 """
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 indent a block of text
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 text -- lines of text to indent
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 indentation -- number of spaces to indent
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 space -- what to indent with
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 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
19 """
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 if not indentation:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 # nothing to do
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 return text
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 if indentation > 0:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 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
27 else:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 # negative indentation
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 indentation = -indentation
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 retval = []
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 for line in text:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 prefix = line[:indentation]
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 for index, char in enumerate(prefix):
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 if not char == space:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 if strict:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 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
37 break
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 else:
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 index = indentation
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 retval.append(line[index:])
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
10
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
42 return retval
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
43
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
44
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 def main(args=sys.argv[1:]):
11
0d0db0d79bfd STUB: textshaper/indent.py
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
46 # TODO : refactor to be more general and stuff
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
10
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
48 # parse command line
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
49 usage = '%prog [options] [file] [file2] [...]'
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
50 description = """indent files or stdin if no files given"""
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 parser = optparse.OptionParser(usage=usage, description=__doc__)
10
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
52 parser.add_option('-o', '--output', dest='output',
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
53 help="output file or stdout if not given")
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
54 options, files = parser.parse_args(args)
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
10
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
56 # input from files or stdin
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
57 if files:
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
58 missing = [not os.path.exists(filename) for filename in files]
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
59 if missing:
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
60 parser.error("File(s) not found: %s" % ', '.join(missing))
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
61 def _files():
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
62 for filename in files:
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
63 with open(f, 'r') as f:
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
64 yield f
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
65 else:
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
66 def _files():
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
67 yield sys.stdin
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():
466386702968 more stuff
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
71 print f
9
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 if __name__ == '__main__':
71fb16088d54 add file for indentation; wth did my work go??? :(
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 main()