|
a/scripts/wiki-post.py |
|
b/scripts/wiki-post.py |
|
... |
|
... |
2 |
|
2 |
|
3 |
from sys import stdin, stdout
|
3 |
from sys import stdin, stdout
|
4 |
import hmac, hashlib
|
4 |
import hmac, hashlib
|
5 |
from datetime import datetime
|
5 |
from datetime import datetime
|
6 |
import os
|
6 |
import os
|
7 |
from urllib import urlencode
|
7 |
import urllib
|
8 |
from urllib2 import urlopen
|
8 |
from urllib2 import urlopen, HTTPError
|
9 |
from urlparse import urlparse, urljoin
|
9 |
from urlparse import urlparse, urljoin
|
10 |
from optparse import OptionParser
|
10 |
from optparse import OptionParser
|
11 |
from ConfigParser import ConfigParser
|
11 |
from ConfigParser import ConfigParser
|
|
|
12 |
|
|
|
13 |
def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
|
|
|
14 |
"""
|
|
|
15 |
Returns a bytestring version of 's', encoded as specified in 'encoding'.
|
|
|
16 |
|
|
|
17 |
If strings_only is True, don't convert (some) non-string-like objects.
|
|
|
18 |
|
|
|
19 |
This function was borrowed from Django
|
|
|
20 |
"""
|
|
|
21 |
if strings_only and isinstance(s, (types.NoneType, int)):
|
|
|
22 |
return s
|
|
|
23 |
elif not isinstance(s, basestring):
|
|
|
24 |
try:
|
|
|
25 |
return str(s)
|
|
|
26 |
except UnicodeEncodeError:
|
|
|
27 |
if isinstance(s, Exception):
|
|
|
28 |
# An Exception subclass containing non-ASCII data that doesn't
|
|
|
29 |
# know how to print itself properly. We shouldn't raise a
|
|
|
30 |
# further exception.
|
|
|
31 |
return ' '.join([smart_str(arg, encoding, strings_only,
|
|
|
32 |
errors) for arg in s])
|
|
|
33 |
return unicode(s).encode(encoding, errors)
|
|
|
34 |
elif isinstance(s, unicode):
|
|
|
35 |
r = s.encode(encoding, errors)
|
|
|
36 |
return r
|
|
|
37 |
elif s and encoding != 'utf-8':
|
|
|
38 |
return s.decode('utf-8', errors).encode(encoding, errors)
|
|
|
39 |
else:
|
|
|
40 |
return s
|
|
|
41 |
|
|
|
42 |
def generate_smart_str(params):
|
|
|
43 |
for (key, value) in params:
|
|
|
44 |
if value is None: continue
|
|
|
45 |
yield smart_str(key), smart_str(value)
|
|
|
46 |
|
|
|
47 |
def urlencode(params):
|
|
|
48 |
"""
|
|
|
49 |
A version of Python's urllib.urlencode() function that can operate on
|
|
|
50 |
unicode strings. The parameters are first case to UTF-8 encoded strings and
|
|
|
51 |
then encoded as per normal.
|
|
|
52 |
"""
|
|
|
53 |
return urllib.urlencode([i for i in generate_smart_str(params)])
|
|
|
54 |
|
12 |
|
55 |
|
13 |
class Signer(object):
|
56 |
class Signer(object):
|
14 |
|
57 |
|
15 |
def __init__(self, secret_key, api_key):
|
58 |
def __init__(self, secret_key, api_key):
|
16 |
self.secret_key = secret_key
|
59 |
self.secret_key = secret_key
|
|
... |
|
... |
32 |
op.add_option('-s', '--secret-key', metavar='KEY')
|
75 |
op.add_option('-s', '--secret-key', metavar='KEY')
|
33 |
op.add_option('-u', '--url', metavar='URL')
|
76 |
op.add_option('-u', '--url', metavar='URL')
|
34 |
(options, args) = op.parse_args()
|
77 |
(options, args) = op.parse_args()
|
35 |
|
78 |
|
36 |
page = args[0]
|
79 |
page = args[0]
|
37 |
f = open(args[1], 'r') if len(args)>=1 else stdin
|
80 |
f = open(args[1], 'r') if len(args)>1 else stdin
|
38 |
markdown = f.read()
|
81 |
markdown = f.read()
|
39 |
|
82 |
|
40 |
config = ConfigParser()
|
83 |
config = ConfigParser()
|
41 |
config.read([str(os.path.expanduser('~/.forge-api.ini')), str(options.config)])
|
84 |
config.read([str(os.path.expanduser('~/.forge-api.ini')), str(options.config)])
|
42 |
|
85 |
|
|
... |
|
... |
46 |
|
89 |
|
47 |
url = urljoin(options.url or config.get('wiki', 'url'), page)
|
90 |
url = urljoin(options.url or config.get('wiki', 'url'), page)
|
48 |
|
91 |
|
49 |
sign = Signer(secret_key, api_key)
|
92 |
sign = Signer(secret_key, api_key)
|
50 |
params = sign(urlparse(url).path, [('text', markdown)])
|
93 |
params = sign(urlparse(url).path, [('text', markdown)])
|
|
|
94 |
try:
|
51 |
result = urlopen(url, urlencode(params))
|
95 |
result = urlopen(url, urlencode(params))
|
52 |
stdout.write(result.read())
|
96 |
stdout.write(result.read())
|
|
|
97 |
except HTTPError, e:
|
|
|
98 |
stdout.write(e.read())
|
53 |
|
99 |
|
54 |
if __name__ == '__main__':
|
100 |
if __name__ == '__main__':
|
55 |
main()
|
101 |
main()
|