Daniel Graña avatar Daniel Graña committed b5e527b

remove "pastebins" global dict

Comments (0)

Files changed (1)

 
 
 def _paste_dpaste(content, syntax, title, user, keep, **kw):
-    return {
-            'content': content, 
-            'language': syntax or 'Diff',
-            'title': title,
-            'poster': user[:30],
-            'hold': 'on' if keep else 'off',
-            }
+    return ('http://dpaste.com/api/v1/', {
+        'content': content, 
+        'language': syntax or 'Diff',
+        'title': title,
+        'poster': user[:30],
+        'hold': 'on' if keep else 'off',
+        })
 
 def _paste_dpaste_org(content, syntax, title, user, keep, **kw):
-    return {
-            'content': content,
-            'lexer': syntax or 'diff',
-            'title': title,
-            'poster': user[:30],
-            'expire_options': '3110400000' if keep else '2592000',
-            }
+    return ('http://dpaste.org/', {
+        'content': content,
+        'lexer': syntax or 'diff',
+        'title': title,
+        'author': user[:30],
+        'expire_options': '3110400000' if keep else '2592000',
+        })
 
 def _paste_pastebin_dixo_net(content, syntax, user, keep, **kw):
-    return {
-            'paste': 'Send',
-            'code2': content,
-            'format': syntax or 'diff',
-            'poster': user,
-            'expiry': 'f' if keep else 'm',
-            }
+    return ('http://pastebin.dixo.net/', {
+        'paste': 'Send',
+        'code2': content,
+        'format': syntax or 'diff',
+        'poster': user,
+        'expiry': 'f' if keep else 'm',
+        })
 
-
-pastebins = {
-    'dpaste': { 'url': 'http://dpaste.com/api/v1/',
-                'parameters': {
-                    'required': ['content'],
-                    'optional': ['title', 'user', 'keep'], },
-                'handler': _paste_dpaste,
-    },
-    'dpaste.org': { 'url': 'http://dpaste.org/',
-                'parameters': {
-                    'required': ['content'],
-                    'optional': ['title', 'user', 'keep'], },
-                'handler': _paste_dpaste_org,
-    },
-    'pastebin.dixo.net': { 'url': 'http://pastebin.dixo.net/',
-                'parameters': {
-                    'required': ['content'],
-                    'optional': ['user', 'keep'], },
-                'handler': _paste_pastebin_dixo_net,
-    },
-}
+def _paste(handler, content, opts):
+    default_url, data = handler(content, **opts)
+    url = opts['url'] or default_url
+    request = urllib2.Request(url, urlencode(data))
+    _authorize_request(request, **opts)
+    response = urllib2.urlopen(request)
+    return response.geturl()
 
 def _authorize_request(request, httpauth=None, usenetrc=None, **kwargs):
     """Sets basic authorization header using httpauth flag or netrc if enabled"""
             b64cred = urlsafe_b64encode('%s:%s' % (cred[0], cred[2]))
             request.add_header('Authorization', 'Basic %s' % b64cred)
 
-def _do_paste(pastebin, content, opts):
-    url = opts['url'] or pastebin['url']
-    data = pastebin['handler'](content, **opts)
-    request = urllib2.Request(url, urlencode(data))
-    _authorize_request(request, **opts)
-    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
     dry = opts.pop('dry_run')
     if not dest:
         dest = 'dpaste'
-    if dest not in pastebins:
+
+    handler = globals().get('_paste_' + dest.replace('.', '_'))
+    if not handler:
         raise util.Abort('unknown pastebin (see "hg help pastebins")!')
 
     if not opts['user']:
         ui.status('Pasting:\n%s\n' % content)
 
     if not dry:
-        url = _do_paste(pastebins[dest], content, opts)
+        url = _paste(handler, content, opts)
         ui.write('%s\n' % url)
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.