Beispiel #1
0
 def _get_union_types(self, union_leaf):
     union_type = union_leaf.property_type
     contained_types = set()
     for contained_type_stmt in union_type.types:
         contained_property_type = TypesExtractor().get_property_type(contained_type_stmt)
         if isinstance(contained_property_type, UnionTypeSpec):
             contained_types.update(self._get_union_types(contained_property_type))
         elif isinstance(contained_property_type, PathTypeSpec):
             contained_types.add('%s' % self._get_leafref_comment(union_leaf))
         else:
             contained_types.add(contained_type_stmt.i_type_spec.name)
     return contained_types
def get_ptypes(prop, property_type, type_stmt, one_class_per_module,
               identity_subclasses):
    if prop.stmt.keyword == 'anyxml':
        return ["'str'"]

    ptypes = []
    type_spec = type_stmt.i_type_spec
    types_extractor = TypesExtractor()
    if isinstance(type_spec, UnionTypeSpec):
        for contained_type_stmt in type_spec.types:
            contained_property_type = types_extractor.get_property_type(
                contained_type_stmt)
            ptypes.extend(
                get_ptypes(prop, contained_property_type, contained_type_stmt,
                           one_class_per_module, identity_subclasses))
    else:
        ptypes.append(
            get_ptype(prop, property_type, type_stmt, one_class_per_module,
                      identity_subclasses))
    return ptypes
