Skip to main content

parse dsn urls

Project description

dsnparse

Parse dsn connection url strings. Responsible for parsing dsn strings in projects like prom and morp.

This is a generic version of dj-database-url.

So, now you can create dsns like this:

scheme://user:pass@host:port/path?query=query_val#fragment

For example, let's look at a prom dsn:

prom.interface.postgres.Interface://testuser:testpw@localhost/testdb

Now let's parse it:

import dsnparse

dsn = "prom.interface.postgres.Interface://testuser:testpw@localhost:1234/testdb"
r = dsnparse.parse(dsn)

print(r.scheme) # prom.interface.postgres.Interface
print(r.username) # testuser
print(r.password) # testpw
print(r.host) # localhost
print(r.port) # 1234
print(r.hostloc) # localhost:1234
print(r.paths) # ['testdb']

Also, dsnparse can easily use environment variables:

r = dsnparse.parse_environ('ENVIRONMENT_VARIABLE_NAME')

I tried to keep the interface very similar to urlparse so it will feel familiar to use.

Customizing

By default, dsnparse.parse(dsn) returns a ParseResult instance, but that can be customized:

import dsnparse

class MyResult(dsnparse.ParseResult):
    def configure(self):
        # expose an interface property
        self.interface = self.scheme

dsn = "Interface://testuser:testpw@localhost:1234/testdb"
r = dsnparse.parse(dsn, parse_class=MyResult)
print(isinstance(r, MyResult)) # True
print(r.interface) # Interface

Install

Use pip:

pip install dsnparse

or use pip with github:

pip install -U "git+https://github.com/Jaymon/dsnparse#egg=dsnparse"

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

dsnparse-0.4.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dsnparse-0.4.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file dsnparse-0.4.0.tar.gz.

File metadata

  • Download URL: dsnparse-0.4.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for dsnparse-0.4.0.tar.gz
Algorithm Hash digest
SHA256 301a251ef74832dccfb3535650b270749c8c7ef49437847a4cd3ed353b873ff2
MD5 45cd6e8ca63aaa0ae7f96c08e9f02548
BLAKE2b-256 a11ff906185824fe38ca95131a736ec5a78774c437066747fed131f1debcedaa

See more details on using hashes here.

File details

Details for the file dsnparse-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: dsnparse-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for dsnparse-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67db9977a53cf78b1fb5d3d5641cd55442370560935fdf821edb587946b8c67d
MD5 46630a45a453e684c1f1646f617afecc
BLAKE2b-256 922d77561298370e748428a2b5b753ab41c0b8c946b4e2d7a5c1f0b81ff5dd42

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