示例#1
0
def convert2obj(cls, json_obj):
    """ 将JSON对象转换为指定模型类的对象 """
    if isinstance(json_obj, dict):
        for k, v in list(json_obj.items()):
            if v is None or v == str:
                json_obj.pop(k)
    obj = convertJSON2OBJ(cls, json_obj)
    fields = [f for f in cls.__dict__.keys() if f[0] != '_']
    for f in fields:
        if f not in obj.__dict__:
            obj.__dict__[f] = None
    return obj
示例#2
0
    }]
}


class Project(object):
    seq = int
    name = str


class User(object):
    userid = int
    username = str
    projects = [Project]


c = convertJSON2OBJ(User, doc)
print("JSON original")
print("*************")
print(json.dumps(doc, sort_keys=True, indent=2, separators=(',', ': ')))
print("")
print("Python obj     ")
print("***************")
print("User data      ")
print("---------------")
print("userid   : %d " % c.userid)
print("username : %s " % c.username)
print("")
print("User's projects")
print("---------------")
for project in c.projects:
    print("\tprojects.seq  : %d " % project.seq)
示例#3
0
    def _exe(self, method):
        """ Executes the python function for the Rest Service """
        request_path = self.request.path
        path = request_path.split('/')
        services_and_params = list(filter(lambda x: x != '', path))
        content_type = None
        if 'Content-Type' in self.request.headers.keys():
            content_type = self.request.headers['Content-Type']

        # Get all funcion names configured in the class RestHandler
        functions = list(
            filter(
                lambda op: hasattr(getattr(self, op), '_service_name') == True
                and inspect.ismethod(getattr(self, op)) == True, dir(self)))
        # Get all http methods configured in the class RestHandler
        http_methods = list(
            map(lambda op: getattr(getattr(self, op), '_method'), functions))

        if method not in http_methods:
            raise tornado.web.HTTPError(
                405, 'The service not have %s verb' % method)
        if path[1] not in list(
                op._path.split('/')[1]
                for op in list(map(lambda o: getattr(self, o), functions))):
            raise tornado.web.HTTPError(404,
                                        '404 Not Found {}'.format(path[1]))
        for operation in list(map(lambda op: getattr(self, op), functions)):
            service_name = getattr(operation, '_service_name')
            service_params = getattr(operation, '_service_params')
            # If the _types is not specified, assumes str types for the params
            params_types = getattr(operation,
                                   "_types") or [str] * len(service_params)
            params_types = params_types + [str] * (len(service_params) -
                                                   len(params_types))
            produces = getattr(operation, '_produces')
            consumes = getattr(operation, '_consumes')
            services_from_request = list(
                filter(lambda x: x in path, service_name))
            query_params = getattr(operation, '_query_params')
            manual_response = getattr(operation, '_manual_response')
            catch_fire = getattr(operation, '_catch_fire')

            if operation._method == self.request.method and service_name == services_from_request and len(
                    service_params) + len(service_name) == len(
                        services_and_params):
                try:
                    params_values = self._find_params_value_of_url(
                        service_name, request_path
                    ) + self._find_params_value_of_arguments(operation)
                    p_values = self._convert_params_values(
                        params_values, params_types)

                    if consumes == None and content_type != None:
                        consumes = content_type
                    if consumes == mediatypes.APPLICATION_XML:
                        param_obj = None
                        body = self.request.body
                        if sys.version_info > (3, ):
                            body = str(body, 'utf-8')
                        if len(body) > 0 and params_types[0] in [str]:
                            param_obj = xml.dom.minidom.parseString(body)
                        elif len(body) > 0:
                            param_obj = convertXML2OBJ(
                                params_types[0],
                                xml.dom.minidom.parseString(
                                    body).documentElement)
                        if param_obj != None:
                            p_values.append(param_obj)
                    elif consumes == mediatypes.APPLICATION_JSON:
                        param_obj = None
                        body = self.request.body
                        if sys.version_info > (3, ):
                            body = str(body, 'utf-8')
                        if len(body) > 0 and params_types[0] in [dict, str]:
                            param_obj = json.loads(body)
                        elif len(body) > 0:
                            param_obj = convertJSON2OBJ(
                                params_types[0], json.loads(body))
                        if param_obj != None:
                            p_values.append(param_obj)
                    elif consumes == mediatypes.TEXT_PLAIN:
                        body = self.request.body
                        if len(body) >= 1:
                            if sys.version_info > (3, ):
                                body = str(body, 'utf-8')
                            if isinstance(body, str):
                                param_obj = body
                            else:
                                param_obj = convertJSON2OBJ(
                                    params_types[0], json.loads(body))
                            p_values.append(param_obj)

                    response = operation(*p_values)

                    if response == None:
                        return

                    if produces != None:
                        self.set_header('Content-Type', produces)

                    if manual_response:
                        return

                    if (produces == mediatypes.APPLICATION_JSON or produces
                            == None) and hasattr(response, '__module__'):
                        response = convert2JSON(response)
                    elif produces == mediatypes.APPLICATION_XML and hasattr(
                            response, '__module__') and not isinstance(
                                response, xml.dom.minidom.Document):
                        response = convert2XML(response)

                    if produces == None and (isinstance(response, dict)
                                             or isinstance(response, str)):
                        self.write(response)
                        self.finish()
                    elif produces == None and isinstance(response, list):
                        self.write(json.dumps(response))
                        self.finish()
                    elif produces == mediatypes.TEXT_PLAIN and isinstance(
                            response, str):
                        self.write(response)
                        self.finish()
                    elif produces == mediatypes.APPLICATION_JSON and isinstance(
                            response, dict):
                        self.write(response)
                        self.finish()
                    elif produces == mediatypes.APPLICATION_JSON and isinstance(
                            response, list):
                        self.write(json.dumps(response))
                        self.finish()
                    elif produces in [
                            mediatypes.APPLICATION_XML, mediatypes.TEXT_XML
                    ] and isinstance(response, xml.dom.minidom.Document):
                        self.write(response.toxml())
                        self.finish()
                    else:
                        self.gen_http_error(
                            500,
                            'Internal Server Error : response is not %s document'
                            % produces)
                        if catch_fire == True:
                            raise PyRestfulException(
                                'Internal Server Error : response is not %s document'
                                % produces)
                except Exception as detail:
                    self.gen_http_error(500,
                                        'Internal Server Error : %s' % detail)
                    if catch_fire == True:
                        raise PyRestfulException(detail)
