def get_primitive_type_tag(typ, language): TYPE_TAG_MAP = { ('py', 'int'): 'int', ('py', 'str'): 'str', ('py', 'bool'): 'bool', ('py', 'Decimal64'): ':py:class:`Decimal64<ydk.types.Decimal64>`', ('py', 'Empty'): ':py:class:`Empty<ydk.types.Empty>`', ('py', 'Enum'): ':py:class:`Enum<ydk.types.Enum>`', ('py', 'Identity'): ':py:class:`Identity<ydk.types.Identity>`', ('cpp', 'int'): '``int``', ('cpp', 'str'): '``std::string``', ('cpp', 'bool'): '``bool``', ('cpp', 'Decimal64'): ':cpp:class:`Decimal64<ydk::Decimal64>`', ('cpp', 'Empty'): ':cpp:class:`Empty<ydk::Empty>`', ('cpp', 'Enum'): ':cpp:class:`Enum<ydk::Enum>`', ('cpp', 'Identity'): ':cpp:class:`Identity<ydk::Identity>`', ('go', 'int'): 'int', ('go', 'str'): 'string', ('go', 'bool'): 'bool', ('go', 'Decimal64'): ':go:struct:`Decimal64<Ydk_Decimal64>`', ('go', 'Empty'): ':go:struct:`Empty<Ydk_Empty>`', ('go', 'Enum'): ':go:struct:`Enum<Ydk_Enum>`', ('go', 'Identity'): ':go:struct:`Identity<Ydk_Identity>`', } try: return TYPE_TAG_MAP[(language, typ)] except KeyError: raise EmitError( "Invalid language, type combination for rst documentation\n" "language = %s, type = %s" % (language, typ))
def get_range_base_type_name(range_type): base_type = get_range_base_type_spec(range_type) if isinstance(base_type, IntTypeSpec): ptype = 'int' elif isinstance(base_type, Decimal64TypeSpec): ptype = 'Decimal64' else: raise EmitError('Unknown range type') return ptype
def print_rst_file(self, named_element): if isinstance(named_element, Enum): self._print_enum_rst(named_element) elif isinstance(named_element, Class): self._print_class_rst(named_element) elif isinstance(named_element, Package): self._print_package_rst(named_element) else: raise EmitError('Unrecognized named_element')
def print_module_documentation(self, named_element): self.lines = [] if isinstance(named_element, Enum): self._print_enum_rst(named_element) elif isinstance(named_element, Class): self._print_class_rst(named_element) elif isinstance(named_element, Package): self._print_package_rst(named_element) else: raise EmitError('Unrecognized named_element') self.ctx.writelns(self.lines) del self.lines
def get_tag_template(language, domain, crossref): TEMPLATES = { ('py', 'class', False): '.. py:class:: %s\n', ('py', 'module', False): '.. py:module:: %s.%s\n', ('py', 'currentmodule', False): '.. py:currentmodule:: %s\n', ('cpp', 'class', False): '.. cpp:class:: %s\n', ('py', 'class', True): ' :py:class:`%s <%s.%s>`', ('cpp', 'class', True): ' :cpp:class:`%s <%s>`', } try: return TEMPLATES[(language, domain, crossref)] except KeyError: raise EmitError("Invalid language, domain, crossref combination for" "rst documentation\n" "language = %s, type = %s" % (language, domain, crossref))
def get_range_base_type_name(range_type): base_type = get_range_base_type_spec(range_type) if isinstance(base_type, IntTypeSpec): ptype = 'int' for m_min, m_max in range_type.ranges: pmax = None if m_max == 'max': pmax = range_type.base.max else: if m_max is not None: pmax = m_max if types.yang_type_specs['uint64'].max == pmax: ptype = 'long' elif isinstance(base_type, Decimal64TypeSpec): ptype = 'Decimal64' else: raise EmitError('Unknown range type') return ptype
def get_tag_template(language, domain, crossref): templates = { ('py', 'class', False): '.. py:class:: %s\n', ('py', 'class', True): ' :py:class:`%s <%s.%s>`', ('py', 'module', False): '.. py:module:: %s.%s\n', ('py', 'currentmodule', False): '.. py:currentmodule:: %s\n', ('cpp', 'class', False): '.. cpp:class:: %s\n', ('cpp', 'class', True): ' :cpp:class:`%s <%s>`', ('go', 'class', False): '.. go:struct:: %s()\n', ('go', 'class', True): ' :go:struct:`%s <%s>`', ('go', 'module', False): '.. go:package:: %s.%s\n', ('go', 'currentmodule', False): '.. go:package:: %s\n', } try: return templates[(language, domain, crossref)] except KeyError: raise EmitError("Invalid language, domain, crossref combination for" "rst documentation\n" "language = %s, domain = %s, crossref = %s" % (language, domain, crossref))
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
def _check_tab_leak(self): end_tab = self.ctx.lvl if self._start_tab.pop() != end_tab: raise EmitError('Tab leak !!!')
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