How to use the pyang.types.PathTypeSpec function in pyang

To help you get started, we’ve selected a few pyang examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
def leafref_type(self, tchain, p_elem):
        typ = tchain[0]
        occur = p_elem.occur
        pathstr = typ.parent.i_leafref.i_expanded_path
        p_elem.attr["nma:leafref"] = self.yang_to_xpath(pathstr)
        while type(typ.i_type_spec) == types.PathTypeSpec:
            typ = typ.i_type_spec.i_target_node.search_one("type")
        self.handle_stmt(typ, p_elem)
        if occur == 0: p_elem.occur = 0
github CiscoDevNet / ydk-gen / ydkgen / builder / _types_extractor.py View on Github external
def _get_type_stmt(self, stmt, typeSpec):
        if stmt.keyword == 'type':
            type_stmt = stmt
        else:
            type_stmt = stmt.search_one('type')

        if hasattr(type_stmt, 'i_typedef') and type_stmt.i_typedef is not None:
            typedef_stmt = type_stmt.i_typedef
            return self._get_type_stmt(typedef_stmt, typeSpec)
        elif hasattr(type_stmt, 'i_type_spec'):
            type_spec = type_stmt.i_type_spec
            while isinstance(type_spec, PathTypeSpec):
                if not hasattr(type_spec, 'i_target_node'):
                    return None
                type_stmt = type_spec.i_target_node.search_one('type')
                type_spec = type_stmt.i_type_spec
                if hasattr(type_stmt, 'i_typedef') and type_stmt.i_typedef is not None:
                    typedef_stmt = type_stmt.i_typedef
                    return self._get_type_stmt(typedef_stmt, typeSpec)

            if isinstance(type_spec, typeSpec):
                return type_stmt
            else:
                return None
        else:
            return None
github mbj4668 / pyang / pyang / statements.py View on Github external
stmt.i_type_spec = types.PatternTypeSpec(stmt.i_type_spec,
                                                     pattern_specs)

    # check the path restriction
    path = stmt.search_one('path')
    if path is not None and stmt.arg != 'leafref':
        err_add(ctx.errors, path.pos, 'BAD_RESTRICTION', 'path')
    elif stmt.arg == 'leafref' and path is None:
        err_add(ctx.errors, stmt.pos, 'MISSING_TYPE_SPEC_1',
                ('leafref', 'path'))
    elif path is not None:
        stmt.i_is_derived = True
        if path.is_grammatically_valid is True:
            path_spec = types.validate_path_expr(ctx.errors, path)
            if path_spec is not None:
                stmt.i_type_spec = types.PathTypeSpec(stmt.i_type_spec,
                                                      path_spec, path, path.pos)
                stmt.i_type_spec.i_source_stmt = stmt

    # check the base restriction
    bases = stmt.search('base')
    if bases and stmt.arg != 'identityref':
        err_add(ctx.errors, bases[0].pos, 'BAD_RESTRICTION', 'base')
    elif len(bases) > 1 and stmt.i_module.i_version == '1':
        err_add(ctx.errors, bases[1].pos, 'UNEXPECTED_KEYWORD', 'base')
    elif stmt.arg == 'identityref' and not bases:
        err_add(ctx.errors, stmt.pos, 'MISSING_TYPE_SPEC',
                ('identityref', 'base'))
    else:
        idbases = []
        for base in bases:
            v_type_base(ctx, base)
github mbj4668 / pyang / pyang / statements.py View on Github external
# non-top-level typedef; check if it is already defined
        ptype = search_typedef(stmt.parent.parent, name)
        if ptype is not None:
            err_add(ctx.errors, stmt.pos, 'TYPE_ALREADY_DEFINED',
                    (name, ptype.pos))
    type_ = stmt.search_one('type')
    if type_ is None or type_.is_grammatically_valid is False:
        # error is already reported by grammar check
        stmt.i_is_validated = True
        return
    # ensure our type is validated
    v_type_type(ctx, type_)

    # keep track of our leafref
    type_spec = type_.i_type_spec
    if isinstance(type_spec, types.PathTypeSpec):
        stmt.i_leafref = type_spec

    def check_circular_typedef(ctx, type_):
        # ensure the type is validated
        v_type_type(ctx, type_)
        # check the direct typedef
        if (type_.i_typedef is not None and
            type_.i_typedef.is_grammatically_valid is True):
            v_type_typedef(ctx, type_.i_typedef)
        # check all union's types
        membertypes = type_.search('type')
        for t in membertypes:
            check_circular_typedef(ctx, t)

    check_circular_typedef(ctx, type_)
github mbj4668 / pyang / pyang / statements.py View on Github external
stmt.i_type_spec = types.PatternTypeSpec(stmt.i_type_spec,
                                                     pattern_specs)

    # check the path restriction
    path = stmt.search_one('path')
    if path is not None and stmt.arg != 'leafref':
        err_add(ctx.errors, path.pos, 'BAD_RESTRICTION', 'path')
    elif stmt.arg == 'leafref' and path is None:
        err_add(ctx.errors, stmt.pos, 'MISSING_TYPE_SPEC_1',
                ('leafref', 'path'))
    elif path is not None:
        stmt.i_is_derived = True
        if path.is_grammatically_valid == True:
            path_spec = types.validate_path_expr(ctx.errors, path)
            if path_spec is not None:
                stmt.i_type_spec = types.PathTypeSpec(stmt.i_type_spec,
                                                      path_spec, path, path.pos)
                stmt.i_type_spec.i_source_stmt = stmt

    # check the base restriction
    bases = stmt.search('base')
    if bases != [] and stmt.arg != 'identityref':
        err_add(ctx.errors, bases[0].pos, 'BAD_RESTRICTION', 'base')
    elif len(bases) > 1 and stmt.i_module.i_version == '1':
        err_add(ctx.errors, bases[1].pos, 'UNEXPECTED_KEYWORD', 'base')
    elif stmt.arg == 'identityref' and bases == []:
        err_add(ctx.errors, stmt.pos, 'MISSING_TYPE_SPEC',
                ('identityref', 'base'))
    else:
        idbases = []
        for base in bases:
            v_type_base(ctx, base)
github CiscoDevNet / ydk-gen / ydkgen / printer / cpp / class_members_printer.py View on Github external
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' % _get_leafref_comment(union_leaf))
            else:
                contained_types.add(contained_type_stmt.i_type_spec.name)
        return contained_types
github CiscoDevNet / ydk-gen / ydkgen / printer / meta_data_util.py View on Github external
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):