示例#4
0
    def _exe(self, method):
        ''' Executes the python function for the Rest Service '''
        request_path = self.request.path
        path = request_path.split('/')
        services_and_params = [p for p in path if p != '']
        content_type = None
        if 'Content-Type' in self.request.headers.keys():
            content_type = self.request.headers['Content-Type']

        # Get all funcion names configured in the class RestHandler
        functions = [
            op for op in dir(self)
            if hasattr(getattr(self, op), '_service_name')
            and inspect.ismethod(getattr(self, op))
        ]

        # Get all http methods configured in the class RestHandler
        http_methods = [
            getattr(getattr(self, op), '_method') for op in functions
        ]

        if method not in http_methods:
            raise tornado.web.HTTPError(
                405, 'The service not have %s verb' % method)
        for operation in [getattr(self, op) for op in functions]:
            service_name = getattr(operation, '_service_name')
            service_params = getattr(operation, '_service_params')
            # If the _types is not specified, assumes str types for the params
            params_types = getattr(operation,
                                   '_types') or [str] * len(service_params)
            params_types = params_types + [str] * (len(service_params) -
                                                   len(params_types))
            produces = getattr(operation, '_produces')
            consumes = getattr(operation, '_consumes')
            services_from_request = [s for s in service_name if s in path]
            query_params = getattr(operation, '_query_params')

            if operation._method == self.request.method and service_name == services_from_request and len(
                    service_params) + len(service_name) == len(
                        services_and_params):
                try:
                    url_params = self._find_params_value_of_url(
                        service_name, request_path)
                    arg_params = self._find_params_value_of_arguments(
                        operation)
                    params_values = url_params + arg_params
                    p_values = self._convert_params_values(
                        params_values, params_types)
                    if consumes is None and produces is None:
                        consumes = content_type
                        produces = content_type
                    if consumes == mediatypes.APPLICATION_XML:
                        param_obj = convertXML2OBJ(
                            params_types[0],
                            xml.dom.minidom.parseString(
                                self.request.body).documentElement)
                        p_values.append(param_obj)
                    elif consumes == mediatypes.APPLICATION_JSON:
                        body = self.request.body
                        if sys.version_info > (3, ):
                            body = str(self.request.body, 'utf-8')
                        param_obj = convertJSON2OBJ(params_types[0],
                                                    json.loads(body))
                        p_values.append(param_obj)
                    response = operation(*p_values)

                    if response is None:
                        return

                    self.set_header('Content-Type', produces)

                    if produces == mediatypes.APPLICATION_JSON and hasattr(
                            response, '__module__'):
                        response = convert2JSON(response)
                    elif produces == mediatypes.APPLICATION_XML and hasattr(
                            response, '__module__'):
                        response = convert2XML(response)

                    if produces == mediatypes.APPLICATION_JSON and isinstance(
                            response, dict):
                        self.write(response)
                        self.finish()
                    elif produces == mediatypes.APPLICATION_JSON and isinstance(
                            response, list):
                        self.write(json.dumps(response))
                        self.finish()
                    elif produces in [
                            mediatypes.APPLICATION_XML, mediatypes.TEXT_XML
                    ] and isinstance(response, xml.dom.minidom.Document):
                        self.write(response.toxml())
                        self.finish()
                    else:
                        self.gen_http_error(
                            500,
                            'Internal Server Error : response is not %s document'
                            % produces)
                except Exception as detail:
                    self.gen_http_error(500,
                                        'Internal Server Error : %s' % detail)
