Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion search_query/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
# For Python < 3.8
from importlib_metadata import version # type: ignore

__version__ = version("search_query")
try:
__version__ = version("search_query")
except Exception: # pragma: no cover - fallback when package metadata missing
__version__ = "0.0.0"
58 changes: 56 additions & 2 deletions search_query/wos/v_0_0_0/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from __future__ import annotations

import typing
import warnings

from search_query.constants import Fields
from search_query.query import Query
from search_query.wos.translator import WOSTranslator

if typing.TYPE_CHECKING: # pragma: no cover
Expand All @@ -15,11 +18,62 @@ class WOSTranslator_v0_0_0(WOSTranslator):

VERSION = "0.0.0"

# TODO : implement translator for deprecated fields (at least show a warning!)
# TODO : add corresponding tests for upgrades from 0.0.0 to 1.0.0
#: Mapping of deprecated field tags used by WOS ``0.0.0`` to
#: generic search fields. Only a subset of tags are supported; tags
#: without a mapping will trigger a warning and the corresponding
#: term will be dropped during translation.
DEPRECATED_FIELD_MAP = {
"AF=": Fields.AUTHOR,
"BA=": Fields.AUTHOR,
"BF=": Fields.GROUP_AUTHOR,
"BE=": Fields.EDITOR,
"C1=": Fields.ADDRESS,
"RP=": Fields.ADDRESS,
"DE=": Fields.AUTHOR_KEYWORDS,
"ID=": Fields.KEYWORDS_PLUS,
"DI=": Fields.DOI,
"RI=": Fields.AUTHOR_IDENTIFIERS,
"OI=": Fields.AUTHOR_IDENTIFIERS,
"SN=": Fields.ISSN_ISBN,
"EI=": Fields.ISSN_ISBN,
"BN=": Fields.ISSN_ISBN,
}

@classmethod
def translate_fields_to_generic(cls, query: Query) -> None:
"""Translate deprecated 0.0.0 field tags to generic field names."""

if query.field:
field_val = query.field.value

if field_val in cls.DEPRECATED_FIELD_MAP:
query.field.value = cls.DEPRECATED_FIELD_MAP[field_val]
else:
# Try default translation first; if it fails, emit a warning
# and drop the term as it cannot be represented in newer
# versions.
try:
super().translate_fields_to_generic(query)
except ValueError:
warnings.warn(
f"Field '{field_val}' is deprecated and cannot be "
"translated; the term will be ignored.",
UserWarning,
stacklevel=2,
)
parent = query.get_parent()
if parent:
parent.children.remove(query)
return
else:
return

for child in list(query.children):
cls.translate_fields_to_generic(child)


def register(registry: Registry, *, platform: str, version: str) -> None:
"""Register this translator with the ``registry``."""

registry.register_translator(platform, version, WOSTranslator_v0_0_0)

12 changes: 12 additions & 0 deletions test/wos/test_wos_upgrade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest

from search_query.constants import PLATFORM
from search_query.upgrade import upgrade_query


def test_upgrade_wos_0_to_1_deprecated_fields() -> None:
query = "DI=10.1000/xyz123 AND DE=robotics AND PU=Springer"
with pytest.warns(UserWarning) as record:
upgraded = upgrade_query(query, PLATFORM.WOS.value, "0.0.0", "1.0.0")
assert upgraded == "DO=10.1000/xyz123 AND AK=robotics"
assert any("PU=" in str(w.message) for w in record)
Loading