Beispiel #3
0
def get_meta_info_data(prop, property_type, type_stmt):
    """ Gets an instance of MetaInfoData that has the useful information about the property.

        Args:
            prop: The property
            property_type : The type under consideration
            type_stmt : The type stmt currently under consideration 
    """
    clazz = prop.owner
    meta_info_data = MetaInfoData(prop)
    types_extractor = TypesExtractor()
    target_type_stmt = type_stmt

    if isinstance(property_type, Class):
        meta_info_data.pmodule_name = "'%s'" % property_type.get_py_mod_name()
        meta_info_data.clazz_name = "'%s'" % property_type.qn()

        meta_info_data.doc_link = ':py:class:`%s <%s.%s>`' % (
            property_type.name, property_type.get_py_mod_name(),
            property_type.qn())

        if prop.is_many:
            meta_info_data.mtype = 'REFERENCE_LIST'
            meta_info_data.doc_link = 'list of %s' % meta_info_data.doc_link
        elif property_type.is_identity():
            meta_info_data.mtype = 'REFERENCE_IDENTITY_CLASS'
        else:
            meta_info_data.mtype = 'REFERENCE_CLASS'
        # if the class is local use just the local name
        if property_type in clazz.owned_elements:
            meta_info_data.ptype = property_type.name
        else:
            meta_info_data.ptype = property_type.qn()

    elif isinstance(property_type, Enum):
        meta_info_data.pmodule_name = "'%s'" % property_type.get_py_mod_name()
        meta_info_data.clazz_name = "'%s'" % property_type.qn()
        meta_info_data.doc_link = ':py:class:`%s <%s.%s>`' % (
            property_type.name, property_type.get_py_mod_name(),
            property_type.qn())

        meta_info_data.mtype = 'REFERENCE_ENUM_CLASS'
        if prop.is_many:
            meta_info_data.mtype = 'REFERENCE_LEAFLIST'
            meta_info_data.doc_link = 'list of %s' % meta_info_data.doc_link

        if prop.property_type in clazz.owned_elements:
            meta_info_data.ptype = property_type.name
        else:
            meta_info_data.ptype = property_type.qn()

    elif isinstance(property_type, Bits):
        meta_info_data.pmodule_name = "'%s'" % property_type.get_py_mod_name()
        meta_info_data.clazz_name = "'%s'" % property_type.qn()
        meta_info_data.doc_link = ':py:class:`%s <%s.%s>`' % (
            property_type.name, property_type.get_py_mod_name(),
            property_type.qn())

        meta_info_data.mtype = 'REFERENCE_BITS'
        if prop.is_many:
            meta_info_data.mtype = 'REFERENCE_LEAFLIST'
            meta_info_data.doc_link = 'list of %s' % meta_info_data.doc_link

        if prop.property_type in clazz.owned_elements:
            meta_info_data.ptype = property_type.name
        else:
            meta_info_data.ptype = property_type.qn()

    else:
        if prop.stmt.keyword == 'leaf-list':
            meta_info_data.mtype = 'REFERENCE_LEAFLIST'
            meta_info_data.doc_link = 'list of '
        elif prop.stmt.keyword == 'anyxml':
            meta_info_data.mtype = 'ANYXML_CLASS'
            meta_info_data.doc_link = 'anyxml'
            meta_info_data.ptype = 'object'
            return meta_info_data
        else:
            meta_info_data.mtype = 'ATTRIBUTE'
            meta_info_data.doc_link = ''

        type_spec = type_stmt.i_type_spec

        while isinstance(type_spec, PathTypeSpec):
            if not hasattr(type_spec, 'i_target_node'):
                return None
            target_type_stmt = type_spec.i_target_node.search_one('type')
            type_spec = target_type_stmt.i_type_spec

        if isinstance(type_spec, BinaryTypeSpec):
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += meta_info_data.ptype
        elif isinstance(type_spec, BitsTypeSpec):
            # This can happen in a Union
            raise EmitError('Illegal Code path')
        elif isinstance(type_spec, BooleanTypeSpec):
            meta_info_data.ptype = 'bool'
            meta_info_data.doc_link += meta_info_data.ptype
        elif isinstance(type_spec, Decimal64TypeSpec):
            meta_info_data.ptype = 'Decimal64'
            meta_info_data.prange.append(
                ('%s' % type_spec.min.s, '%s' % type_spec.max.s))
            # ' :ref:`Decimal64 <ydk_models_types_Decimal64>`'
            meta_info_data.doc_link += ':py:class:`Decimal64 <ydk.types.Decimal64>`'
        elif isinstance(type_spec, EmptyTypeSpec):
            meta_info_data.ptype = 'Empty'
            # ' :ref:`Empty <ydk_models_types_Empty>`'
            meta_info_data.doc_link += ':py:class:`Empty <ydk.types.Empty>`'
        elif isinstance(prop.property_type, Enum):
            raise EmitError('Illegal Code path')
        elif isinstance(type_spec, IdentityrefTypeSpec):
            raise EmitError('Illegal Code path')
        elif isinstance(type_spec, IntTypeSpec):
            meta_info_data.ptype = 'int'
            meta_info_data.doc_link += meta_info_data.ptype
            meta_info_data.prange.append((type_spec.min, type_spec.max))
        elif isinstance(type_spec, LengthTypeSpec):
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += meta_info_data.ptype
            meta_info_data.prange = get_length_limits(type_spec)

        elif isinstance(type_spec, PathTypeSpec):
            raise EmitError('Illegal Code path')
        elif isinstance(type_spec, PatternTypeSpec):
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += meta_info_data.ptype
            while hasattr(
                    target_type_stmt,
                    'i_typedef') and target_type_stmt.i_typedef is not None:
                target_type_stmt = target_type_stmt.i_typedef.search_one(
                    'type')
            pattern = target_type_stmt.search_one('pattern')
            if pattern is not None:
                meta_info_data.pattern.append(pattern.arg.encode('ascii'))
        elif isinstance(type_spec, RangeTypeSpec):
            meta_info_data.ptype = get_range_base_type_name(type_spec)
            meta_info_data.prange = get_range_limits(type_spec)
            meta_info_data.doc_link += meta_info_data.ptype

        elif isinstance(type_spec, StringTypeSpec):
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += meta_info_data.ptype
        elif isinstance(type_spec, UnionTypeSpec):
            # validate against all the data types
            meta_info_data.mtype = 'REFERENCE_UNION'
            meta_info_data.ptype = 'str'
            meta_info_data.property_type = type_spec
            if len(type_spec.types) > 0:
                # meta_info_data.doc_link += 'one of { '
                for contained_type_stmt in type_spec.types:
                    enum_type_stmt = types_extractor.get_enum_type_stmt(
                        contained_type_stmt)
                    bits_type_stmt = types_extractor.get_bits_type_stmt(
                        contained_type_stmt)
                    union_type_stmt = types_extractor.get_union_type_stmt(
                        contained_type_stmt)
                    contained_property_type = contained_type_stmt.i_type_spec
                    if isinstance(contained_property_type,
                                  IdentityrefTypeSpec):
                        contained_property_type = contained_property_type.base.i_identity.i_class
                    elif enum_type_stmt is not None:
                        # this is an enumeration
                        if not hasattr(enum_type_stmt, 'i_enum'):
                            raise EmitError('Failure to get i_enum')
                        contained_property_type = enum_type_stmt.i_enum
                    elif bits_type_stmt is not None:
                        # bits
                        contained_property_type = bits_type_stmt.i_bits
                    elif union_type_stmt is not None:
                        contained_property_type = union_type_stmt
                    child_meta_info_data = get_meta_info_data(
                        prop, contained_property_type, contained_type_stmt)
                    meta_info_data.children.append(child_meta_info_data)
                    # if meta_info_data.doc_link[-1:] != ' ':
                    #    meta_info_data.doc_link += ' | '
                    # meta_info_data.doc_link += child_meta_info_data.doc_link
                # meta_info_data.doc_link += ' }'

        elif isinstance(type_spec,
                        TypeSpec) and type_spec.name == 'instance-identifier':
            # Treat as string
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += meta_info_data.ptype
        else:
            raise EmitError('Illegal path')
    return meta_info_data
