Skip to content

Commit

Permalink
Task: Add orator schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jairojair committed Sep 26, 2018
1 parent 9bdd896 commit d26bb1d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@ __pycache__
site
dist
*.egg-info
build
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -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 .
Expand Down
8 changes: 8 additions & 0 deletions 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"]
24 changes: 24 additions & 0 deletions 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)
69 changes: 0 additions & 69 deletions py_database_url/py_database_url.py

This file was deleted.

24 changes: 24 additions & 0 deletions 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
17 changes: 16 additions & 1 deletion 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",
Expand Down
16 changes: 8 additions & 8 deletions tests/test_parse.py → 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():
Expand All @@ -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"
Expand All @@ -36,4 +36,4 @@ def test_config_database_url_orator():
}
}

assert config.orator == expected
assert orator() == expected

0 comments on commit d26bb1d

Please sign in to comment.