Example #1
0
    def test_convert_21_22(self):
        if os.path.exists(SERVICE_22):
            os.unlink(SERVICE_22)

        try:
            subprocess.check_output([CONVERT_21_22, '-f', SERVICE_21,
                                     '-o', SERVICE_22])
            s = ServiceDef()
            s.load(SERVICE_22)

            t = s.find('#/types/type3')
            t.validate({'key1': 15})

        finally:
            if os.path.exists(SERVICE_22):
                os.unlink(SERVICE_22)
Example #2
0
class TestReschemaInvalidRef(unittest.TestCase):

    def setUp(self):
        self.sd = ServiceDef()
        self.sd.load(SERVICE_DEF_INVALID_REF)

    def test_invalid_ref_in_property(self):
        """
        testing when one property's ref consists of undefined types,
        an invalid reference exception should be raised, such as below:
        properties:
           property: { $ref: '#/types/blah' }
        """
        with self.assertRaises(reschema.exceptions.InvalidReference):
            schema = self.sd.resources.values()[0].properties['name']
            RefSchemaProxy(schema, None)

    def test_invalid_ref_in_links(self):
        """
        testing when one property's ref consists of undefined resources,
        an invalid reference exception should be raised. such as below:
        properties:
        links:
          self: { path: '$/test_invalid_ref_in_lnks'}
          params:
             id:
                $ref: '#/types/does_not_exist'
        """
        with self.assertRaises(reschema.exceptions.InvalidReference):
            resource = self.sd.resources.values()[0]
            title = "%s v%s %s" % (self.sd.title, self.sd.version,
                                   self.sd.status)
            htmldoc = reschema.html.Document(title, printable=False)
            r2h = ResourceToHtml(resource, htmldoc.content,
                                 htmldoc.menu.add_submenu(),
                                 "http://{device}/{root}",
                                 None)
            baseid = html_str_to_id(r2h.schema.fullid(True))
            div = r2h.container.div(id=baseid)
            r2h.menu.add_item(r2h.schema.name, href=div)
            r2h.process_links(div, baseid)
Example #3
0
 def test_load_schema_21(self):
     s = ServiceDef()
     with self.assertRaises(UnsupportedSchema):
         s.load(SERVICE_21)
