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
Release history Release notifications | RSS feed
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.2.1.tar.gz
(9.4 kB
view details)
File details
Details for the file dsnparse-0.2.1.tar.gz
.
File metadata
- Download URL: dsnparse-0.2.1.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90956235967569c875994adbad0928c347a1592b5fc8ecd9c1b1a20397dae697 |
|
MD5 | 606324175cd635f5944f137d80834c9c |
|
BLAKE2b-256 | e487f01ae16fab0e55253750cd77f225d6dcadbf33332985620fa653356553a3 |