Skip to main content

Flask-style routing for URIs

Project description

URIRouter

Creates an object-oriented structure and decorators similar to Flask, but for handling internal URIs.

Format

URIRouter uses a minimum of two parts to add a route, a scheme and parameter(s). Optionally, there can also be a net location. Any parameters will be treated as kwargs and passed to the function that the route decorates.

{{scheme}}:///{{path/more-path}}?{{param1=something}}&{{param2=somethingelse}} uses .route("/path/more/path") and will add to kwargs the parameters {'param1':'something', 'param2':'somethingelse'}

{{scheme}}://{netloc}/{{path/more-path}}?{{param1=something}}&{{param2=somethingelse}}

Example Application Class

URIRouter can be used to handle any in-bound URIs, but it is intended to be used with a structure that can accept events with callbacks, such as:

from urirouter import URIRouter
from PySide6.QtCore import QEvent, QUrl
from PySide6.QtWidgets import QApplication


router = URIRouter("myappscheme")


class CustomURIApplication(QApplication):

    def __init__(self, router: URIRouter, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.router = router

    def event(self, e):
        """Handle macOS FileOpen events or pass to super."""
        if e.type() == QEvent.FileOpen:
            url: QUrl = e.url()
            if url.isValid():
                self.router.handle(url.url())
            else:
                print(f"application received invalid uri: {url.errorString()}")
        else:
            return super().event(e)
        return True

if __name__ == "__main__":
    app = CustomURIApplication(router)
    # see quickstart to install routes

Quickstart

Note: the quickstart requires a way to handle in-bound URIs, either with a NSApplication (such as with pyobjc) or a QApplication (such as PySide6). See the Example Application Class section above for an example with QApplication.

from urirouter import URIRouter


router = URIRouter("myappscheme")


@router.route("/")
def home(*args, **kwargs):
    print("in home")
    print(kwargs) # any parameters contained in the URI are passed to the function in kwargs


if __name__ == "__main__":
    inbound_uri = ... # get/handle in-bound URIs (see Example Application Class above)
    router.handle(inbound_uri)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

URIRouter-1.0.3.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file URIRouter-1.0.3.tar.gz.

File metadata

  • Download URL: URIRouter-1.0.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.9

File hashes

Hashes for URIRouter-1.0.3.tar.gz
Algorithm Hash digest
SHA256 9d2a5fc9a2ddd62acc65da9a469c90fc501b09a10fc45cbed13dfe757195470c
MD5 a7dc7ef734a6ee773571c4177c42bc09
BLAKE2b-256 8f245c77a2b1a7149198a8ad394333e2abf09b6f1b242d7cdb8d9e515c7009d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page