From fbd4511ffa0fbbb84dbe0bcae0b6094e24304f34 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Wed, 27 Apr 2011 00:41:50 +0200 Subject: [PATCH] CapPaste: add method to find if a paste is suitable for the backend Basic support in pastoob added (there is no way yet to change the default options). Backends support the "public/private" requirement. --- weboob/applications/pastoob/pastoob.py | 20 +++++++++++++++++--- weboob/backends/pastealacon/backend.py | 5 +++++ weboob/backends/pastealacon/test.py | 4 ++++ weboob/backends/pastebin/backend.py | 3 +++ weboob/backends/pastebin/test.py | 5 +++++ weboob/capabilities/paste.py | 16 ++++++++++++++++ 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/weboob/applications/pastoob/pastoob.py b/weboob/applications/pastoob/pastoob.py index 106a6978..0b0a2a6c 100644 --- a/weboob/applications/pastoob/pastoob.py +++ b/weboob/applications/pastoob/pastoob.py @@ -84,12 +84,26 @@ class Pastoob(ReplApplication): with open(filename) as fp: contents = fp.read() - # chose a random backend from the ones able to satisfy our requirements - accepted_backends = [backend for backend in self.weboob.iter_backends()] - backend = choice(accepted_backends) + # get and sort the backends able to satisfy our requirements + params = self._get_params() + backends = {} + for backend in self.weboob.iter_backends(): + score = backend.can_post(**params) + if score: + backends.setdefault(score, []).append(backend) + # select a random backend from the best scores + if len(backends): + backend = choice(backends[max(backends.keys())]) + else: + print >>sys.stderr, 'No suitable backend found.' + return 1 p = backend.new_paste(_id=None) + p.public = params.get('public') p.title = os.path.basename(filename) p.contents = contents backend.post_paste(p) print 'Successfuly posted paste: %s' % p.page_url + + def _get_params(self): + return {'public': True} diff --git a/weboob/backends/pastealacon/backend.py b/weboob/backends/pastealacon/backend.py index 8539104e..619a9986 100644 --- a/weboob/backends/pastealacon/backend.py +++ b/weboob/backends/pastealacon/backend.py @@ -43,6 +43,11 @@ class PastealaconBackend(BaseBackend, ICapPaste): def new_paste(self, *args, **kwargs): return PastealaconPaste(*args, **kwargs) + def can_post(self, public=None): + if public is False: + return 0 + return 1 + def get_paste(self, _id): with self.browser: return self.browser.get_paste(_id) diff --git a/weboob/backends/pastealacon/test.py b/weboob/backends/pastealacon/test.py index 8418eb93..b43000a1 100644 --- a/weboob/backends/pastealacon/test.py +++ b/weboob/backends/pastealacon/test.py @@ -79,3 +79,7 @@ class PastealaconTest(BackendTest): # same even with correct domain (IDs are numeric) assert self.backend.get_paste('http://pastealacon.com/nJG9ZFG8') is None assert self.backend.get_paste('nJG9ZFG8') is None + + def test_can_post(self): + assert 0 == self.backend.can_post(public=False) + assert 1 == self.backend.can_post(public=True) diff --git a/weboob/backends/pastebin/backend.py b/weboob/backends/pastebin/backend.py index 414ae1ea..107bbe78 100644 --- a/weboob/backends/pastebin/backend.py +++ b/weboob/backends/pastebin/backend.py @@ -47,6 +47,9 @@ class PastebinBackend(BaseBackend, ICapPaste): def new_paste(self, *args, **kwargs): return PastebinPaste(*args, **kwargs) + def can_post(self, public=None): + return 1 + def get_paste(self, _id): with self.browser: return self.browser.get_paste(_id) diff --git a/weboob/backends/pastebin/test.py b/weboob/backends/pastebin/test.py index 772d0e41..80eabe37 100644 --- a/weboob/backends/pastebin/test.py +++ b/weboob/backends/pastebin/test.py @@ -77,3 +77,8 @@ class PastebinTest(BackendTest): def test_checkurl(self): # call with an URL we can't handle with this backend assert self.backend.get_paste('http://pastealacon.com/1') is None + + def test_can_post(self): + assert self.backend.can_post(public=None) > 0 + assert self.backend.can_post(public=True) > 0 + assert self.backend.can_post(public=False) > 0 diff --git a/weboob/capabilities/paste.py b/weboob/capabilities/paste.py index 51714ad8..d2136be4 100644 --- a/weboob/capabilities/paste.py +++ b/weboob/capabilities/paste.py @@ -64,6 +64,22 @@ class ICapPaste(IBaseCap): """ raise NotImplementedError() + def can_post(self, public=None): + """ + Checks if the paste can be pasted by this backend. + Some properties are considered required (public/private, max_age) while others + are just bonuses (language). + + public: True must be public, False must be private, None do not care + + A score of 0 means the backend is not suitable. + A score of 1 means the backend is suitable. + Higher scores means it is more suitable than others with a lower score. + + @return int Score + """ + raise NotImplementedError() + def get_paste(self, url): """ Get a Paste from an ID or URL.