CapPaste: check the contents in can_post
For pastealacon, it is used to check for encoding compatibility.
This commit is contained in:
parent
56fe4448d3
commit
0d2148bd84
6 changed files with 32 additions and 22 deletions
|
|
@ -91,7 +91,7 @@ class Pastoob(ReplApplication):
|
||||||
params = self._get_params()
|
params = self._get_params()
|
||||||
backends = {}
|
backends = {}
|
||||||
for backend in self.weboob.iter_backends():
|
for backend in self.weboob.iter_backends():
|
||||||
score = backend.can_post(**params)
|
score = backend.can_post(contents, **params)
|
||||||
if score:
|
if score:
|
||||||
backends.setdefault(score, []).append(backend)
|
backends.setdefault(score, []).append(backend)
|
||||||
# select a random backend from the best scores
|
# select a random backend from the best scores
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,11 @@ class PastealaconBackend(BaseBackend, BasePasteBackend):
|
||||||
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, max_age=None):
|
def can_post(self, contents, public=None, max_age=None):
|
||||||
|
try:
|
||||||
|
contents.encode(self.browser.ENCODING)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
return 0
|
||||||
if public is False:
|
if public is False:
|
||||||
return 0
|
return 0
|
||||||
if max_age is not None:
|
if max_age is not None:
|
||||||
|
|
|
||||||
|
|
@ -81,11 +81,13 @@ class PastealaconTest(BackendTest):
|
||||||
assert self.backend.get_paste('nJG9ZFG8') is None
|
assert self.backend.get_paste('nJG9ZFG8') is None
|
||||||
|
|
||||||
def test_can_post(self):
|
def test_can_post(self):
|
||||||
assert 0 == self.backend.can_post(public=False)
|
assert 0 == self.backend.can_post('hello', public=False)
|
||||||
assert 1 == self.backend.can_post(public=True)
|
assert 1 == self.backend.can_post('hello', public=True)
|
||||||
assert 0 == self.backend.can_post(public=True, max_age=600)
|
assert 0 == self.backend.can_post('hello', public=True, max_age=600)
|
||||||
assert 1 == self.backend.can_post(public=True, max_age=3600*24)
|
assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24)
|
||||||
assert 1 == self.backend.can_post(public=True, max_age=3600*24*3)
|
assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24*3)
|
||||||
assert 1 == self.backend.can_post(public=True, max_age=False)
|
assert 1 == self.backend.can_post('hello', public=True, max_age=False)
|
||||||
assert 1 == self.backend.can_post(public=None, max_age=False)
|
assert 1 == self.backend.can_post('hello', public=None, max_age=False)
|
||||||
assert 1 == self.backend.can_post(public=True, max_age=3600*24*40)
|
assert 1 == self.backend.can_post('hello', public=True, max_age=3600*24*40)
|
||||||
|
assert 1 == self.backend.can_post(u'héhé', public=True)
|
||||||
|
assert 0 == self.backend.can_post(u'hello ♥', public=True)
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ class PastebinBackend(BaseBackend, BasePasteBackend):
|
||||||
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, max_age=None):
|
def can_post(self, contents, public=None, max_age=None):
|
||||||
if max_age is not None:
|
if max_age is not None:
|
||||||
if self.get_closest_expiration(max_age) is None:
|
if self.get_closest_expiration(max_age) is None:
|
||||||
return 0
|
return 0
|
||||||
|
|
|
||||||
|
|
@ -79,12 +79,14 @@ class PastebinTest(BackendTest):
|
||||||
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):
|
def test_can_post(self):
|
||||||
assert self.backend.can_post(public=None) > 0
|
assert self.backend.can_post('hello', public=None) > 0
|
||||||
assert self.backend.can_post(public=True) > 0
|
assert self.backend.can_post('hello', public=True) > 0
|
||||||
assert self.backend.can_post(public=False) > 0
|
assert self.backend.can_post('hello', public=False) > 0
|
||||||
assert self.backend.can_post(public=True, max_age=600) > 0
|
assert self.backend.can_post('hello', public=True, max_age=600) > 0
|
||||||
assert self.backend.can_post(public=True, max_age=3600*24) > 0
|
assert self.backend.can_post('hello', public=True, max_age=3600*24) > 0
|
||||||
assert self.backend.can_post(public=True, max_age=3600*24*3) > 0
|
assert self.backend.can_post('hello', public=True, max_age=3600*24*3) > 0
|
||||||
assert self.backend.can_post(public=True, max_age=False) > 0
|
assert self.backend.can_post('hello', public=True, max_age=False) > 0
|
||||||
assert self.backend.can_post(public=None, max_age=False) > 0
|
assert self.backend.can_post('hello', public=None, max_age=False) > 0
|
||||||
assert self.backend.can_post(public=True, max_age=3600*24*40) > 0
|
assert self.backend.can_post('hello', public=True, max_age=3600*24*40) > 0
|
||||||
|
assert self.backend.can_post(u'héhé', public=True) > 0
|
||||||
|
assert self.backend.can_post(u'hello ♥', public=True) > 0
|
||||||
|
|
|
||||||
|
|
@ -64,13 +64,15 @@ class ICapPaste(IBaseCap):
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def can_post(self, public=None, max_age=None):
|
def can_post(self, contents, public=None, max_age=None):
|
||||||
"""
|
"""
|
||||||
Checks if the paste can be pasted by this backend.
|
Checks if the paste can be pasted by this backend.
|
||||||
Some properties are considered required (public/private, max_age) while others
|
Some properties are considered required (public/private, max_age) while others
|
||||||
are just bonuses (language).
|
are just bonuses (language).
|
||||||
|
|
||||||
public: True must be public, False must be private, None do not care
|
contents: Can be used to check encodability, maximum length, etc.
|
||||||
|
public: True must be public, False must be private, None do not care.
|
||||||
|
max_age: Maximum time to live in seconds.
|
||||||
|
|
||||||
A score of 0 means the backend is not suitable.
|
A score of 0 means the backend is not suitable.
|
||||||
A score of 1 means the backend is suitable.
|
A score of 1 means the backend is suitable.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue