Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add support for WN-LMF 1.4#261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
goodmami wants to merge5 commits intomain
base:main
Choose a base branch
Loading
fromgh-260-wn-lmf-1.4
Draft
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Don't delete decompressed .xml files too early
  • Loading branch information
@goodmami
goodmami committedMay 27, 2025
commit3453761685e2a08eb45716f8a5c10df436c3b441
35 changes: 34 additions & 1 deletiontests/conftest.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@

from pathlibimportPath
importlzma
import tempfile
from pathlib import Path

import pytest

Expand DownExpand Up@@ -41,6 +42,19 @@ def mini_db_dir(datadir):
yield Path(dir)


@pytest.fixture
def mini_lmf_compressed(datadir):
data = (datadir / 'mini-lmf-1.0.xml').read_bytes()
with tempfile.NamedTemporaryFile(suffix='.xml.xz', delete=False) as file:
path = Path(file.name)
with lzma.open(path, "w") as f:
f.write(data)
try:
yield Path(file.name)
finally:
Path(file.name).unlink()


@pytest.fixture(scope='session')
def mini_db_1_1_dir(datadir):
with tempfile.TemporaryDirectory('wn_data_mini_1_1') as dir:
Expand All@@ -53,6 +67,17 @@ def mini_db_1_1_dir(datadir):
yield Path(dir)


@pytest.fixture(scope='session')
def mini_db_1_4_dir(datadir):
with tempfile.TemporaryDirectory('wn_data_mini_1_4') as dir:
with pytest.MonkeyPatch.context() as m:
m.setattr(wn.config, 'data_directory', dir)
wn.add(datadir / 'mini-lmf-1.4.xml')
wn._db.clear_connections()

yield Path(dir)


@pytest.fixture
def mini_db(monkeypatch, mini_db_dir):
with monkeypatch.context() as m:
Expand All@@ -67,3 +92,11 @@ def mini_db_1_1(monkeypatch, mini_db_1_1_dir):
m.setattr(wn.config, 'data_directory', mini_db_1_1_dir)
yield
wn._db.clear_connections()


@pytest.fixture
def mini_db_1_4(monkeypatch, mini_db_1_4_dir):
with monkeypatch.context() as m:
m.setattr(wn.config, 'data_directory', mini_db_1_4_dir)
yield
wn._db.clear_connections()
12 changes: 12 additions & 0 deletionstests/project_test.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,12 +13,14 @@ def test_is_collection_directory(datadir):

def test_get_project(datadir):
proj = project.get_project(path=datadir / "test-package")
assert proj.type == "wordnet"
assert proj.resource_file() == datadir / "test-package" / "test-wn.xml"
assert proj.readme() == datadir / "test-package" / "README.md"
assert proj.license() == datadir / "test-package" / "LICENSE"
assert proj.citation() == datadir / "test-package" / "citation.bib"

proj = project.get_project(path=datadir / "mini-lmf-1.0.xml")
assert proj.type == "wordnet"
assert proj.resource_file() == datadir / "mini-lmf-1.0.xml"
assert proj.readme() is None
assert proj.license() is None
Expand All@@ -41,3 +43,13 @@ def test_iterpackages(datadir):
}
assert "mini-lmf-1.0.xml" in pkg_names
assert "test-wn.xml" not in pkg_names


def test_compressed_iterpackages(mini_lmf_compressed):
for pkg in project.iterpackages(mini_lmf_compressed):
assert pkg.type == "wordnet"
assert pkg.resource_file().exists()
# ensure cleanup of temporary data
assert not pkg.resource_file().exists()
# ensure original file not deleted
assert mini_lmf_compressed.exists()
58 changes: 30 additions & 28 deletionswn/project.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -199,43 +199,42 @@ def get_project(


def _get_project_from_path(
path: AnyPath,tmpdir: Optional[str] = None,
) -> tuple[Project, Optional[str]]:
path: AnyPath,tmp_path: Optional[Path] = None,
) -> tuple[Project, Optional[Path]]:
path = Path(path).expanduser()

