Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_parameter_types(parameter_types=None):
if not parameter_types:
parameter_types = []
parameter_types = ['fair.parameter'] + parameter_types
types = {}
for package_name in parameter_types:
exec('import %s as package' % package_name)
parameter_package = locals()['package']
for item in dir(parameter_package):
# flask's request and session(werkzeug.local.LocalProxy) raise RuntimeError
if item in ['request', 'session']:
continue
parameter = getattr(parameter_package, item)
try:
if issubclass(parameter, Param):
if parameter.__name__ not in types:
types[parameter.__name__] = parameter
except TypeError:
pass # Some object throw TypeError in issubclass
return types
param = {'name': items[-1], 'type': param_type, 'requisite': False, 'description': content}
if len(items) > 2 and items[1] == '*':
param['requisite'] = True
self.param_not_null.append(items[-1])
else:
self.param_allow_null.append(items[-1])
self.param_list.append(param)
self.param_dict[items[-1]] = param
self.param_default[items[-1]] = None
self.param_types[items[-1]] = param_type
if isinstance(param['type'], List):
self.__code_set(param_type.type.error_code, param_type.type.description, 'type')
self.__code_set(param_type.error_code,
param_type.description % param_type.type.__name__, 'type')
elif param['type'] != Param:
self.__code_set(param_type.error_code, param_type.description, 'type')
else:
setattr(self, name, content)
def structure(cls, view, value):
if view.application_json:
if type(value) is bool:
return value
else:
raise Exception()
else:
if value == 'true':
return True
elif value == 'false':
return False
else:
raise Exception()
class Int(Param):
""" Int type parameter
"""
error_code = 'param_type_error_int'
description = 'Parameter must be Integer'
@classmethod
def structure(cls, view, value):
"""conversion value to int
:param value: parameter value
:returns: parameter value
:rtype int:
"""
return int(value)
# self.__name__ = 'List[%s]' % _type.__name__
self.__name__ = List.__name__
def structure(self, view, value):
if type(value) is not list:
return self.error_code
if self.type:
for item in value:
if item:
error_code = self.type.structure(item)
if error_code:
return error_code
class Mail(Param):
""" Parameter that is Email address
:cvar str error_code: Error code
:cvar str description: Parameter description
"""
error_code = 'param_type_error_email'
description = 'Parameter must be email address'
@classmethod
def structure(cls, view, value):
if type(value) is not str:
return cls.code
if value and not re.match("([^@|\s]+@[^@]+\.[^@|\s]+)", value):
return cls.code
class Str(Param):
""" String type parameter
"""
error_code = 'param_type_error_str'
description = 'Parameter must be String'
@classmethod
def structure(cls, view, value):
if request.json is not None and type(value) is not str:
raise Exception()
return value
class Bool(Param):
""" Boolean type parameter
"""
error_code = 'param_type_error_bool'
description = 'Parameter must be true or false'
@classmethod
def structure(cls, view, value):
if view.application_json:
if type(value) is bool:
return value
else:
raise Exception()
else:
if value == 'true':
return True
elif value == 'false':
raise Exception('%s.%s use parameter %s type that not support %s method.' %
(self.__name__, method.__name__, param_type.__name__, method.__name__.upper()))
param = {'name': items[-1], 'type': param_type, 'requisite': False, 'description': content}
if len(items) > 2 and items[1] == '*':
param['requisite'] = True
self.param_not_null.append(items[-1])
else:
self.param_allow_null.append(items[-1])
self.param_list.append(param)
self.param_dict[items[-1]] = param
self.param_default[items[-1]] = None
self.param_types[items[-1]] = param_type
if isinstance(param['type'], List):
self.__meta_code_set(param_type.type.error_code, param_type.type.description, 'type')
self.__meta_code_set(param_type.error_code, param_type.description % param_type.type.__name__, 'type')
elif param['type'] != Param:
self.__meta_code_set(param_type.error_code, param_type.description, 'type')
else:
setattr(self, name, content)
def set_parameter_types(app, parameter_types):
if not parameter_types:
parameter_types = []
parameter_types = ['fair.parameter'] + parameter_types
types = {}
for package_name in parameter_types:
exec('import %s as package' % package_name)
parameter_package = locals()['package']
for item in dir(parameter_package):
# flask's request and session(werkzeug.local.LocalProxy) raise RuntimeError
if item in ['request', 'session']:
continue
parameter = getattr(parameter_package, item)
try:
if issubclass(parameter, Param):
if parameter.__name__ not in types:
types[parameter.__name__] = parameter
except TypeError:
pass # Some object throw TypeError in issubclass
app.config['parameter_types'] = types
"""
error_code = 'param_type_error_int'
description = 'Parameter must be Integer'
@classmethod
def structure(cls, view, value):
"""conversion value to int
:param value: parameter value
:returns: parameter value
:rtype int:
"""
return int(value)
class Float(Param):
""" Float type parameter
:cvar str error_code: Error code
:cvar str description: Parameter description
"""
error_code = 'param_type_error_float'
description = 'Parameter must be Float'
@classmethod
def structure(cls, view, value):
return float(value)
class List(Param):
""" List type parameter
class Float(Param):
""" Float type parameter
:cvar str error_code: Error code
:cvar str description: Parameter description
"""
error_code = 'param_type_error_float'
description = 'Parameter must be Float'
@classmethod
def structure(cls, view, value):
return float(value)
class List(Param):
""" List type parameter
POST (application/json) only
:cvar str error_code: Error code
:cvar str description: Parameter description
"""
error_code = 'param_type_error_list'
description = 'Parameter must be List[%s]'
support = ['POST']
has_sub_type = True
def __init__(self, _type=None):
self.type = _type
# self.__name__ = 'List[%s]' % _type.__name__
self.__name__ = List.__name__
for item in dir(parameter_package):
# flask's request and session(werkzeug.local.LocalProxy) raise RuntimeError
if item in ['request', 'session']:
continue
parameter = getattr(parameter_package, item)
try:
if issubclass(parameter, Param):
if parameter.__name__ not in types:
types[parameter.__name__] = parameter
except TypeError:
pass # Some object throw TypeError in issubclass
return types
class Str(Param):
""" String type parameter
"""
error_code = 'param_type_error_str'
description = 'Parameter must be String'
@classmethod
def structure(cls, view, value):
if request.json is not None and type(value) is not str:
raise Exception()
return value
class Bool(Param):
""" Boolean type parameter
"""
error_code = 'param_type_error_bool'