# dsnparse
Parse [dsn connection url strings](http://en.wikipedia.org/wiki/Data_source_name). Responsible for parsing dsn strings in projects like [prom](https://github.com/jaymon/prom) and [morp](https://github.com/jaymon/morp).
This is a generic version of [dj-database-url](https://github.com/kennethreitz/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:
```python
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:
```python
r = dsnparse.parse_environ('ENVIRONMENT_VARIABLE_NAME')
```
I tried to keep the interface very similar to [urlparse](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse) so it will feel familiar to use.
## Customizing
By default, `dsnparse.parse(dsn)` returns a `ParseResult` instance, but that can be customized:
```python
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"
Raw data
{
"_id": null,
"home_page": null,
"name": "dsnparse",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "dsn url parser database configuration",
"author": null,
"author_email": "Jay Marcyes <jay@marcyes.com>",
"download_url": "https://files.pythonhosted.org/packages/a1/1f/f906185824fe38ca95131a736ec5a78774c437066747fed131f1debcedaa/dsnparse-0.4.0.tar.gz",
"platform": null,
"description": "# dsnparse\n\nParse [dsn connection url strings](http://en.wikipedia.org/wiki/Data_source_name). Responsible for parsing dsn strings in projects like [prom](https://github.com/jaymon/prom) and [morp](https://github.com/jaymon/morp).\n\nThis is a generic version of [dj-database-url](https://github.com/kennethreitz/dj-database-url).\n\nSo, now you can create dsns like this:\n\n scheme://user:pass@host:port/path?query=query_val#fragment\n\nFor example, let's look at a prom dsn:\n\n prom.interface.postgres.Interface://testuser:testpw@localhost/testdb\n\nNow let's parse it:\n\n```python\nimport dsnparse\n\ndsn = \"prom.interface.postgres.Interface://testuser:testpw@localhost:1234/testdb\"\nr = dsnparse.parse(dsn)\n\nprint(r.scheme) # prom.interface.postgres.Interface\nprint(r.username) # testuser\nprint(r.password) # testpw\nprint(r.host) # localhost\nprint(r.port) # 1234\nprint(r.hostloc) # localhost:1234\nprint(r.paths) # ['testdb']\n```\n\nAlso, dsnparse can easily use environment variables:\n\n```python\nr = dsnparse.parse_environ('ENVIRONMENT_VARIABLE_NAME')\n```\n\nI tried to keep the interface very similar to [urlparse](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse) so it will feel familiar to use.\n\n\n## Customizing\n\nBy default, `dsnparse.parse(dsn)` returns a `ParseResult` instance, but that can be customized:\n\n```python\nimport dsnparse\n\nclass MyResult(dsnparse.ParseResult):\n def configure(self):\n # expose an interface property\n self.interface = self.scheme\n\ndsn = \"Interface://testuser:testpw@localhost:1234/testdb\"\nr = dsnparse.parse(dsn, parse_class=MyResult)\nprint(isinstance(r, MyResult)) # True\nprint(r.interface) # Interface\n```\n\n\n## Install\n\nUse pip:\n\n pip install dsnparse\n\nor use pip with github:\n\n pip install -U \"git+https://github.com/Jaymon/dsnparse#egg=dsnparse\"\n\n",
"bugtrack_url": null,
"license": null,
"summary": "parse dsn urls",
"version": "0.4.0",
"project_urls": {
"Homepage": "http://github.com/Jaymon/dsnparse",
"Repository": "https://github.com/Jaymon/dsnparse"
},
"split_keywords": [
"dsn",
"url",
"parser",
"database",
"configuration"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "922d77561298370e748428a2b5b753ab41c0b8c946b4e2d7a5c1f0b81ff5dd42",
"md5": "46630a45a453e684c1f1646f617afecc",
"sha256": "67db9977a53cf78b1fb5d3d5641cd55442370560935fdf821edb587946b8c67d"
},
"downloads": -1,
"filename": "dsnparse-0.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "46630a45a453e684c1f1646f617afecc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 9236,
"upload_time": "2025-10-13T23:48:40",
"upload_time_iso_8601": "2025-10-13T23:48:40.285989Z",
"url": "https://files.pythonhosted.org/packages/92/2d/77561298370e748428a2b5b753ab41c0b8c946b4e2d7a5c1f0b81ff5dd42/dsnparse-0.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "a11ff906185824fe38ca95131a736ec5a78774c437066747fed131f1debcedaa",
"md5": "45cd6e8ca63aaa0ae7f96c08e9f02548",
"sha256": "301a251ef74832dccfb3535650b270749c8c7ef49437847a4cd3ed353b873ff2"
},
"downloads": -1,
"filename": "dsnparse-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "45cd6e8ca63aaa0ae7f96c08e9f02548",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 9268,
"upload_time": "2025-10-13T23:48:39",
"upload_time_iso_8601": "2025-10-13T23:48:39.309620Z",
"url": "https://files.pythonhosted.org/packages/a1/1f/f906185824fe38ca95131a736ec5a78774c437066747fed131f1debcedaa/dsnparse-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-10-13 23:48:39",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Jaymon",
"github_project": "dsnparse",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "dsnparse"
}