Commits

Daniel Graña committed bf95ff3

group common handlers code and leave form data handling only

Comments (0)

Files changed (1)

 from mercurial import cmdutil, commands, help, util
 from urllib import urlencode
 
-
 def _paste_dpaste(content, **parameters):
     data = {'content': content, 'language': parameters['syntax'] or 'Diff'}
     if parameters['title']:
 
     if parameters['keep']:
         data['hold'] = 'on'
-    data = urlencode(data)
-
-    if parameters['url']:
-        url = parameters['url']
-    else:
-        url = pastebins['dpaste']['url']
-
-    request = urllib2.Request(url, data)
-    _authorize_request(request, **parameters)
-    response = urllib2.urlopen(request)
-    return response.geturl()
+    return data
 
 def _paste_dpaste_org(content, **parameters):
     data = {'content': content, 'lexer': parameters['syntax'] or 'diff'}
 
     # Same values used in dpaste.org form for default (a month) and forever expires.
     data['expire_options'] = '3110400000' if parameters['keep'] else '2592000'
-    data = urlencode(data)
-
-    if parameters['url']:
-        url = parameters['url']
-    else:
-        url = pastebins['dpaste.org']['url']
-
-    request = urllib2.Request(url, data)
-    _authorize_request(request, **parameters)
-    response = urllib2.urlopen(request)
-    return response.geturl()
+    return data
 
 def _paste_pastebin_dixo_net(content, **parameters):
-    data = {
+    return {
             'paste': 'Send',
             'code2': content,
             'format': parameters['syntax'] or 'diff',
             'poster': parameters.get('user', ''),
             }
 
-    url = parameters.get('url') or pastebins['dpaste.org']['url']
-    request = urllib2.Request(url, urlencode(data))
-    _authorize_request(request, **parameters)
-    response = urllib2.urlopen(request)
-    return response.geturl()
-
 
 pastebins = {
     'dpaste': { 'url': 'http://dpaste.com/api/v1/',
             b64cred = urlsafe_b64encode('%s:%s' % (cred[0], cred[2]))
             request.add_header('Authorization', 'Basic %s' % b64cred)
 
+def _do_paste(pastebin, content, parameters):
+    url = parameters.get('url') or pastebin['url']
+    data = pastebin['handler'](content, **parameters)
+    request = urllib2.Request(url, urlencode(data))
+    _authorize_request(request, **parameters)
+    response = urllib2.urlopen(request)
+    geturl = pastebin.get('geturl')
+    return geturl(response) if geturl else response.geturl()
 
 def paste(ui, repo, *fnames, **opts):
     '''send diffs from Mercurial to various pastebin websites
 
     if ui.verbose:
         ui.status('Pasting:\n%s\n' % content)
-    
+
     if not dry:
-        url = pastebins[dest]['handler'](content=content, **opts)
+        url = _do_paste(pastebins[dest], content, opts)
         ui.write('%s\n' % url)