if path.is_dir():
if is_package_directory(path):
return Package(path),tmpdir
return Package(path),tmp_path

elif is_collection_directory(path):
return Collection(path),tmpdir
return Collection(path),tmp_path

else:
raise wn.Error(
f'does not appear to be a valid package or collection: {path!s}'
)

elif tarfile.is_tarfile(path):
tmpdir_ = tempfile.mkdtemp()
tmpdir_ =Path(tempfile.mkdtemp())
with tarfile.open(path) as tar:
_check_tar(tar)
tar.extractall(path=tmpdir_)
contents = list(Path(tmpdir_).iterdir())
contents = list(tmpdir_.iterdir())
if len(contents) != 1:
raise wn.Error(
'archive may only have one resource, package, or collection'
)
return _get_project_from_path(contents[0],tmpdir=tmpdir_)
return _get_project_from_path(contents[0],tmp_path=tmpdir_)

else:
decompressed: Path
with _get_decompressed(path) as decompressed:
if lmf.is_lmf(decompressed) or _ili.is_ili(decompressed):
return ResourceOnlyPackage(decompressed), tmpdir
else:
raise wn.Error(
f'not a valid lexical resource: {path!s}'
)
decompressed, tmp_path = _get_decompressed(path, tmp_path)
if lmf.is_lmf(decompressed) or _ili.is_ili(decompressed):
return ResourceOnlyPackage(decompressed), tmp_path
else:
raise wn.Error(
f'not a valid lexical resource: {path!s}'
)


def iterpackages(path: AnyPath, delete: bool = True) -> Iterator[Package]:
Expand DownExpand Up@@ -263,7 +262,7 @@ def iterpackages(path: AnyPath, delete: bool = True) -> Iterator[Package]:
temporary directory.

"""
project,tmpdir = _get_project_from_path(path)
project,tmp_path = _get_project_from_path(path)

try:
if isinstance(project, Package):
Expand All@@ -273,37 +272,40 @@ def iterpackages(path: AnyPath, delete: bool = True) -> Iterator[Package]:
else:
raise wn.Error(f'unexpected project type: {project.__class__.__name__}')
finally:
if tmpdir and delete:
shutil.rmtree(tmpdir)
if tmp_path and delete:
if tmp_path.is_dir():
shutil.rmtree(tmp_path)
elif tmp_path.is_file():
tmp_path.unlink()
else:
raise wn.Error(f'could not remove temporary path: {tmp_path}')


@contextmanager
def _get_decompressed(source: Path) -> Iterator[Path]:
def _get_decompressed(source: Path, tmp_path: Optional[Path]) -> Path:
gzipped = is_gzip(source)
xzipped = is_lzma(source)
if not (gzipped or xzipped):
yield source
return source, tmp_path
else:
tmp = tempfile.NamedTemporaryFile(suffix='.xml', delete=False)
path = Path(tmp.name)
try:
# for typing issues, see https://github.com/python/mypy/issues/15031
if gzipped:
with gzip.open(source, 'rb') as gzip_src:
shutil.copyfileobj(gzip_src, tmp) # type: ignore
shutil.copyfileobj(gzip_src, tmp)
else: # xzipped
with lzma.open(source, 'rb') as lzma_src:
shutil.copyfileobj(lzma_src, tmp) # type: ignore
shutil.copyfileobj(lzma_src, tmp)

tmp.close() # Windows cannot reliably reopen until it's closed

yield path

except (OSError, EOFError, lzma.LZMAError) as exc:
raise wn.Error(f'could not decompress file: {source}') from exc

finally:
path.unlink()
# if tmp_path is not None, the compressed file was in a
# temporary directory, so return that. Otherwise the new path
# becomes the tmp_path
return path, tmp_path or path


def _check_tar(tar: tarfile.TarFile) -> None:
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp