Skip to content

Commit

Permalink
Add table_base parameter
Browse files Browse the repository at this point in the history
Bring requirements up to date
Bring precommit config up to date
  • Loading branch information
mcarans committed Feb 15, 2024
1 parent d5662fa commit 42c8be1
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 54 deletions.
2 changes: 0 additions & 2 deletions .config/black.toml

This file was deleted.

19 changes: 9 additions & 10 deletions .config/pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ default_language_version:
python: python3.11
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-ast
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
args: [--config, .config/black.toml]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.267
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.14
hooks:
# Run the linter.
- id: ruff
args: [--config, .config/ruff.toml, --fix, --exit-non-zero-on-fix]
args: [--config, .config/ruff.toml, --fix]
# Run the formatter.
- id: ruff-format
args: [--config, .config/ruff.toml]
- repo: https://github.com/jazzband/pip-tools
rev: 6.13.0
rev: 7.3.0
hooks:
- id: pip-compile
name: pip-compile requirements.txt
Expand Down
12 changes: 7 additions & 5 deletions .config/ruff.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
line-length = 79
exclude = ["_version.py"]
ignore = [
"E501" # Line too long
]
# List of rules: https://beta.ruff.rs/docs/rules/

[lint]
# List of rules: https://docs.astral.sh/ruff/rules/
select = [
"E", # pycodestyle - default
"F", # pyflakes - default
"I" # isort
]
ignore = [
"E501" # Line too long
]

[isort]
[lint.isort]
known-local-folder = ["hdx"]
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ you make a git commit:
pre-commit install

The configuration file for this project is in a
non-start location. Thus, you will need to edit your
non-standard location. Thus, you will need to edit your
`.git/hooks/pre-commit` file to reflect this. Change
the line that begins with `ARGS` to:

ARGS=(hook-impl --config=.config/pre-commit-config.yaml --hook-type=pre-commit)

With pre-commit, all code is formatted according to
[black]("https://github.com/psf/black") and
[ruff]("https://github.com/charliermarsh/ruff") guidelines.

To check if your changes pass pre-commit without committing, run:
Expand All @@ -41,7 +40,7 @@ To run the tests and view coverage, execute:

pytest -c .config/pytest.ini --cov hdx --cov-config .config/coveragerc

Follow the example set out already in ``api.rst`` as you write the documentation.
Follow the example set out already in ``documentation/main.md`` as you write the documentation.

## Packages

Expand Down
16 changes: 4 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,14 @@ test = """
"""

[[tool.hatch.envs.test.matrix]]
python = ["3.11"]
python = ["3.12"]

[tool.hatch.envs.lint]
detached = true
dependencies = [
"black",
"ruff",
]
dependencies = ["ruff"]

[tool.hatch.envs.lint.scripts]
style = [
"ruff --config .config/ruff.toml {args:.}",
"black --config .config/black.toml --check --diff {args:.}",
]
# Not used for anything at the moment
fmt = [
"black --config .config/black.toml {args:.}",
"ruff --config .config/ruff.toml --fix {args:.}",
"ruff check --config .config/ruff.toml --diff {args:.}",
"ruff format --config .config/ruff.toml --diff {args:.}",
]
34 changes: 17 additions & 17 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,65 @@
#
# pip-compile --all-extras --output-file=requirements.txt --resolver=backtracking pyproject.toml
#
bcrypt==4.0.1
bcrypt==4.1.2
# via paramiko
cffi==1.16.0
# via
# cryptography
# pynacl
cfgv==3.4.0
# via pre-commit
coverage[toml]==7.3.2
coverage[toml]==7.4.1
# via pytest-cov
cryptography==41.0.4
cryptography==42.0.2
# via paramiko
distlib==0.3.7
distlib==0.3.8
# via virtualenv
filelock==3.12.4
filelock==3.13.1
# via virtualenv
greenlet==3.0.0
greenlet==3.0.3
# via sqlalchemy
identify==2.5.30
identify==2.5.34
# via pre-commit
iniconfig==2.0.0
# via pytest
nodeenv==1.8.0
# via pre-commit
packaging==23.2
# via pytest
paramiko==3.3.1
paramiko==3.4.0
# via sshtunnel
platformdirs==3.11.0
platformdirs==4.2.0
# via virtualenv
pluggy==1.3.0
pluggy==1.4.0
# via pytest
pre-commit==3.4.0
pre-commit==3.6.1
# via hdx-python-database (pyproject.toml)
psycopg[binary]==3.1.12
psycopg[binary]==3.1.18
# via hdx-python-database (pyproject.toml)
psycopg-binary==3.1.12
psycopg-binary==3.1.18
# via psycopg
pycparser==2.21
# via cffi
pynacl==1.5.0
# via paramiko
pytest==7.4.2
pytest==8.0.0
# via
# hdx-python-database (pyproject.toml)
# pytest-cov
pytest-cov==4.1.0
# via hdx-python-database (pyproject.toml)
pyyaml==6.0.1
# via pre-commit
sqlalchemy==2.0.21
sqlalchemy==2.0.27
# via hdx-python-database (pyproject.toml)
sshtunnel==0.4.0
# via hdx-python-database (pyproject.toml)
typing-extensions==4.8.0
typing-extensions==4.9.0
# via
# psycopg
# sqlalchemy
virtualenv==20.24.5
virtualenv==20.25.0
# via pre-commit

# The following packages are considered to be unsafe in a requirements file:
Expand Down
13 changes: 8 additions & 5 deletions src/hdx/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Database:
reflected classes. db_has_tz which defaults to False indicates whether
database datetime columns have timezones. If not, conversion occurs between
Python datetimes with timezones to timezoneless database columns (but not
when using reflection).
when using reflection). If table_base is supplied, db_has_tz is ignored.
Args:
database (Optional[str]): Database name
Expand All @@ -34,6 +34,7 @@ class Database:
dialect (str): Database dialect. Defaults to "postgresql".
driver (Optional[str]): Database driver. Defaults to None (psycopg if postgresql or None)
db_has_tz (bool): True if db datetime columns have timezone. Defaults to False.
table_base (Optional[Type[DeclarativeBase]]): Override table base. Defaults to None.
reflect (bool): Whether to reflect existing tables. Defaults to False.
**kwargs: See below
ssh_host (str): SSH host (the server to connect to)
Expand All @@ -55,6 +56,7 @@ def __init__(
dialect: str = "postgresql",
driver: Optional[str] = None,
db_has_tz: bool = False,
table_base: Optional[Type[DeclarativeBase]] = None,
reflect: bool = False,
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -98,10 +100,11 @@ def __init__(
)
if dialect == "postgresql":
wait_for_postgresql(db_uri)
if db_has_tz:
table_base = TZBase
else:
table_base = NoTZBase
if not table_base:
if db_has_tz:
table_base = TZBase
else:
table_base = NoTZBase
self.session = self.get_session(
db_uri, table_base=table_base, reflect=reflect
)
Expand Down
8 changes: 8 additions & 0 deletions tests/hdx/database/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,11 @@ def test_get_session_ssh(self, mock_psycopg, mock_SSHTunnelForwarder):
== "postgresql+psycopg://myuser@0.0.0.0:12345/mydatabase"
)
assert TestDatabase.table_base == TZBase
with Database(
ssh_host="mysshhost", ssh_port=25, table_base=TZBase, **params
) as dbsession:
assert (
str(dbsession.bind.engine.url)
== "postgresql+psycopg://myuser@0.0.0.0:12345/mydatabase"
)
assert TestDatabase.table_base == TZBase

0 comments on commit 42c8be1

Please sign in to comment.