2016-04-15 15:54:21 +00:00
|
|
|
from szurubooru import errors
|
2016-05-05 11:40:32 +00:00
|
|
|
from szurubooru.func import net
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2016-06-05 08:39:56 +00:00
|
|
|
def _lower_first(source):
|
|
|
|
return source[0].lower() + source[1:]
|
2016-06-05 08:30:10 +00:00
|
|
|
|
|
|
|
def _param_wrapper(func):
|
|
|
|
def wrapper(self, name, required=False, default=None, **kwargs):
|
2016-08-14 10:35:14 +00:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
if name in self._params:
|
|
|
|
value = self._params[name]
|
2016-06-05 08:30:10 +00:00
|
|
|
try:
|
|
|
|
value = func(self, value, **kwargs)
|
2016-06-05 08:39:56 +00:00
|
|
|
except errors.InvalidParameterError as ex:
|
2016-06-05 08:30:10 +00:00
|
|
|
raise errors.InvalidParameterError(
|
|
|
|
'Parameter %r is invalid: %s' % (
|
2016-06-05 08:39:56 +00:00
|
|
|
name, _lower_first(str(ex))))
|
2016-06-05 08:30:10 +00:00
|
|
|
return value
|
|
|
|
if not required:
|
|
|
|
return default
|
|
|
|
raise errors.MissingRequiredParameterError(
|
|
|
|
'Required parameter %r is missing.' % name)
|
|
|
|
return wrapper
|
|
|
|
|
2016-08-14 10:35:14 +00:00
|
|
|
class Context():
|
|
|
|
# pylint: disable=too-many-arguments
|
|
|
|
def __init__(self, method, url, headers=None, params=None, files=None):
|
|
|
|
self.method = method
|
|
|
|
self.url = url
|
|
|
|
self._headers = headers or {}
|
|
|
|
self._params = params or {}
|
|
|
|
self._files = files or {}
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2016-08-14 10:35:14 +00:00
|
|
|
# provided by middleware
|
|
|
|
# self.session = None
|
|
|
|
# self.user = None
|
|
|
|
|
|
|
|
def has_header(self, name):
|
|
|
|
return name in self._headers
|
|
|
|
|
|
|
|
def get_header(self, name):
|
|
|
|
return self._headers.get(name, None)
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2016-04-30 21:17:08 +00:00
|
|
|
def has_file(self, name):
|
2016-08-14 10:35:14 +00:00
|
|
|
return name in self._files or name + 'Url' in self._params
|
2016-04-30 21:17:08 +00:00
|
|
|
|
|
|
|
def get_file(self, name, required=False):
|
2016-08-14 10:35:14 +00:00
|
|
|
if name in self._files:
|
|
|
|
return self._files[name]
|
|
|
|
if name + 'Url' in self._params:
|
|
|
|
return net.download(self._params[name + 'Url'])
|
2016-04-30 21:17:08 +00:00
|
|
|
if not required:
|
|
|
|
return None
|
|
|
|
raise errors.MissingRequiredFileError(
|
|
|
|
'Required file %r is missing.' % name)
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2016-08-14 10:35:14 +00:00
|
|
|
def has_param(self, name):
|
|
|
|
return name in self._params
|
|
|
|
|
2016-06-05 08:30:10 +00:00
|
|
|
@_param_wrapper
|
|
|
|
def get_param_as_list(self, value):
|
|
|
|
if not isinstance(value, list):
|
2016-08-14 10:35:14 +00:00
|
|
|
if ',' in value:
|
|
|
|
return value.split(',')
|
2016-06-05 08:30:10 +00:00
|
|
|
return [value]
|
|
|
|
return value
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2016-06-05 08:30:10 +00:00
|
|
|
@_param_wrapper
|
|
|
|
def get_param_as_string(self, value):
|
|
|
|
if isinstance(value, list):
|
2016-04-15 15:54:21 +00:00
|
|
|
try:
|
2016-06-05 08:30:10 +00:00
|
|
|
value = ','.join(value)
|
|
|
|
except:
|
|
|
|
raise errors.InvalidParameterError('Expected simple string.')
|
|
|
|
return value
|
|
|
|
|
|
|
|
# pylint: disable=redefined-builtin
|
|
|
|
@_param_wrapper
|
|
|
|
def get_param_as_int(self, value, min=None, max=None):
|
|
|
|
try:
|
|
|
|
value = int(value)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
raise errors.InvalidParameterError(
|
|
|
|
'The value must be an integer.')
|
|
|
|
if min is not None and value < min:
|
|
|
|
raise errors.InvalidParameterError(
|
|
|
|
'The value must be at least %r.' % min)
|
|
|
|
if max is not None and value > max:
|
|
|
|
raise errors.InvalidParameterError(
|
|
|
|
'The value may not exceed %r.' % max)
|
|
|
|
return value
|
|
|
|
|
|
|
|
@_param_wrapper
|
|
|
|
def get_param_as_bool(self, value):
|
|
|
|
value = str(value).lower()
|
|
|
|
if value in ['1', 'y', 'yes', 'yeah', 'yep', 'yup', 't', 'true']:
|
|
|
|
return True
|
|
|
|
if value in ['0', 'n', 'no', 'nope', 'f', 'false']:
|
|
|
|
return False
|
|
|
|
raise errors.InvalidParameterError('The value must be a boolean value.')
|