diff --git a/.config/black.toml b/.config/black.toml deleted file mode 100644 index a8f43fe..0000000 --- a/.config/black.toml +++ /dev/null @@ -1,2 +0,0 @@ -[tool.black] -line-length = 79 diff --git a/.config/pre-commit-config.yaml b/.config/pre-commit-config.yaml index 9b45c2e..8aaefe0 100644 --- a/.config/pre-commit-config.yaml +++ b/.config/pre-commit-config.yaml @@ -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 diff --git a/.config/ruff.toml b/.config/ruff.toml index ac322d2..4625a34 100644 --- a/.config/ruff.toml +++ b/.config/ruff.toml @@ -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"] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b5bb4eb..acfb9e9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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: @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 121ebc1..2ced9c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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:.}", ] diff --git a/requirements.txt b/requirements.txt index 0af52b5..e8178a0 100755 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ # # 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 @@ -12,17 +12,17 @@ cffi==1.16.0 # 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 @@ -30,23 +30,23 @@ 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 @@ -54,15 +54,15 @@ 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: diff --git a/src/hdx/database/__init__.py b/src/hdx/database/__init__.py index aff7313..8c70b6d 100644 --- a/src/hdx/database/__init__.py +++ b/src/hdx/database/__init__.py @@ -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 @@ -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) @@ -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: @@ -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 ) diff --git a/tests/hdx/database/test_database.py b/tests/hdx/database/test_database.py index 9b87b5c..1d64c77 100755 --- a/tests/hdx/database/test_database.py +++ b/tests/hdx/database/test_database.py @@ -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