Mercurial > hg > TextShaper
comparison textshaper/quote.py @ 26:c23782a7b7ba
more hookups, yo
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Sun, 23 Feb 2014 11:41:37 -0800 |
| parents | a43d0ad17c29 |
| children | 4aba57a33376 |
comparison
equal
deleted
inserted
replaced
| 25:a43d0ad17c29 | 26:c23782a7b7ba |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
| 3 | 3 |
| 4 """ | 4 """ |
| 5 split some lines | 5 quote some lines |
| 6 """ | 6 """ |
| 7 | 7 |
| 8 import argparse | 8 import argparse |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| 11 | 11 |
| 12 def quotelines(text, quote="'", close_quote=None): | |
| 13 """ | |
| 14 individually quote each line of text | |
| 15 | |
| 16 quote -- quote character to use | |
| 17 close_quote -- closing quote character, if different from opening quote | |
| 18 """ | |
| 19 | |
| 20 if close_quote is None: | |
| 21 close_quote = quote | |
| 22 return ["{}{}{}".format(quote, line, close_quote) for line in lines] | |
| 23 | |
| 24 | |
| 12 def main(args=sys.argv[1:]): | 25 def main(args=sys.argv[1:]): |
| 26 """CLI""" | |
| 13 | 27 |
| 14 parser = argparse.ArgumentParser(description=__doc__) | 28 parser = argparse.ArgumentParser(description=__doc__) |
| 15 parser.add_argument('input', nargs='?', | 29 parser.add_argument('input', nargs='?', |
| 16 type=argparse.FileType('r'), default=sys.stdin, | 30 type=argparse.FileType('r'), default=sys.stdin, |
| 17 help='input file, or read from stdin if ommitted') | 31 help='input file, or read from stdin if ommitted') |
| 18 parser.add_argument('--quote', dest='quote', | |
| 19 action='store_true', default=False) | |
| 20 options = parser.parse_args(args) | 32 options = parser.parse_args(args) |
| 21 | 33 |
| 22 read = options.input.read() | 34 lines = quotelines(options.input) |
| 23 | |
| 24 lines = read.strip().split() | |
| 25 if options.quote: | |
| 26 lines = ["'{}'".format(line) for line in lines] | |
| 27 | 35 |
| 28 print '\n'.join(lines) | 36 print '\n'.join(lines) |
| 29 | 37 |
| 30 if __name__ == '__main__': | 38 if __name__ == '__main__': |
| 31 main() | 39 main() |
