mirror of https://github.com/openspug/spug
F 发布 - 修复点击发布报错的问题 #6
parent
e9a06486b4
commit
811da44394
|
@ -5,7 +5,6 @@ from queue import Queue
|
||||||
import ipaddress
|
import ipaddress
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
import six
|
|
||||||
|
|
||||||
|
|
||||||
def human_time(date=None):
|
def human_time(date=None):
|
||||||
|
@ -74,14 +73,12 @@ class Argument(object):
|
||||||
def __init__(self, name, default=None, required=True, type=str, filter=None, help=None, nullable=False):
|
def __init__(self, name, default=None, required=True, type=str, filter=None, help=None, nullable=False):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.default = default
|
self.default = default
|
||||||
self.type = (type,)
|
self.type = type
|
||||||
self.required = required
|
self.required = required
|
||||||
self.nullable = nullable
|
self.nullable = nullable
|
||||||
self.filter = filter
|
self.filter = filter
|
||||||
self.help = help
|
self.help = help
|
||||||
if type == str:
|
if not isinstance(self.name, str):
|
||||||
self.type = six.string_types
|
|
||||||
if not isinstance(self.name, six.string_types):
|
|
||||||
raise TypeError('Argument name must be string')
|
raise TypeError('Argument name must be string')
|
||||||
if filter and not callable(self.filter):
|
if filter and not callable(self.filter):
|
||||||
raise TypeError('Argument filter is not callable')
|
raise TypeError('Argument filter is not callable')
|
||||||
|
@ -100,15 +97,17 @@ class Argument(object):
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
if self.type[0] in six.integer_types:
|
if self.type:
|
||||||
value = self.type[0](value)
|
if self.type in (list, dict) and isinstance(value, str):
|
||||||
elif self.type[0] == dict:
|
|
||||||
value = json.loads(value)
|
value = json.loads(value)
|
||||||
elif self.type[0] == bool:
|
assert isinstance(value, self.type)
|
||||||
|
elif self.type == bool and isinstance(value, str):
|
||||||
assert value.lower() in ['true', 'false']
|
assert value.lower() in ['true', 'false']
|
||||||
value = value.lower() == 'true'
|
value = value.lower() == 'true'
|
||||||
except (ValueError, AssertionError):
|
elif not isinstance(value, self.type):
|
||||||
raise ParseError(self.help or 'Type Error: %s type must be %s' % (self.name, self.type[0]))
|
value = self.type(value)
|
||||||
|
except (TypeError, ValueError, AssertionError):
|
||||||
|
raise ParseError(self.help or 'Type Error: %s type must be %s' % (self.name, self.type))
|
||||||
|
|
||||||
if self.filter:
|
if self.filter:
|
||||||
if not self.filter(value):
|
if not self.filter(value):
|
||||||
|
@ -120,7 +119,7 @@ class BaseParser(object):
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
self.args = []
|
self.args = []
|
||||||
for e in args:
|
for e in args:
|
||||||
if isinstance(e, six.string_types):
|
if isinstance(e, str):
|
||||||
e = Argument(e)
|
e = Argument(e)
|
||||||
elif not isinstance(e, Argument):
|
elif not isinstance(e, Argument):
|
||||||
raise TypeError('%r is not instance of Argument' % e)
|
raise TypeError('%r is not instance of Argument' % e)
|
||||||
|
@ -162,8 +161,9 @@ class JsonParser(BaseParser):
|
||||||
self.__data.update(post_json or {})
|
self.__data.update(post_json or {})
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if isinstance(data, six.string_types):
|
if isinstance(data, (str, bytes)):
|
||||||
self.__data = json.loads(data)
|
data = data.decode('utf-8')
|
||||||
|
self.__data = json.loads(data) if data else {}
|
||||||
else:
|
else:
|
||||||
assert hasattr(data, '__contains__')
|
assert hasattr(data, '__contains__')
|
||||||
assert hasattr(data, 'get')
|
assert hasattr(data, 'get')
|
||||||
|
|
Loading…
Reference in New Issue