示例#5
0
    def _exe(self, method):
        """ Executes the python function for the Rest Service """
        request_path = self.request.path
        path = request_path.split('/')
        services_and_params = list(filter(lambda x: x!='',path))
        content_type = None
        if 'Content-Type' in self.request.headers.keys():
            content_type = self.request.headers['Content-Type']

        # Get all funcion names configured in the class RestHandler
        functions    = list(filter(lambda op: hasattr(getattr(self,op),'_service_name') == True and inspect.ismethod(getattr(self,op)) == True, dir(self)))
        # Get all http methods configured in the class RestHandler
        http_methods = list(map(lambda op: getattr(getattr(self,op),'_method'), functions))

        if method not in http_methods:
            raise tornado.web.HTTPError(405,'The service not have %s verb'%method)
        for operation in list(map(lambda op: getattr(self,op), functions)):
            service_name          = getattr(operation,"_service_name")
            service_params        = getattr(operation,"_service_params")
            # If the _types is not specified, assumes str types for the params
            params_types          = getattr(operation,"_types") or [str]*len(service_params)
            params_types          = params_types + [str]*(len(service_params)-len(params_types))
            produces              = getattr(operation,"_produces")
            consumes              = getattr(operation,"_consumes")
            services_from_request = list(filter(lambda x: x in path,service_name))
            query_params          = getattr(operation,"_query_params")

            if operation._method == self.request.method and service_name == services_from_request and len(service_params) + len(service_name) == len(services_and_params):
                try:
                    params_values = self._find_params_value_of_url(service_name,request_path) + self._find_params_value_of_arguments(operation)
                    p_values      = self._convert_params_values(params_values, params_types)
                    if consumes == None and produces == None:
                        consumes = content_type
                        produces = content_type
                    if consumes == mediatypes.APPLICATION_XML:
                        param_obj = convertXML2OBJ(params_types[0],xml.dom.minidom.parseString(self.request.body).documentElement)
                        p_values.append(param_obj)
                    elif consumes == mediatypes.APPLICATION_JSON:
                        body = self.request.body
                        if sys.version_info > (3,):
                            body = str(self.request.body,'utf-8')
                        param_obj = convertJSON2OBJ(params_types[0],json.loads(body))
                        p_values.append(param_obj)
                    response = operation(*p_values)

                    if response == None:
                        return

                    self.set_header("Content-Type",produces)

                    if produces == mediatypes.APPLICATION_JSON and hasattr(response,'__module__'):
                        response = convert2JSON(response)
                    elif produces == mediatypes.APPLICATION_XML and hasattr(response,'__module__'):
                        response = convert2XML(response)

                    if produces == mediatypes.APPLICATION_JSON and isinstance(response,dict):
                        self.write(response)
                        self.finish()
                    elif produces == mediatypes.APPLICATION_JSON and isinstance(response,list):
                        self.write(json.dumps(response))
                        self.finish()
                    elif produces in [mediatypes.APPLICATION_XML,mediatypes.TEXT_XML] and isinstance(response,xml.dom.minidom.Document):
                        self.write(response.toxml())
                        self.finish()
                    else:
                        self.gen_http_error(500,"Internal Server Error : response is not %s document"%produces)
                except Exception as detail:
                    self.gen_http_error(500,"Internal Server Error : %s"%detail)
                    # 修改的地方 调用RequestHandler的处理请求异常的方法
                    self._handle_request_exception(detail)
