paste* backends: Accept an URL or an ID

Like most other backends, with the id2url decorator.
This commit is contained in:
Laurent Bachelier 2011-04-21 18:29:18 +02:00
commit dc35df83ef
6 changed files with 69 additions and 39 deletions

View file

@ -43,22 +43,26 @@ class PastebinBackend(BaseBackend, ICapPaste):
)
def get_paste(self, _id):
return PastebinPaste(_id)
with self.browser:
return self.browser.get_paste(_id)
def fill_paste(self, paste, fields):
# if we only want the contents
if fields == ['contents']:
if paste.contents is NotLoaded:
contents = self.browser.get_contents(paste.id)
with self.browser:
contents = self.browser.get_contents(paste.id)
paste.contents = contents
elif fields:
self.browser.fill_paste(paste)
with self.browser:
self.browser.fill_paste(paste)
return paste
def post_paste(self, paste):
if self.config['apikey']:
self.browser.api_post_paste(self.config['apikey'], paste)
else:
self.browser.post_paste(paste)
with self.browser:
if self.config['apikey']:
self.browser.api_post_paste(self.config['apikey'], paste)
else:
self.browser.post_paste(paste)
OBJECTS = {PastebinPaste: fill_paste}

View file

@ -19,10 +19,12 @@
from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound
from weboob.tools.browser.decorators import id2url
from weboob.capabilities.paste import PasteNotFound
from .pages import PastePage, PostPage
from .paste import PastebinPaste
import urllib
import re
@ -51,6 +53,11 @@ class PastebinBrowser(BaseBrowser):
except BrowserHTTPNotFound:
raise PasteNotFound()
@id2url(PastebinPaste.id2url)
def get_paste(self, url):
_id = re.match(self.PASTE_URL, url).groupdict()['id']
return PastebinPaste(_id)
def get_contents(self, _id):
"""
Get the contents from the raw URL

View file

@ -26,19 +26,20 @@ class PastebinTest(BackendTest):
BACKEND = 'pastebin'
def test_get_paste(self):
# html method
p = self.backend.get_paste('7HmXwzyt')
self.backend.fillobj(p, ['title'])
assert p.title == 'plop'
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
for _id in ('7HmXwzyt', 'http://pastebin.com/7HmXwzyt'):
# html method
p = self.backend.get_paste(_id)
self.backend.fillobj(p, ['title'])
assert p.title == 'plop'
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
# raw method
p = self.backend.get_paste('7HmXwzyt')
self.backend.fillobj(p, ['contents'])
assert p.title is NotLoaded
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
# raw method
p = self.backend.get_paste(_id)
self.backend.fillobj(p, ['contents'])
assert p.title is NotLoaded
assert p.page_url == 'http://pastebin.com/7HmXwzyt'
assert p.contents == 'prout'
def test_post(self):
p = PastebinPaste(None, title='ouiboube', contents='Weboob Test')
@ -60,10 +61,11 @@ class PastebinTest(BackendTest):
assert p2.contents == p1.contents
def test_notfound(self):
# html method
p = self.backend.get_paste('weboooooooooooooooooooooooooob')
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title'])
for _id in ('weboooooooooooooooooooooooooob', 'http://pastebin.com/weboooooooooooooooooooooooooob'):
# html method
p = self.backend.get_paste(_id)
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['title'])
# raw method
p = self.backend.get_paste('weboooooooooooooooooooooooooob')
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents'])
# raw method
p = self.backend.get_paste(_id)
self.assertRaises(PasteNotFound, self.backend.fillobj, p, ['contents'])