[go: up one dir, main page]

Page MenuHomePhabricator

phaste.py
ActivePublic

Authored by mmodell on Nov 18 2014, 10:37 PM.
Referenced Files
F918: phaste.py
Nov 18 2014, 10:39 PM
F917: Masterwork_From_Distant_Lands
Nov 18 2014, 10:37 PM
Subscribers
None
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
phaste: Phabricator paste tool
Forked from: https://gist.github.com/atdt/82163a594180b425ff12
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import fileinput
import hashlib
import json
import time
import urllib
import urllib2
import os
class Conduit(object):
"""A wrapper around Phabricator's Conduit API."""
def __init__(self, phab, user, cert):
self.phab = phab
self.user = user
self.cert = cert
self.credentials = None
def _do_request(self, method, data):
url = '%s/api/%s' % (self.phab, method)
req = urllib2.urlopen(url, data=urllib.urlencode(data))
resp = json.load(req)
if resp.get('error_info'):
raise RuntimeError('%(error_code)s: %(error_info)s' % resp)
return resp['result']
def _get_credentials(self):
if self.credentials is None:
token = int(time.time())
signature = hashlib.sha1(str(token) + self.cert).hexdigest()
params = {
'client': 'phaste',
'clientVersion': 0,
'user': self.user,
'authToken': token,
'authSignature': signature,
}
data = {
'params': json.dumps(params),
'output': 'json',
'__conduit__': 'true',
}
self.credentials = self._do_request('conduit.connect', data)
return self.credentials
def call(self, method, **params):
"""Call the conduit API."""
params['__conduit__'] = self._get_credentials()
data = {
'params': json.dumps(params),
'output': 'json',
}
return self._do_request(method, data)
with open(os.path.join(os.path.expanduser('~'), '.phaste.conf')) as f:
config = json.load(f)
p = Conduit(phab=config['phab'], user=config['user'], cert=config['cert'])
res = p.call('paste.create', content=''.join(fileinput.input()))
print res['uri']

Event Timeline

mmodell changed the title of this paste from untitled to Masterwork From Distant Lands.
mmodell updated the paste's language from autodetect to autodetect.
mmodell changed the title of this paste from Masterwork From Distant Lands to phaste.py.
mmodell updated the paste's language from autodetect to python.
mmodell added a project: Phabricator.