Skip to content

Commit

Permalink
Merge pull request #85 from goodmami/v0.4.1
Browse files Browse the repository at this point in the history
v0.4.1
  • Loading branch information
goodmami authored Jan 19, 2021
2 parents 8118f3f + b095514 commit 180a84d
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 284 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
## [Unreleased]


## [v0.4.1]

**Release date: 2021-01-19**

### Removed

* `wn.config.database_filename` (only `wn.config.data_directory` is
configurable now)

### Changed

* Schema validation is now done when creating a new connection,
instead of on import of `wn`
* One connection is shared per database path, rather than storing
connections on the modeling classes ([#81])

### Fixed

* More robustly check for LMF validity ([#83])


## [v0.4.0]

**Release date: 2020-12-29**
Expand Down Expand Up @@ -113,6 +134,7 @@ the https://github.com/nltk/wordnet/ code which had been effectively
abandoned, but this is an entirely new codebase.


[v0.4.1]: ../../releases/tag/v0.4.1
[v0.4.0]: ../../releases/tag/v0.4.0
[v0.3.0]: ../../releases/tag/v0.3.0
[v0.2.0]: ../../releases/tag/v0.2.0
Expand All @@ -139,3 +161,4 @@ abandoned, but this is an entirely new codebase.
[#78]: https://github.com/goodmami/wn/issues/78
[#79]: https://github.com/goodmami/wn/issues/79
[#81]: https://github.com/goodmami/wn/issues/81
[#83]: https://github.com/goodmami/wn/issues/83
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def mini_db_dir(mini_lmf_1_0):
wn.add(mini_lmf_1_0)
wn.config.data_directory = old_data_dir
yield Path(dir)
# close any open DB connections before teardown
for conn in wn._db.pool.values():
conn.close()


@pytest.fixture
Expand Down
11 changes: 9 additions & 2 deletions tests/db_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

from wn import _db
import sqlite3

import pytest

import wn


@pytest.mark.usefixtures('mini_db')
def test_schema_compatibility():
assert _db.is_schema_compatible(create=True)
conn = sqlite3.connect(str(wn.config.database_path))
schema_hash = wn._db.schema_hash(conn)
assert schema_hash in wn._db.COMPATIBLE_SCHEMA_HASHES
10 changes: 0 additions & 10 deletions wn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from wn._meta import __version__
from wn._exceptions import Error
from wn._config import config # noqa: F401
from wn._db import is_schema_compatible
from wn._add import add, remove
from wn._export import export
from wn._download import download
Expand All @@ -41,12 +40,3 @@
synset, synsets, Synset,
Wordnet
)

if not is_schema_compatible():
installed = '\n '.join(f'{lex.id}:{lex.version}' for lex in lexicons())
raise Error(
"Wn's schema has changed and is no longer compatible with the "
f"database. Please move or delete {config.database_path} and "
"rebuild it. Note that the following lexicons are currently "
f"installed:\n {installed}"
)
16 changes: 3 additions & 13 deletions wn/_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

import sys
import logging
import sqlite3

import wn
from wn._types import AnyPath
from wn._db import connects
from wn._db import connect
from wn._queries import find_lexicons
from wn._util import get_progress_handler
from wn.project import iterpackages
Expand Down Expand Up @@ -76,16 +75,13 @@ def progress_handler(n: int, **kwargs) -> str:
_add_lmf(package.resource_file(), progress_handler)


@connects
def _add_lmf(
source,
progress_handler,
conn: sqlite3.Connection = None
) -> None:
assert conn is not None # provided by decorator
callback = get_progress_handler(progress_handler, 'Database', '\b', '')

with conn:
with connect() as conn:
cur = conn.cursor()
# these two settings increase the risk of database corruption
# if the system crashes during a write, but they should also
Expand Down Expand Up @@ -370,13 +366,7 @@ def remove(lexicon: str) -> None:
>>> wn.remove('ewn:2019')
"""
_remove(lexicon)


@connects
def _remove(lexicon: str, conn: sqlite3.Connection = None) -> None:
assert conn is not None # provided by decorator
with conn:
with connect() as conn:
for rowid, id, _, _, _, _, version, *_ in find_lexicons(lexicon=lexicon):
conn.execute('DELETE FROM entries WHERE lexicon_rowid = ?', (rowid,))
conn.execute('DELETE FROM synsets WHERE lexicon_rowid = ?', (rowid,))
Expand Down
7 changes: 4 additions & 3 deletions wn/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

# The directory where downloaded and added data will be stored.
DEFAULT_DATA_DIRECTORY = Path.home() / '.wn_data'
DEFAULT_DATABASE_FILENAME = 'wn.db'
DATABASE_FILENAME = 'wn.db'


class WNConfig:

def __init__(self):
self._data_directory = DEFAULT_DATA_DIRECTORY
self._projects = {}
self.database_filename = DEFAULT_DATABASE_FILENAME
self._dbpath = self._data_directory / DATABASE_FILENAME

@property
def data_directory(self) -> Path:
Expand All @@ -37,11 +37,12 @@ def data_directory(self, path):
if dir.exists() and not dir.is_dir():
raise Error(f'path exists and is not a directory: {dir}')
self._data_directory = dir
self._dbpath = dir / DATABASE_FILENAME

@property
def database_path(self):
"""The path to the database file."""
return self.data_directory / self.database_filename
return self._dbpath

@property
def downloads_directory(self):
Expand Down
Loading

0 comments on commit 180a84d

Please sign in to comment.