annotate python/quote.py @ 469:764535e1f991

python/hgrc.py
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 10 Aug 2013 18:54:29 -0700
parents 2454d5a1728b
children a075f8a93183
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
271
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 quote text
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import sys
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import textwrap
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 from subprocess import check_output as call
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 def quote(text, prefix='> ', width=69):
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 """
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 returns quoted text
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 - prefix: string to prepend quote
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 - width: final width (emacs wraps at 70)
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 """
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 width -= len(prefix) # subtract the prefix
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 text = text.strip() # remove surrounding whitespace
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 lines = []
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 for line in text.splitlines():
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 line = line.strip()
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 lines.extend(textwrap.wrap(line, width))
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 return '\n'.join(['%s%s' % (prefix, line)
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 for line in lines])
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 def main(args=sys.argv[1:]):
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 text = sys.stdin.read()
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 sys.stdout.write(quote(text))
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 if __name__ == '__main__':
2454d5a1728b add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 main()