Beispiel #4
0
 def __init__(self, lang, identity_subclasses):
     self.lang = lang
     self.identity_subclasses = identity_subclasses
     self.types_extractor = TypesExtractor()
     self.type_spec = None
     self.type_stmt = None
Beispiel #5
0
def get_meta_info_data(prop,
                       property_type,
                       type_stmt,
                       language,
                       identity_subclasses=None):
    """ Gets an instance of MetaInfoData that has the useful information about the property.

        Args:
            prop: The property
            property_type : The type under consideration
            type_stmt : The type stmt currently under consideration
    """
    clazz = prop.owner
    meta_info_data = MetaInfoData(prop)
    types_extractor = TypesExtractor()
    target_type_stmt = type_stmt

    mandatory = prop.stmt.search_one('mandatory')
    if mandatory is not None and mandatory.arg == 'true':
        meta_info_data.mandatory = True

    presence = prop.stmt.search_one('presence')
    if presence is not None:
        meta_info_data.is_presence = True

    units = prop.stmt.search_one('units')
    if units is not None:
        meta_info_data.units = units.arg

    status = prop.stmt.search_one('status')
    if status is not None:
        meta_info_data.status = status.arg

    default_value = prop.stmt.search_one('default')
    if default_value is not None:
        meta_info_data.default_value = default_value.arg

    if isinstance(property_type, Class):
        meta_info_data.pmodule_name = "'%s'" % property_type.get_py_mod_name()
        meta_info_data.clazz_name = "'%s'" % property_type.qn()

        if identity_subclasses is None:
            meta_info_data.doc_link = get_class_crossref_tag(
                property_type.name, property_type, language)
        else:
            meta_info_data.doc_link = _get_identity_docstring(
                identity_subclasses, property_type, language)
            meta_info_data.doc_link_description = 'one of the below types:'

        if prop.stmt.keyword == 'leaf-list':
            meta_info_data.mtype = 'REFERENCE_LEAFLIST'
            target = ''
            if isinstance(meta_info_data.doc_link, list):
                doc_link = map(lambda l: '\n\n\t\t%s' % l,
                               meta_info_data.doc_link)
                target = ''.join(doc_link)
            meta_info_data.doc_link = target
            meta_info_data.doc_link = '\n\t\t' + _get_list_doc_link_tag(
                meta_info_data, 'doc_link', language, meta_info_data.mtype)
        elif prop.stmt.keyword == 'list':
            meta_info_data.mtype = 'REFERENCE_LIST'
            meta_info_data.doc_link_description = _get_list_doc_link_tag(
                meta_info_data, 'doc_link_description', language,
                meta_info_data.mtype)
        elif property_type.is_identity():
            meta_info_data.mtype = 'REFERENCE_IDENTITY_CLASS'
        else:
            meta_info_data.mtype = 'REFERENCE_CLASS'
        # if the class is local use just the local name
        if property_type in clazz.owned_elements:
            meta_info_data.ptype = property_type.name
        else:
            meta_info_data.ptype = property_type.qn()

    elif isinstance(property_type, Enum):
        meta_info_data.pmodule_name = "'%s'" % property_type.get_py_mod_name()
        meta_info_data.clazz_name = "'%s'" % property_type.qn()

        meta_info_data.doc_link = get_class_crossref_tag(
            property_type.name, property_type, language)
        meta_info_data.mtype = 'REFERENCE_ENUM_CLASS'
        if prop.is_many:
            meta_info_data.mtype = 'REFERENCE_LEAFLIST'
            meta_info_data.doc_link = _get_list_doc_link_tag(
                meta_info_data, 'doc_link', language, meta_info_data.mtype)

        if prop.property_type in clazz.owned_elements:
            meta_info_data.ptype = property_type.name
        else:
            meta_info_data.ptype = property_type.qn()

    elif isinstance(property_type, Bits):
        meta_info_data.pmodule_name = "'%s'" % property_type.get_py_mod_name()
        meta_info_data.clazz_name = "'%s'" % property_type.qn()
        meta_info_data.doc_link = get_bits_doc_link(property_type, language)
        meta_info_data.mtype = 'REFERENCE_BITS'
        if prop.is_many:
            meta_info_data.mtype = 'REFERENCE_LEAFLIST'
            meta_info_data.doc_link = _get_list_doc_link_tag(
                meta_info_data, 'doc_link', language, meta_info_data.mtype)

        if prop.property_type in clazz.owned_elements:
            meta_info_data.ptype = property_type.name
        else:
            meta_info_data.ptype = property_type.qn()

    else:
        if prop.stmt.keyword == 'leaf-list':
            meta_info_data.mtype = 'REFERENCE_LEAFLIST'
            meta_info_data.doc_link = _get_list_tag(language,
                                                    meta_info_data.mtype)
        elif prop.stmt.keyword == 'anyxml':
            meta_info_data.mtype = 'ANYXML_CLASS'
            meta_info_data.doc_link = 'anyxml'
            meta_info_data.ptype = 'object'
            return meta_info_data
        else:
            meta_info_data.mtype = 'ATTRIBUTE'
            meta_info_data.doc_link = ''

        type_spec = type_stmt.i_type_spec

        if isinstance(type_spec, PathTypeSpec):
            if prop.stmt.i_leafref_ptr is not None:
                reference_class = prop.stmt.i_leafref_ptr[0].parent.i_class
                reference_prop = prop.stmt.i_leafref_ptr[0].i_property
                tag = get_class_crossref_tag(reference_prop.name,
                                             reference_class, language)
                meta_info_data.target_of_leafref = tag
        while isinstance(type_spec, PathTypeSpec):
            if not hasattr(type_spec, 'i_target_node'):
                return None
            target_type_stmt = type_spec.i_target_node.search_one('type')
            type_spec = target_type_stmt.i_type_spec

        if isinstance(type_spec, BinaryTypeSpec):
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += get_primitive_type_tag('str', language)
        elif isinstance(type_spec, BitsTypeSpec):
            # This can happen in a Union
            raise EmitError('Illegal Code path')
        elif isinstance(type_spec, BooleanTypeSpec):
            meta_info_data.ptype = 'bool'
            meta_info_data.doc_link += get_primitive_type_tag('bool', language)
        elif isinstance(type_spec, Decimal64TypeSpec):
            meta_info_data.ptype = 'Decimal64'
            meta_info_data.prange.append(
                ('%s' % str(type_spec.min.s), '%s' % str(type_spec.max.s)))
            meta_info_data.doc_link += get_primitive_type_tag(
                'Decimal64', language)
        elif isinstance(type_spec, EmptyTypeSpec):
            meta_info_data.ptype = 'Empty'
            meta_info_data.doc_link += get_primitive_type_tag(
                'Empty', language)
        elif isinstance(prop.property_type, Enum):
            raise EmitError('Illegal Code path')
        elif isinstance(type_spec, IdentityrefTypeSpec):
            raise EmitError('Illegal Code path')
        elif isinstance(type_spec, IntTypeSpec):
            meta_info_data.ptype = 'int'
            meta_info_data.doc_link += meta_info_data.ptype
            lower = str(type_spec.min)
            upper = str(type_spec.max)
            meta_info_data.prange.append((lower, upper))
        elif isinstance(type_spec, LengthTypeSpec):
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += get_primitive_type_tag('str', language)
            meta_info_data.prange = get_length_limits(type_spec)

        elif isinstance(type_spec, PathTypeSpec):
            raise EmitError('Illegal Code path')
        elif isinstance(type_spec, PatternTypeSpec):
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += get_primitive_type_tag('str', language)
            while hasattr(
                    target_type_stmt,
                    'i_typedef') and target_type_stmt.i_typedef is not None:
                target_type_stmt = target_type_stmt.i_typedef.search_one(
                    'type')
            pattern = target_type_stmt.search_one('pattern')
            if pattern is not None:
                meta_info_data.pattern.append(pattern.arg.encode('ascii'))
        elif isinstance(type_spec, RangeTypeSpec):
            meta_info_data.ptype = get_range_base_type_name(type_spec)
            meta_info_data.prange = get_range_limits(type_spec)
            meta_info_data.doc_link += meta_info_data.ptype

        elif isinstance(type_spec, StringTypeSpec):
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += get_primitive_type_tag('str', language)
        elif isinstance(type_spec, UnionTypeSpec):
            # validate against all the data types
            meta_info_data.mtype = 'REFERENCE_UNION'
            meta_info_data.ptype = 'str'
            meta_info_data.property_type = type_spec
            for contained_type_stmt in type_spec.types:
                contained_property_type = types_extractor.get_property_type(
                    contained_type_stmt)
                child_meta_info_data = get_meta_info_data(
                    prop,
                    contained_property_type,
                    contained_type_stmt,
                    language,
                    identity_subclasses=identity_subclasses)
                meta_info_data.children.append(child_meta_info_data)

        elif isinstance(type_spec,
                        TypeSpec) and type_spec.name == 'instance-identifier':
            # Treat as string
            meta_info_data.ptype = 'str'
            meta_info_data.doc_link += get_primitive_type_tag('str', language)
        else:
            raise EmitError('Illegal path')
    return meta_info_data