Example #4
0
    def run(self):
        options = self.options

        servicedef = ServiceDef()
        servicedef.load(options.filename)
        self.servicedefmgr.add(servicedef)

        for related in (options.related or []):
            relateddef = ServiceDef()
            relateddef.load(related)
            self.servicedefmgr.add(relateddef)

        title = "%s v%s %s" % (servicedef.title, servicedef.version,
                               servicedef.status)

        name = (self.options.urlname or servicedef.name)
        relname = os.path.join(name, servicedef.version, 'service')

        outdir = options.outdir

        fullname = os.path.abspath(os.path.join(outdir, relname))
        fulldir = os.path.dirname(fullname)

        if not os.path.exists(fulldir):
            os.makedirs(fulldir)

        apiroot = options.apiroot or ("/api/%s/%s" %
                                      (servicedef.name, servicedef.version))

        # HTML version
        if options.html:
            html = fullname + '.html'
            if os.path.exists(html):
                os.remove(html)

            htmldoc = reschema.html.Document(title, printable=False)
            htmldoc.header.a(href="http://www.riverbed.com", cls="headerimg")
            hl = htmldoc.header.div(cls="headerleft")
            breadcrumbs = hl.div(cls="breadcrumbs")
            breadcrumbs.a(href="../../index.html").text = "apis"
            breadcrumbs.span().text = unescape(" » ")
            breadcrumbs.a(href=("../index.html")).text = name
            breadcrumbs.span().text = unescape(" » {}"
                                               "".format(servicedef.version))
            hl.div(cls="headertitle").text = title
            htmldoc.header.span(cls="headerright").text = (
                "Created %s" %
                datetime.datetime.now().strftime("%b %d, %Y at %I:%M %p"))

            r2h = ServiceDefToHtml(servicedef,
                                   htmldoc.content,
                                   htmldoc.menu,
                                   device=options.device,
                                   apiroot=apiroot,
                                   options=Options(printable=False,
                                                   json=(not options.nojson),
                                                   xml=(not options.noxml),
                                                   apiroot=apiroot,
                                                   docroot=outdir))
            r2h.process()
            htmldoc.write(html)
            print("Wrote %s" % html)

        # PDF
        if options.pdf:
            # First make a printable HTML
            phtml = fullname + '-printable.html'
            if os.path.exists(phtml):
                os.remove(phtml)

            htmldoc = reschema.html.Document(title, printable=True)
            r2h = ServiceDefToHtml(servicedef,
                                   htmldoc.content,
                                   htmldoc.menu,
                                   device=options.device,
                                   apiroot=apiroot,
                                   options=Options(printable=True,
                                                   json=(not options.nojson),
                                                   xml=(not options.noxml),
                                                   apiroot=apiroot,
                                                   docroot=outdir))
            r2h.process()
            htmldoc.write(phtml)
            print("Wrote %s" % phtml)

            ### PDF
            if options.wkhtmltopdf is not None:
                wkhtmltopdf = options.wkhtmltopdf
            else:
                wkhtmltopdf = distutils.spawn.find_executable("wkhtmltopdf")
                if not wkhtmltopdf:
                    if not "WKHTMLTOPDF" in os.environ:
                        raise ReschemaDocException(
                            "Cannot find 'wkhtmltopdf' in path, "
                            "use -w <path> or set WKHTMLTOPDF env variable")
                    wkhtmltopdf = os.environ['WKHTMLTOPDF']

            args = [wkhtmltopdf, "--version"]
            lines = subprocess.check_output(args).split('\n')
            version = None
            for line in lines:
                g = re.search("wkhtmltopdf ([0-9]+)\.([0-9]+)", line)
                if g:
                    version = int(g.group(2))
                    break

            if version is None:
                print("WARNING: Could not determine wkhtmltopdf version, "
                      "assuming latest")
            else:
                print("wkhtmltopdf version %d" % version)

            if version is None or version >= 10:
                tocarg = "toc"
                coverarg = "cover"
            else:
                tocarg = "--toc"
                coverarg = "--cover"

            # create a cover apge
            cover = fullname + '-cover.html'
            cover_base = reschema.html.HTMLElement('html')
            cover_body = cover_base.body()
            cover_body.h1().text = title
            if options.copyright:
                cover_body.p().text = "Copyright \xa9 " + str(
                    options.copyright)
            created = datetime.datetime.now().strftime("%b %d, %Y at %I:%m %p")
            cover_body.p().text = "Created %s" % created

            f = open(cover, "w")
            f.write(reschema.html.ET.tostring(cover_base, method="html"))
            f.close()

            pdf = fullname + '.pdf'
            if os.path.exists(pdf):
                os.remove(pdf)

            args = [
                wkhtmltopdf, '--title', title, coverarg, cover, tocarg,
                '--toc-header-text', 'Contents', '--footer-center', '[page]',
                phtml, pdf
            ]
            subprocess.check_call(args)
            # Fix for 'illegal byte sequence'
            os.environ['LC_TYPE'] = 'C'
            os.environ['LANG'] = 'C'
            args = ['sed', '-i.bak', '-e', 's/#00//g', pdf]
            subprocess.check_call(args)
            print("Wrote %s" % pdf)
            os.remove(pdf + ".bak")