示例#6
0
}


class User(object):
    userid = int
    username = str


class Message(object):
    num = int
    text = str
    sender = User
    receiver = User


c = convertJSON2OBJ(Message, doc)
print("JSON original")
print("*************")
print(json.dumps(doc, sort_keys=True, indent=2, separators=(',', ': ')))
print("")
print("Python obj     ")
print("***************")
print("Message        ")
print("---------------")
print("num  : %d " % c.num)
print("text : %s " % c.text)
print("")
print("Sender         ")
print("---------------")
print("c.sender.userid   : %d " % c.sender.userid)
print("c.sender.username : %s " % c.sender.username)
示例#7
0
	def _exe(self, method):
		""" Executes the python function for the Rest Service """
		request_path = self.request.path
		path = request_path.split('/')
		services_and_params = filter(lambda x: x!='',path)
		
		# Get all funcion names configured in the class RestHandler
		functions    = filter(lambda op: hasattr(getattr(self,op),'_service_name') == True and inspect.ismethod(getattr(self,op)) == True, dir(self))
		# Get all http methods configured in the class RestHandler
		http_methods = map(lambda op: getattr(getattr(self,op),'_method'), functions)

		if method not in http_methods:
			raise tornado.web.HTTPError(405,'The service not have %s verb'%method)

		for operation in map(lambda op: getattr(self,op), functions):
			service_name          = getattr(operation,"_service_name")
			service_params        = getattr(operation,"_service_params")
			# If the _types is not specified, assumes str types for the params
			params_types          = getattr(operation,"_types") or [str]*len(service_params)
			params_types          = map(lambda x,y : y if x is None else x, params_types, [str]*len(service_params))
			produces              = getattr(operation,"_produces")
			consumes              = getattr(operation,"_consumes")
			services_from_request = filter(lambda x: x in path,service_name)
			query_params          = getattr(operation,"_query_params")

			if operation._method == self.request.method and service_name == services_from_request and len(service_params) + len(service_name) == len(services_and_params):
				try:
					params_values = self._find_params_value_of_url(service_name,request_path) + self._find_params_value_of_arguments(operation)
					p_values      = self._convert_params_values(params_values, params_types)

					if consumes == mediatypes.APPLICATION_XML:
						param_obj = convertXML2OBJ(params_types[0],xml.dom.minidom.parseString(self.request.body).documentElement)
						p_values.append(param_obj)
					elif consumes == mediatypes.APPLICATION_JSON:
						param_obj = convertJSON2OBJ(params_types[0],json.loads(self.request.body))
						p_values.append(param_obj)

					response = operation(*p_values)
				
					if response == None:
						return

					self.set_header("Content-Type",produces)

					if produces == mediatypes.APPLICATION_JSON and hasattr(response,'__module__'):
						response = convert2JSON(response)
					elif produces == mediatypes.APPLICATION_XML and hasattr(response,'__module__'):
						response = convert2XML(response)

					if produces == mediatypes.APPLICATION_JSON and isinstance(response,dict):
						self.write(response)
						self.finish()
					elif produces == mediatypes.APPLICATION_JSON and isinstance(response,list):
						self.write(json.dumps(response))
						self.finish()
					elif produces in [mediatypes.APPLICATION_XML,mediatypes.TEXT_XML] and isinstance(response,xml.dom.minidom.Document):
						self.write(response.toxml())
						self.finish()
					else:
						self.gen_http_error(500,"Internal Server Error : response is not %s document"%produces)
				except Exception as detail:
					self.gen_http_error(500,"Internal Server Error : %s"%detail)
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# -*- coding: utf-8 -*-

