diff --git a/.gitignore b/.gitignore index 5e4e33f..1acaacd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ __pycache__ site dist *.egg-info +build diff --git a/Makefile b/Makefile index 9c62d3e..8dc73d9 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ clean: @rm -rf "py_database_url.egg-info" tests: clean fmt lint - @PYTHONPATH=py_database_url pytest --cov=py_database_url tests/ + @pytest --cov=py_database_url lint: @pycodestyle --ignore=E501 . diff --git a/py_database_url/__init__.py b/py_database_url/__init__.py new file mode 100644 index 0000000..1f463cc --- /dev/null +++ b/py_database_url/__init__.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- + +from .base import parse +from .schemas import orator + +__version__ = "0.0.4" + +__all__ = ["parse", "orator"] diff --git a/py_database_url/base.py b/py_database_url/base.py new file mode 100644 index 0000000..5771ae3 --- /dev/null +++ b/py_database_url/base.py @@ -0,0 +1,24 @@ +import os + +try: + # Python 3 + from urllib.parse import urlparse + +except ImportError: + # Python 2 + from urlparse import urlparse + + +DEFAULT_ENV = "DATABASE_URL" + + +def parse(url=None): + """ + Parses a database URL. + """ + + if url: + return urlparse(url) + + url = os.environ.get(DEFAULT_ENV) + return urlparse(url) diff --git a/py_database_url/py_database_url.py b/py_database_url/py_database_url.py deleted file mode 100644 index 5032ad0..0000000 --- a/py_database_url/py_database_url.py +++ /dev/null @@ -1,69 +0,0 @@ -# -*- coding: utf-8 -*- - -import os - -try: - # Python 3 - from urllib.parse import urlparse - -except ImportError: - # Python 2 - from urlparse import urlparse - - -class Config(object): - """ - Describe class - """ - - def __init__(self): - pass - - def __get_database_url_var(self): - """ - Describe - """ - return os.environ["DATABASE_URL"] - - def config(self): - """ - Get database config from environment var (DATABASE_URL) automatically. - """ - pass - - def parse(self, url=None): - """ - Parses a database URL. - """ - - config = {} - - if url is None: - return - - return urlparse(url) - - @property - def orator(self): - """ - Method to parser for orator format. - """ - - url = self.__get_database_url_var() - url = self.parse(url) - - config = { - f"{url.scheme}": { - "driver": url.scheme, - "host": url.hostname, - "database": url.path[1:], - "user": url.username, - "password": url.password, - "prefix": "", - } - } - - return config - - -config = Config() diff --git a/py_database_url/schemas.py b/py_database_url/schemas.py new file mode 100644 index 0000000..04b93d8 --- /dev/null +++ b/py_database_url/schemas.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +from .base import parse + + +def orator(url=None, prefix=None): + """ + Function to parser for orator format. + """ + + url = parse(url) + + config = { + f"{url.scheme}": { + "driver": url.scheme, + "host": url.hostname, + "database": url.path[1:], + "user": url.username, + "password": url.password, + "prefix": prefix or "", + } + } + + return config diff --git a/setup.py b/setup.py index caf1c85..b7e74b6 100644 --- a/setup.py +++ b/setup.py @@ -1,9 +1,24 @@ +import os +import re + from setuptools import find_packages, setup + +def get_version(package): + """ + Return package version as listed in `__version__` in `init.py`. + """ + init_py = open(os.path.join(package, "__init__.py")).read() + return re.search("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1) + + +version = get_version("py_database_url") + + setup( name="py-database-url", description="A universal database URLs for Python applications.", - version="0.0.2", + version=version, url="https://github.com/jairojair/py-database-url", license="MIT", author="Jairo Jair", diff --git a/tests/test_parse.py b/test_all.py similarity index 75% rename from tests/test_parse.py rename to test_all.py index 3f46956..75033a9 100644 --- a/tests/test_parse.py +++ b/test_all.py @@ -1,11 +1,11 @@ -import pytest +from py_database_url import parse, orator -from py_database_url import config - -def test_config_without_url(): - - assert config.parse() is None +def test_parse_database_url_wrong(): + """ + Describe + """ + pass def test_parse_database_url(): @@ -15,7 +15,7 @@ def test_parse_database_url(): url = "postgres://user:password@hostname:5432/db-name" - ret = config.parse(url) + ret = parse(url) assert ret.username == "user" assert ret.password == "password" @@ -36,4 +36,4 @@ def test_config_database_url_orator(): } } - assert config.orator == expected + assert orator() == expected