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.
This commit is contained in:
parent
9022435e49
commit
fbd4511ffa
6 changed files with 50 additions and 3 deletions
|
|
@ -84,12 +84,26 @@ class Pastoob(ReplApplication):
|
||||||
with open(filename) as fp:
|
with open(filename) as fp:
|
||||||
contents = fp.read()
|
contents = fp.read()
|
||||||
|
|
||||||
# chose a random backend from the ones able to satisfy our requirements
|
# get and sort the backends able to satisfy our requirements
|
||||||
accepted_backends = [backend for backend in self.weboob.iter_backends()]
|
params = self._get_params()
|
||||||
backend = choice(accepted_backends)
|
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 = backend.new_paste(_id=None)
|
||||||
|
p.public = params.get('public')
|
||||||
p.title = os.path.basename(filename)
|
p.title = os.path.basename(filename)
|
||||||
p.contents = contents
|
p.contents = contents
|
||||||
backend.post_paste(p)
|
backend.post_paste(p)
|
||||||
print 'Successfuly posted paste: %s' % p.page_url
|
print 'Successfuly posted paste: %s' % p.page_url
|
||||||
|
|
||||||
|
def _get_params(self):
|
||||||
|
return {'public': True}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,11 @@ class PastealaconBackend(BaseBackend, ICapPaste):
|
||||||
def new_paste(self, *args, **kwargs):
|
def new_paste(self, *args, **kwargs):
|
||||||
return PastealaconPaste(*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):
|
def get_paste(self, _id):
|
||||||
with self.browser:
|
with self.browser:
|
||||||
return self.browser.get_paste(_id)
|
return self.browser.get_paste(_id)
|
||||||
|
|
|
||||||
|
|
@ -79,3 +79,7 @@ class PastealaconTest(BackendTest):
|
||||||
# same even with correct domain (IDs are numeric)
|
# same even with correct domain (IDs are numeric)
|
||||||
assert self.backend.get_paste('http://pastealacon.com/nJG9ZFG8') is None
|
assert self.backend.get_paste('http://pastealacon.com/nJG9ZFG8') is None
|
||||||
assert self.backend.get_paste('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)
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,9 @@ class PastebinBackend(BaseBackend, ICapPaste):
|
||||||
def new_paste(self, *args, **kwargs):
|
def new_paste(self, *args, **kwargs):
|
||||||
return PastebinPaste(*args, **kwargs)
|
return PastebinPaste(*args, **kwargs)
|
||||||
|
|
||||||
|
def can_post(self, public=None):
|
||||||
|
return 1
|
||||||
|
|
||||||
def get_paste(self, _id):
|
def get_paste(self, _id):
|
||||||
with self.browser:
|
with self.browser:
|
||||||
return self.browser.get_paste(_id)
|
return self.browser.get_paste(_id)
|
||||||
|
|
|
||||||
|
|
@ -77,3 +77,8 @@ class PastebinTest(BackendTest):
|
||||||
def test_checkurl(self):
|
def test_checkurl(self):
|
||||||
# call with an URL we can't handle with this backend
|
# call with an URL we can't handle with this backend
|
||||||
assert self.backend.get_paste('http://pastealacon.com/1') is None
|
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
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,22 @@ class ICapPaste(IBaseCap):
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
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):
|
def get_paste(self, url):
|
||||||
"""
|
"""
|
||||||
Get a Paste from an ID or URL.
|
Get a Paste from an ID or URL.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue