parse dsn urls
Project description
Parse dsn connection url strings. I kept duplicating dsn parsing code for things like prom and morp, and I realized I was going to need many more dsn urls in the future so I decided to create something a little more modular.
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.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.
Example
By default, dsnparse.parse(dsn) returns a ParseResult instance, but that can be customized:
import dsnparse
class MyResult(dsnparse.ParseResult):
@classmethod
def parse(cls, dsn, **defaults):
d = super(MyResult, cls).parse(dsn, **defaults)
# d is a dict and you can customize its keys/values here
d["interface"] = d.pop("scheme")
return d
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 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
File details
Details for the file dsnparse-0.1.12.tar.gz
.
File metadata
- Download URL: dsnparse-0.1.12.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/2.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 115edcd3f0089290b018d4cb76f0e8cc31cd5a3573861bc77a62ff3f30f908e8 |
|
MD5 | 7404ea543db85961f574b55e48f638fd |
|
BLAKE2b-256 | 8275960c015635665a30c876fd5d16981fb90f23040b488267971f554568ba1a |