Exemplo n.º 1
0
    def emit(self, ctx, modules, fd):
        """Main control function.

        Set up the top-level parts of the stylesheet, then process
        recursively all nodes in all data trees, and finally emit the
        serialized stylesheet.
        """
        for (epos, etag, eargs) in ctx.errors:
            if error.is_error(error.err_level(etag)):
                raise error.EmitError("JSONXSL plugin needs a valid module")
        self.real_prefix = unique_prefixes(ctx)
        self.top_names = []
        for m in modules:
            self.top_names.extend([c.arg for c in m.i_children if c.keyword not in ("rpc", "notification")])
        tree = ET.ElementTree(ss)
        ET.SubElement(ss, "output", method="text")
        xsltdir = os.environ.get("PYANG_XSLT_DIR", "/usr/local/share/yang/xslt")
        ET.SubElement(ss, "include", href=xsltdir + "/jsonxsl-templates.xsl")
        ET.SubElement(ss, "strip-space", elements="*")
        nsmap = ET.SubElement(ss, "template", name="nsuri-to-module")
        ET.SubElement(nsmap, "param", name="uri")
        choo = ET.SubElement(nsmap, "choose")
        for module in self.real_prefix.keys():
            ns_uri = module.search_one("namespace").arg
            ss.attrib["xmlns:" + self.real_prefix[module]] = ns_uri
            when = ET.SubElement(choo, "when", test="$uri='" + ns_uri + "'")
            self.xsl_text(module.i_modulename, when)
            self.process_module(module)
        if sys.version > "3":
            tree.write(fd, encoding="unicode", xml_declaration=True)
        elif sys.version > "2.7":
            tree.write(fd, encoding="UTF-8", xml_declaration=True)
        else:
            tree.write(fd, encoding="UTF-8")
Exemplo n.º 2
0
    def emit(self, ctx, modules, fd):
        """Main control function.
        """
        self.real_prefix = unique_prefixes(ctx)

        for m in modules:
            # m.pprint()
            # statements.print_tree(m)
            proto = Protobuf(m.i_modulename)
            proto.set_headers(m.i_modulename)
            self.process_substatements(m, proto, None)
            self.process_children(m, proto, None)
            out = proto.print_proto()
            for i in out:
                fd.write(i)
Exemplo n.º 3
0
    def emit(self, ctx, modules, fd):
        """Main control function.
        """
        self.real_prefix = unique_prefixes(ctx)

        for m in modules:
            # m.pprint()
            # statements.print_tree(m)
            proto = Protobuf(m.i_modulename)
            proto.set_headers(m.i_modulename)
            self.process_substatements(m, proto, None)
            self.process_children(m, proto, None)
            out = proto.print_proto()
            for i in out:
                fd.write(i)
Exemplo n.º 4
0
 def emit(self, ctx, modules, fd):
     """Main control function.
     """
     for (epos, etag, eargs) in ctx.errors:
         if error.is_error(error.err_level(etag)):
             raise error.EmitError("JTOX plugin needs a valid module")
     tree = {}
     mods = {}
     annots = {}
     for m,p in unique_prefixes(ctx).items():
         mods[m.i_modulename] = [p, m.search_one("namespace").arg]
     for module in modules:
         for ann in module.search(("ietf-yang-metadata", "annotation")):
             typ = ann.search_one("type")
             annots[module.arg + ":" + ann.arg] = (
                 "string" if typ is None else self.base_type(typ))
     for module in modules:
         self.process_children(module, tree, None)
     json.dump({"modules": mods, "tree": tree, "annotations": annots}, fd)
Exemplo n.º 5
0
 def emit(self, ctx, modules, fd):
     """Main control function.
     """
     for (epos, etag, eargs) in ctx.errors:
         if error.is_error(error.err_level(etag)):
             raise error.EmitError("JTOX plugin needs a valid module")
     tree = {}
     mods = {}
     annots = {}
     for m, p in unique_prefixes(ctx).items():
         mods[m.i_modulename] = [p, m.search_one("namespace").arg]
     for module in modules:
         for ann in module.search(("ietf-yang-metadata", "annotation")):
             typ = ann.search_one("type")
             annots[module.arg + ":" + ann.arg] = ("string" if typ is None
                                                   else self.base_type(typ))
     for module in modules:
         self.process_children(module, tree, None)
     json.dump({"modules": mods, "tree": tree, "annotations": annots}, fd)
Exemplo n.º 6
0
    def emit(self, ctx, modules, fd):
        """Main control function.

        Set up the top-level parts of the stylesheet, then process
        recursively all nodes in all data trees, and finally emit the
        serialized stylesheet.
        """
        for (epos, etag, eargs) in ctx.errors:
            if error.is_error(error.err_level(etag)):
                raise error.EmitError("JSONXSL plugin needs a valid module")
        self.real_prefix = unique_prefixes(ctx)
        self.top_names = []
        for m in modules:
            self.top_names.extend([
                c.arg for c in m.i_children
                if c.keyword not in ("rpc", "notification")
            ])
        tree = ET.ElementTree(ss)
        ET.SubElement(ss, "output", method="text")
        xsltdir = os.environ.get("PYANG_XSLT_DIR",
                                 "/usr/local/share/yang/xslt")
        ET.SubElement(ss, "include", href=xsltdir + "/jsonxsl-templates.xsl")
        ET.SubElement(ss, "strip-space", elements="*")
        nsmap = ET.SubElement(ss, "template", name="nsuri-to-module")
        ET.SubElement(nsmap, "param", name="uri")
        choo = ET.SubElement(nsmap, "choose")
        for module in self.real_prefix.keys():
            ns_uri = module.search_one("namespace").arg
            ss.attrib["xmlns:" + self.real_prefix[module]] = ns_uri
            when = ET.SubElement(choo, "when", test="$uri='" + ns_uri + "'")
            self.xsl_text(module.i_modulename, when)
            self.process_module(module)
        if sys.version > "3":
            tree.write(fd, encoding="unicode", xml_declaration=True)
        elif sys.version > "2.7":
            tree.write(fd, encoding="UTF-8", xml_declaration=True)
        else:
            tree.write(fd, encoding="UTF-8")