dsnparse


Namedsnparse JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttp://github.com/Jaymon/dsnparse
Summaryparse dsn urls
upload_time2023-06-28 23:33:15
maintainer
docs_urlNone
authorJay Marcyes
requires_python
licenseMIT
keywords dsn url parser database configuration
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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": "http://github.com/Jaymon/dsnparse",
    "name": "dsnparse",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "dsn url parser database configuration",
    "author": "Jay Marcyes",
    "author_email": "jay@marcyes.com",
    "download_url": "https://files.pythonhosted.org/packages/e4/87/f01ae16fab0e55253750cd77f225d6dcadbf33332985620fa653356553a3/dsnparse-0.2.1.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": "MIT",
    "summary": "parse dsn urls",
    "version": "0.2.1",
    "project_urls": {
        "Homepage": "http://github.com/Jaymon/dsnparse"
    },
    "split_keywords": [
        "dsn",
        "url",
        "parser",
        "database",
        "configuration"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e487f01ae16fab0e55253750cd77f225d6dcadbf33332985620fa653356553a3",
                "md5": "606324175cd635f5944f137d80834c9c",
                "sha256": "90956235967569c875994adbad0928c347a1592b5fc8ecd9c1b1a20397dae697"
            },
            "downloads": -1,
            "filename": "dsnparse-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "606324175cd635f5944f137d80834c9c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9438,
            "upload_time": "2023-06-28T23:33:15",
            "upload_time_iso_8601": "2023-06-28T23:33:15.478846Z",
            "url": "https://files.pythonhosted.org/packages/e4/87/f01ae16fab0e55253750cd77f225d6dcadbf33332985620fa653356553a3/dsnparse-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-28 23:33:15",
    "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"
}
        
Elapsed time: 0.20771s