Mercurial > hg > config
annotate python/quote.py @ 294:a0d830fd8a42
refactor this bad boy
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 11 May 2013 03:49:06 -0700 |
parents | 2454d5a1728b |
children | a075f8a93183 |
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() |