from pyconvert.pyconv import convertJSON2OBJ
import json

doc = [{'username' : 'fred', 'age' : 20} , {'username' : 'barney', 'age' : 20}] 

class User(object):
	username = str
	age = int

users = convertJSON2OBJ(User,doc)
print("JSON original")
print("*************")
print(json.dumps(doc, sort_keys=True, indent=2, separators=(',',': ')))
print("")
print("Python obj     ")
print("***************")
print("Users          ")
print("---------------")
for user in users:
	print("\tuser.username : %s "%user.username)
	print("\tuser.age      : %s "%user.age)
	print("")
示例#9
0
	def _exe(self, method):
		""" Executes the python function for the Rest Service """
		request_path = self.request.path
		path = request_path.split('/')
		services_and_params = list(filter(lambda x: x!='',path))
		content_type = None
		if 'Content-Type' in self.request.headers.keys():
			content_type = self.request.headers['Content-Type']

		# Get all funcion names configured in the class RestHandler
		functions    = list(filter(lambda op: hasattr(getattr(self,op),'_service_name') == True and inspect.ismethod(getattr(self,op)) == True, dir(self)))
		# Get all http methods configured in the class RestHandler
		http_methods = list(map(lambda op: getattr(getattr(self,op),'_method'), functions))

		if method not in http_methods:
			raise tornado.web.HTTPError(405,'The service not have %s verb'%method)
		if path[1] not in list(op._path.split('/')[1] for op in list(map(lambda o: getattr(self,o),functions))):
			raise tornado.web.HTTPError(404,'404 Not Found {}'.format(path[1]))
		for operation in list(map(lambda op: getattr(self,op), functions)):
			service_name          = getattr(operation,'_service_name')
			service_params        = getattr(operation,'_service_params')
			# If the _types is not specified, assumes str types for the params
			params_types          = getattr(operation,"_types") or [str]*len(service_params)
			params_types          = params_types + [str]*(len(service_params)-len(params_types))
			produces              = getattr(operation,'_produces')
			consumes              = getattr(operation,'_consumes')
			services_from_request = list(filter(lambda x: x in path,service_name))
			query_params          = getattr(operation,'_query_params')
			manual_response       = getattr(operation,'_manual_response')
			catch_fire	      = getattr(operation,'_catch_fire')

			if operation._method == self.request.method and service_name == services_from_request and len(service_params) + len(service_name) == len(services_and_params):
				try:
					params_values = self._find_params_value_of_url(service_name,request_path) + self._find_params_value_of_arguments(operation)
					p_values      = self._convert_params_values(params_values, params_types)

					if consumes == None and content_type != None:
                                                consumes = content_type
					if consumes == mediatypes.APPLICATION_XML:
						param_obj = None
						body = self.request.body
						if sys.version_info > (3,):
							body = str(body,'utf-8')
						if len(body) >0 and params_types[0] in [str]:
							param_obj = xml.dom.minidom.parseString(body)
						elif len(body)>0 :
							param_obj = convertXML2OBJ(params_types[0],xml.dom.minidom.parseString(body).documentElement)
						if param_obj != None:
						        p_values.append(param_obj)
					elif consumes == mediatypes.APPLICATION_JSON:
						param_obj = None
						body = self.request.body
						if sys.version_info > (3,):
							body = str(body,'utf-8')
						if len(body)>0 and params_types[0] in [dict,str]:
							param_obj = json.loads(body)
						elif len(body)>0 :
							param_obj = convertJSON2OBJ(params_types[0],json.loads(body))
						if param_obj != None:
						        p_values.append(param_obj)
					elif consumes == mediatypes.TEXT_PLAIN:
						body = self.request.body
						if len(body) >= 1:
						        if sys.version_info > (3,):
							        body = str(body,'utf-8')
						        if isinstance(body,str):
						   	        param_obj = body
						        else:
							        param_obj = convertJSON2OBJ(params_types[0],json.loads(body))
						        p_values.append(param_obj)
                                                
					response = operation(*p_values)
				
					if response == None:
						return

					if produces != None:
                                                self.set_header('Content-Type',produces)

					if manual_response:
						return

					if (produces == mediatypes.APPLICATION_JSON or produces == None) and hasattr(response,'__module__'):
						response = convert2JSON(response)
					elif produces == mediatypes.APPLICATION_XML and hasattr(response,'__module__') and not isinstance(response,xml.dom.minidom.Document):
						response = convert2XML(response)

					if produces == None and (isinstance(response,dict) or isinstance(response,str)):
						self.write(response)
						self.finish()
					elif produces == None and isinstance(response,list):
						self.write(json.dumps(response))
						self.finish()
					elif produces == mediatypes.TEXT_PLAIN and isinstance(response,str):
						self.write(response)
						self.finish()
					elif produces == mediatypes.APPLICATION_JSON and isinstance(response,dict):
						self.write(response)
						self.finish()
					elif produces == mediatypes.APPLICATION_JSON and isinstance(response,list):
						self.write(json.dumps(response))
						self.finish()
					elif produces in [mediatypes.APPLICATION_XML,mediatypes.TEXT_XML] and isinstance(response,xml.dom.minidom.Document):
						self.write(response.toxml())
						self.finish()
					else:
						self.gen_http_error(500,'Internal Server Error : response is not %s document'%produces)
						if catch_fire == True:
							raise PyRestfulException('Internal Server Error : response is not %s document'%produces)
				except Exception as detail:
					self.gen_http_error(500,'Internal Server Error : %s'%detail)
					if catch_fire == True:
						raise PyRestfulException(detail)
from pyconvert.pyconv import convertJSON2OBJ
import json

doc = {'sender' : { 'userid' : 1220000, 'username' : 'Fred '}, 'receiver' : { 'userid' : 203201, 'username' : 'Kathy' } , "num" : 1, "text" : 'Hey please send me the mail'}

class User(object):
	userid = int
	username = str

class Message(object):
	num = int
	text = str
	sender = User
	receiver = User

c = convertJSON2OBJ(Message,doc)
print("JSON original")
print("*************")
print(json.dumps(doc, sort_keys=True, indent=2, separators=(',',': ')))
print("")
print("Python obj     ")
print("***************")
print("Message        ")
print("---------------")
print("num  : %d "%c.num)
print("text : %s "%c.text)
print("")
print("Sender         ")
print("---------------")
print("c.sender.userid   : %d "%c.sender.userid)
print("c.sender.username : %s "%c.sender.username)