Ejemplo n.º 1
0
    def __init__(self, app, manager):
        self.app = app
        self.highlight = Highlight()
        self.manager = manager

        route("/json", "GET", self.api_index)

        self.manager.add_api_route(
            "GET /test/<test variable>",
            """A test route. Doesn't actually exist, just here for
            illustration purposes.
            """)

        self.manager.add_api_route(
            "GET /json", """<p> The list of routes in JSON format.
    You could use this for service or capability discovery, for example.
</p>

<div class="ui horizontal icon divider">
    <i class="circular code icon"></i>
</div>

{0}
            """.format(
                self.highlight.highlight(
                    """
{
    "routes": {
        "GET /test/<test variable>": "A test route.",
        "GET /json": "The list of routes in JSON format. You could use this
                      for service or capability discovery, for example."
    }
}
            """, "json")))
Ejemplo n.º 2
0
    def __init__(self, app, manager):
        self.app = app
        self.highlight = Highlight()
        self.manager = manager

        route("/highlight.css", ["GET", "POST"], self.highlight_css)
        route("/static/<path:path>", ["GET", "POST"], self.static)
        route("/static/", ["GET", "POST"], self.static_403)
        route("/static", ["GET", "POST"], self.static_403)
        route("/.well-known/keybase.txt", ["GET", "POST"], self.static_keybase)
Ejemplo n.º 3
0
class ApiManager(object, metaclass=Singleton):
    apis = {}

    highlighter = None
    markdowner = None

    def __init__(self):
        self.highlighter = Highlight()
        self.markdowner = markdown2.Markdown(extras=MARKDOWN_EXTRAS)

    def add_route(self,
                  route: "The API route itself",
                  method: "The HTTP method for this route",
                  text: "Text description for JSON, should be short",
                  markdown: "Name of Markdown file for HTML description"):
        api_route = {}

        if route in self.apis:
            api_route = self.apis[route]

        if method in api_route:
            return False  # Already exists

        if not os.path.exists("markdown/{0}.md".format(markdown)):
            return False

        description = self.markdowner.convert(
            open("markdown/{0}.md".format(markdown), "r").read()
        )

        code = ""
        if (os.path.exists("markdown/json/{0}.json".format(markdown)) and
                os.path.exists("markdown/xml/{0}.xml".format(markdown))):
            json_h = self.highlighter.highlight(
                open("markdown/json/{0}.json".format(markdown), "r").read(),
                "json"
            )

            xml_h = self.highlighter.highlight(
                open("markdown/xml/{0}.xml".format(markdown), "r").read(),
                "xml"
            )

            code = CODE_TEMPLATE.format(json=json_h, xml=xml_h)

        api_route[method] = {
            "text": text,
            "html": TEMPLATE.format(description=description, code=code)
        }

        self.apis[route] = api_route

        return True
Ejemplo n.º 4
0
class Routes(object):

    def __init__(self, app, manager):
        self.app = app
        self.highlight = Highlight()
        self.manager = manager

        route("/highlight.css", ["GET", "POST"], self.highlight_css)
        route("/static/<path:path>", ["GET", "POST"], self.static)
        route("/static/", ["GET", "POST"], self.static_403)
        route("/static", ["GET", "POST"], self.static_403)
        route("/.well-known/keybase.txt", ["GET", "POST"],
              self.static_keybase)

    def highlight_css(self):
        response.add_header("Content-Type", "text/css")
        return self.highlight.get_css()

    def static(self, path):
        return static_file(path, root="static")

    def static_403(self):
        abort(403, "You may not list the static files.")

    def static_keybase(self):
        return static_file("keybase.txt", root="static")
Ejemplo n.º 5
0
    def __init__(self, app, manager):
        self.app = app
        self.highlight = Highlight()
        self.manager = manager

        route("/highlight.css", ["GET", "POST"], self.highlight_css)
        route("/static/<path:path>", ["GET", "POST"], self.static)
        route("/static/", ["GET", "POST"], self.static_403)
        route("/static", ["GET", "POST"], self.static_403)
        route("/.well-known/keybase.txt", ["GET", "POST"],
              self.static_keybase)
Ejemplo n.º 6
0
class Routes(object):
    def __init__(self, app, manager):
        self.app = app
        self.highlight = Highlight()
        self.manager = manager

        route("/json", "GET", self.api_index)

        self.manager.add_api_route(
            "GET /test/<test variable>",
            """A test route. Doesn't actually exist, just here for
            illustration purposes.
            """,
        )

        self.manager.add_api_route(
            "GET /json",
            """<p> The list of routes in JSON format.
    You could use this for service or capability discovery, for example.
</p>

<div class="ui horizontal icon divider">
    <i class="circular code icon"></i>
</div>

{0}
            """.format(
                self.highlight.highlight(
                    """
{
    "routes": {
        "GET /test/<test variable>": "A test route.",
        "GET /json": "The list of routes in JSON format. You could use this
                      for service or capability discovery, for example."
    }
}
            """,
                    "json",
                )
            ),
        )

    def api_index(self):
        return {"routes": self.manager.api_routes}
Ejemplo n.º 7
0
class Routes(object):
    def __init__(self, app, manager):
        self.app = app
        self.highlight = Highlight()
        self.manager = manager

        route("/highlight.css", ["GET", "POST"], self.highlight_css)
        route("/static/<path:path>", ["GET", "POST"], self.static)
        route("/static/", ["GET", "POST"], self.static_403)
        route("/static", ["GET", "POST"], self.static_403)
        route("/.well-known/keybase.txt", ["GET", "POST"], self.static_keybase)

    def highlight_css(self):
        response.add_header("Content-Type", "text/css")
        return self.highlight.get_css()

    def static(self, path):
        return static_file(path, root="static")

    def static_403(self):
        abort(403, "You may not list the static files.")

    def static_keybase(self):
        return static_file("keybase.txt", root="static")
Ejemplo n.º 8
0
 def __init__(self):
     self.highlighter = Highlight()
     self.markdowner = markdown2.Markdown(extras=MARKDOWN_EXTRAS)