コード例 #1
0
        def _get_json_key_value(json_data, primary_key, secondary_key):
            value_list = []
            handle_item_in_json(json_data, primary_key, secondary_key,
                                value_list)

            if len(value_list) == 1:
                value_list = value_list.pop()

            return value_list
コード例 #2
0
            def _get_ref():
                ref_node = '$ref'
                refs = always_get_list(getattr(self, ref_node))

                if refs:
                    upper_layer_content = self._json_content
                    _values = []

                    for ref in refs:
                        definition_node = ref.split('#/definitions/')[1]

                        if not self.current_root_ref_pair:
                            self._current_root_ref_pair.update({
                                'from': definition_node,
                                'to': None
                            })
                        elif not self.current_root_ref_pair['to']:
                            self._current_root_ref_pair['to'] = definition_node
                        elif self.current_root_ref_pair[
                                'from'] == definition_node:
                            handle_item_in_json(
                                candidate=upper_layer_content,
                                node=ref_node,
                                values={'circular_reference': definition_node},
                                mode='replace_key_with_value')
                            return

                        _switch_to_definition_scope()

                        definition_node_content = \
                            getattr(self, definition_node + '_properties')

                        if definition_node_content:
                            _values.append(
                                {definition_node: definition_node_content})

                            self._switch_json_content(definition_node,
                                                      definition_node_content,
                                                      cached=True)
                            self._recursive_depth_count += 1
                            _get_ref()
                            self._recursive_depth_count -= 1
                    if _values:
                        handle_item_in_json(candidate=upper_layer_content,
                                            node=ref_node,
                                            values=_values,
                                            mode='set')
                    if not self._recursive_depth_count:
                        self._clear_current_root_ref_pair()
コード例 #3
0
    def __getattr__(self, attribute):
        """
        The method can help to get the entity attribute value in json object.
        And no declaration is needed in the entity.
        There are some requirements have to meet for the attribute name:
        i. the name should be combined with 'node name'+'_'+'key name' of the attribute in json object;
        ii. if 'node name' includes '_', the attribute name has to be added in special_node_attributes.
        iii. if 'key name' includes '_', the attribute name has to be added in special_key_attributes.
        :param attribute: the attribute name
        :return: the value which is corresponding to the given attribute name in json object.
        """
        if not (self._response or self._json_content):
            self._get_json_content()

        values = []
        node, key = self.get_json_value_by_node_key(attribute)
        handle_item_in_json(self._json_content, node, key, values)

        if len(values) == 1:
            values = values.pop()

        return values
コード例 #4
0
                def handle_dict_data_pattern():
                    if kwargs:
                        for k in kwargs:
                            node, key = entity.get_json_value_by_node_key(k)
                            value = copy.copy((kwargs[k]))
                            if value is None:
                                handle_item_in_json(data_pattern,
                                                    node,
                                                    key,
                                                    mode='pop_key')
                            else:
                                handle_item_in_json(data_pattern,
                                                    node,
                                                    key,
                                                    values=value,
                                                    mode='set')
                    if args:
                        raise Exception('*args is not supported now!')

                    data = data_pattern

                    return data