Example #5
0
    def run(self):
        options = self.options

        servicedef = ServiceDef()
        servicedef.load(options.filename)
        self.servicedefmgr.add(servicedef)

        for related in (options.related or []):
            relateddef = ServiceDef()
            relateddef.load(related)
            self.servicedefmgr.add(relateddef)

        title = "%s v%s %s" % (servicedef.title, servicedef.version,
                               servicedef.status)

        name = (self.options.urlname or servicedef.name)
        relname = os.path.join(name, servicedef.version, 'service')

        outdir = options.outdir

        fullname = os.path.abspath(os.path.join(outdir, relname))
        fulldir = os.path.dirname(fullname)

        if not os.path.exists(fulldir):
            os.makedirs(fulldir)

        apiroot = options.apiroot or ("/api/%s/%s" %
                                      (servicedef.name, servicedef.version))

        # HTML version
        if options.html:
            html = fullname + '.html'
            if os.path.exists(html):
                os.remove(html)

            h = HTMLParser.HTMLParser()

            htmldoc = reschema.html.Document(title, printable=False)
            htmldoc.header.a(href="http://www.riverbed.com", cls="headerimg")
            hl = htmldoc.header.div(cls="headerleft")
            breadcrumbs = hl.div(cls="breadcrumbs")
            breadcrumbs.a(href="../../index.html").text = "apis"
            breadcrumbs.span().text = h.unescape(" &raquo; ")
            breadcrumbs.a(href=("../index.html")).text = name
            breadcrumbs.span().text = h.unescape(" &raquo; %s" % servicedef.version)
            hl.div(cls="headertitle").text = title
            htmldoc.header.span(cls="headerright").text = (
                "Created %s" %
                datetime.datetime.now().strftime("%b %d, %Y at %I:%M %p"))

            r2h = ServiceDefToHtml(servicedef, htmldoc.content, htmldoc.menu,
                                   device=options.device,
                                   apiroot=apiroot,
                                   options=Options(printable=False,
                                                   json=(not options.nojson),
                                                   xml=(not options.noxml),
                                                   apiroot=apiroot,
                                                   docroot=outdir))
            r2h.process()
            htmldoc.write(html)
            print "Wrote %s" % html

        # PDF
        if options.pdf:
            # First make a printable HTML
            phtml = fullname + '-printable.html'
            if os.path.exists(phtml):
                os.remove(phtml)

            htmldoc = reschema.html.Document(title, printable=True)
            r2h = ServiceDefToHtml(servicedef, htmldoc.content, htmldoc.menu,
                                   device=options.device,
                                   apiroot=apiroot,
                                   options=Options(printable=True,
                                                   json=(not options.nojson),
                                                   xml=(not options.noxml),
                                                   apiroot=apiroot,
                                                   docroot=outdir))
            r2h.process()
            htmldoc.write(phtml)
            print "Wrote %s" % phtml

            ### PDF
            if options.wkhtmltopdf is not None:
                wkhtmltopdf = options.wkhtmltopdf
            else:
                wkhtmltopdf = distutils.spawn.find_executable("wkhtmltopdf")
                if not wkhtmltopdf:
                    if not "WKHTMLTOPDF" in os.environ:
                        raise ReschemaDocException(
                            "Cannot find 'wkhtmltopdf' in path, "
                            "use -w <path> or set WKHTMLTOPDF env variable")
                    wkhtmltopdf = os.environ['WKHTMLTOPDF']

            args = [wkhtmltopdf, "--version"]
            lines = subprocess.check_output(args).split('\n')
            version = None
            for line in lines:
                g = re.search("wkhtmltopdf ([0-9]+)\.([0-9]+)", line)
                if g:
                    version = int(g.group(2))
                    break

            if version is None:
                print ("WARNING: Could not determine wkhtmltopdf version, "
                       "assuming latest")
            else:
                print "wkhtmltopdf version %d" % version

            if version is None or version >= 10:
                tocarg = "toc"
                coverarg = "cover"
            else:
                tocarg = "--toc"
                coverarg = "--cover"

            # create a cover apge
            cover = fullname + '-cover.html'
            cover_base = reschema.html.HTMLElement('html')
            cover_body = cover_base.body()
            cover_body.h1().text = title
            if options.copyright:
                cover_body.p().text = u"Copyright \xa9 " + unicode(options.copyright)
            created = datetime.datetime.now().strftime("%b %d, %Y at %I:%m %p")
            cover_body.p().text = "Created %s" % created

            f = open(cover, "w")
            f.write(reschema.html.ET.tostring(cover_base, method="html"))
            f.close()

            pdf = fullname + '.pdf'
            if os.path.exists(pdf):
                os.remove(pdf)

            args = [wkhtmltopdf,
                    '--title', title,
                    coverarg, cover,
                    tocarg, '--toc-header-text', 'Contents',
                    '--footer-center', '[page]',
                    phtml, pdf]
            #print ' '.join(args)
            subprocess.check_call(args)
            # Fix for 'illegal byte sequence'
            os.environ['LC_TYPE'] = 'C'
            os.environ['LANG'] = 'C'
            args = ['sed', '-i.bak', '-e', 's/#00//g', pdf]
            subprocess.check_call(args)
            print "Wrote %s" % pdf
            os.remove(pdf + ".bak")
Example #6
0
 def test_bad_yaml(self):
     with self.assertRaises(ReschemaException):
         r = ServiceDef()
         r.parse_text(yaml_snippet_bad, format='yaml')
Example #7
0
 def test_bad_json(self):
     with self.assertRaises(ReschemaException):
         r = ServiceDef()
         r.parse_text(json_snippet_bad)
Example #8
0
 def setUp(self):
     self.sd = ServiceDef()
     self.sd.load(SERVICE_DEF_INVALID_REF)
Example #9
0
 def find_by_id(self, id_):
     if id_ in self.service_map:
         return ServiceDef.create_from_file(self.service_map[id_])
     else:
         raise KeyError("Invalid id: %s" % id_)