annotate textshaper/quote.py @ 47:6c7ca72777af

add a test all convenience
author Jeff Hammel <k0scist@gmail.com>
date Sat, 16 May 2015 19:02:03 -0700
parents 4aba57a33376
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
5 quote some lines
14
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import argparse
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import os
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
12 def quotelines(text, quote="'", close_quote=None):
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
13 """
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
14 individually quote each line of text
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
15
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
16 quote -- quote character to use
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
17 close_quote -- closing quote character, if different from opening quote
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
18 """
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
19
27
4aba57a33376 add decorator for converting text to lines of text
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
20 close_quote = quote if close_quote is None else close_quote
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
21 return ["{}{}{}".format(quote, line, close_quote) for line in lines]
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
22
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
23
14
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 def main(args=sys.argv[1:]):
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
25 """CLI"""
14
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 parser = argparse.ArgumentParser(description=__doc__)
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 parser.add_argument('input', nargs='?',
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 type=argparse.FileType('r'), default=sys.stdin,
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 help='input file, or read from stdin if ommitted')
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 options = parser.parse_args(args)
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 25
diff changeset
33 lines = quotelines(options.input)
14
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 print '\n'.join(lines)
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 if __name__ == '__main__':
30f1781024a4 add splitline functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 main()