Skip to content

Commit d26bb1d

Browse files
committed
Task: Add orator schema
1 parent 9bdd896 commit d26bb1d

File tree

8 files changed

+82
-79
lines changed

8 files changed

+82
-79
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ __pycache__
66
site
77
dist
88
*.egg-info
9+
build

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ clean:
88
@rm -rf "py_database_url.egg-info"
99

1010
tests: clean fmt lint
11-
@PYTHONPATH=py_database_url pytest --cov=py_database_url tests/
11+
@pytest --cov=py_database_url
1212

1313
lint:
1414
@pycodestyle --ignore=E501 .

py_database_url/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from .base import parse
4+
from .schemas import orator
5+
6+
__version__ = "0.0.4"
7+
8+
__all__ = ["parse", "orator"]

py_database_url/base.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
3+
try:
4+
# Python 3
5+
from urllib.parse import urlparse
6+
7+
except ImportError:
8+
# Python 2
9+
from urlparse import urlparse
10+
11+
12+
DEFAULT_ENV = "DATABASE_URL"
13+
14+
15+
def parse(url=None):
16+
"""
17+
Parses a database URL.
18+
"""
19+
20+
if url:
21+
return urlparse(url)
22+
23+
url = os.environ.get(DEFAULT_ENV)
24+
return urlparse(url)

py_database_url/py_database_url.py

-69
This file was deleted.

py_database_url/schemas.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from .base import parse
4+
5+
6+
def orator(url=None, prefix=None):
7+
"""
8+
Function to parser for orator format.
9+
"""
10+
11+
url = parse(url)
12+
13+
config = {
14+
f"{url.scheme}": {
15+
"driver": url.scheme,
16+
"host": url.hostname,
17+
"database": url.path[1:],
18+
"user": url.username,
19+
"password": url.password,
20+
"prefix": prefix or "",
21+
}
22+
}
23+
24+
return config

setup.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
import os
2+
import re
3+
14
from setuptools import find_packages, setup
25

6+
7+
def get_version(package):
8+
"""
9+
Return package version as listed in `__version__` in `init.py`.
10+
"""
11+
init_py = open(os.path.join(package, "__init__.py")).read()
12+
return re.search("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1)
13+
14+
15+
version = get_version("py_database_url")
16+
17+
318
setup(
419
name="py-database-url",
520
description="A universal database URLs for Python applications.",
6-
version="0.0.2",
21+
version=version,
722
url="https://github.com/jairojair/py-database-url",
823
license="MIT",
924
author="Jairo Jair",

tests/test_parse.py test_all.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import pytest
1+
from py_database_url import parse, orator
22

3-
from py_database_url import config
43

5-
6-
def test_config_without_url():
7-
8-
assert config.parse() is None
4+
def test_parse_database_url_wrong():
5+
"""
6+
Describe
7+
"""
8+
pass
99

1010

1111
def test_parse_database_url():
@@ -15,7 +15,7 @@ def test_parse_database_url():
1515

1616
url = "postgres://user:password@hostname:5432/db-name"
1717

18-
ret = config.parse(url)
18+
ret = parse(url)
1919

2020
assert ret.username == "user"
2121
assert ret.password == "password"
@@ -36,4 +36,4 @@ def test_config_database_url_orator():
3636
}
3737
}
3838

39-
assert config.orator == expected
39+
assert orator() == expected

0 commit comments

Comments
 (0)