コード例 #5
0
    def next(self):
        try:

            def _switch_to_current_path_method_scope():
                self._reload_json_content()
                self._switch_json_content(scope_path)
                self._switch_json_content(method_type)

            def _switch_to_definition_scope():
                self._reload_json_content()
                self._switch_json_content(scope_definitions)

            def _switch_to_path_scope():
                self._reload_json_content()
                self._switch_json_content(scope_path)

            def _get_ref():
                ref_node = '$ref'
                refs = always_get_list(getattr(self, ref_node))

                if refs:
                    upper_layer_content = self._json_content
                    _values = []

                    for ref in refs:
                        definition_node = ref.split('#/definitions/')[1]

                        if not self.current_root_ref_pair:
                            self._current_root_ref_pair.update({
                                'from': definition_node,
                                'to': None
                            })
                        elif not self.current_root_ref_pair['to']:
                            self._current_root_ref_pair['to'] = definition_node
                        elif self.current_root_ref_pair[
                                'from'] == definition_node:
                            handle_item_in_json(
                                candidate=upper_layer_content,
                                node=ref_node,
                                values={'circular_reference': definition_node},
                                mode='replace_key_with_value')
                            return

                        _switch_to_definition_scope()

                        definition_node_content = \
                            getattr(self, definition_node + '_properties')

                        if definition_node_content:
                            _values.append(
                                {definition_node: definition_node_content})

                            self._switch_json_content(definition_node,
                                                      definition_node_content,
                                                      cached=True)
                            self._recursive_depth_count += 1
                            _get_ref()
                            self._recursive_depth_count -= 1
                    if _values:
                        handle_item_in_json(candidate=upper_layer_content,
                                            node=ref_node,
                                            values=_values,
                                            mode='set')
                    if not self._recursive_depth_count:
                        self._clear_current_root_ref_pair()

            def _get_schema(source):
                schema = getattr(self, source)
                _get_ref()

                return schema

            def _get_parameters_name():
                index = indexes.next()
                name = always_get_list(self.name)[index]

                if parameters_enum or parameters_schema:
                    self._switch_json_content('parameter',
                                              parameters[index],
                                              cached=False)
                    if self.enum:
                        name_enum_pair = {name: {'enum': self.enum}}

                        return name_enum_pair

                    if self.schema:
                        schema = _get_schema('schema')
                        name_schema_pair = {'schema': schema}
                        return name_schema_pair

                return name

            def _get_responses():
                if self.responses_200:
                    schema_source = 'schema'

                    self._switch_json_content('responses_200', cached=False)

                    if self.schema_additionalProperties:
                        schema_source = 'schema_additionalProperties'

                    if self.schema_items:
                        schema_source = 'schema_items'

                    value = _get_schema(schema_source)

                    _switch_to_current_path_method_scope()

                    return value

            def _handle_parameters_in():
                value_for_oprate = None

                variable = parameter_type_map_variable[parameter_type]
                # dynamically getting operator depending on var type
                variable_oprator = \
                    isinstance(variable, list) and \
                    'append' or isinstance(variable, dict) and 'update'
                # get value by parameter type
                value_for_oprate = \
                    parameter_type_map_handle_func[parameter_type]()

                if value_for_oprate:
                    getattr(variable, variable_oprator)(value_for_oprate)

                    _key = PARAMETER_TYPE_MAP_ARG_NAME[parameter_type]
                    _inner_attr_map.update({_key: variable})

                    _switch_to_current_path_method_scope()

            entities_attr_map = {}
            entities_attr_map_key = None
            scope_path = self.iter_paths.next()
            cls_name = re.sub(r'[/{}-]', '', scope_path.title())
            scope_definitions = 'definitions'
            has_data_pattern = False
            content_type = None
            parameter_type_map_handle_func = {
                'path': _get_parameters_name,
                'query': _get_parameters_name,
                'body': _get_parameters_name,
                'header': _get_parameters_name
            }
            _switch_to_path_scope()

            for method_type in (self.post and 'post', self.get
                                and 'get', self.put and 'put', self.delete
                                and 'delete'):

                if not method_type:
                    continue

                _switch_to_current_path_method_scope()

                body_data = {}
                query_data = []
                response = None
                parameters_in_path = []
                schemes = None
                header = []
                parameter_type_map_variable = {
                    'path': parameters_in_path,
                    'query': query_data,
                    'body': body_data,
                    'header': header
                }
                _inner_attr_map = {}
                entities_attr_map_key = method_type + '_entity'
                # set content type value
                if self.consumes:
                    content_type = self.consumes[0]
                # parse parameter and get value
                # get value from path,query,body
                # and set value for parameters_in_path,query_data,body_data
                parameters_in = always_get_list(self.parameters_in)
                if parameters_in:
                    parameters = always_get_list(self.parameters)
                    parameters_enum = always_get_list(self.parameters_enum)
                    parameters_schema = always_get_list(self.parameters_schema)

                    indexes = iter([i for i in xrange(len(parameters_in))])

                    for parameter_type in parameters_in:
                        _handle_parameters_in()
                # set value for has_data_pattern
                if body_data or query_data:
                    has_data_pattern = True
                # set response
                response = _get_responses()
                # set schemes value
                if self.schemes:
                    schemes = self.schemes[0]

                miscellaneous_info = {
                    'method_type': method_type,
                    'has_data_pattern': has_data_pattern,
                    'content_type': content_type,
                    'cls_name': method_type.title() + cls_name,
                    'path': scope_path,
                    'response': response,
                    'schemes': schemes
                }
                _inner_attr_map.update(miscellaneous_info)
                # replace ref tag for ref node with refferd item
                handle_item_in_json(candidate=_inner_attr_map,
                                    node='$ref',
                                    mode='replace_key_with_value',
                                    deep_search=True)
                # remove unwanted key in keys
                [
                    handle_item_in_json(candidate=_inner_attr_map,
                                        node=uk,
                                        mode='pop_key',
                                        deep_search=True)
                    for uk in UNWANTED_KEYS
                ]

                entities_attr_map.update(
                    {entities_attr_map_key: _inner_attr_map})

            print getCleanJsonView(entities_attr_map)
            print '++++++++++++++++++'
            return entities_attr_map
        except StopIteration:
            pass
        finally:
            [
                self._json_content_cache.pop(k)
                for k in ('get', 'post', 'put', 'delete')
                if k in self._json_content_cache
            ]

            self._reload_json_content()