File size: 2,059 Bytes
f8768df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4aee46
f8768df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Public taxonomy composition and deterministic ranking contracts."""

from __future__ import annotations

from importlib.resources import files as resource_files
from pathlib import Path

from ._taxonomy_impl import (
    DefaultRanker,
    DefaultTaxonomy,
    Ranker,
    Taxonomy,
    TaxonomyBuilder,
)

# Keep repr(), documentation, and pickling pointed at the public module after
# the pre-release namespace migration.
for _public_type in (
    DefaultRanker,
    DefaultTaxonomy,
    Ranker,
    Taxonomy,
    TaxonomyBuilder,
):
    _public_type.__module__ = __name__


class GeneralEconomyTaxonomy(DefaultTaxonomy):
    """The bundled, conservative taxonomy for economy and broad-market news."""


def copy_default_taxonomy(

    destination: str | Path,

    *,

    overwrite: bool = False,

) -> Path:
    """Copy the bundled general-economy taxonomy to an editable directory."""

    root = Path(destination).expanduser().resolve()
    package = resource_files("range_curate._core")
    copies = (
        (package.joinpath("configs", "default.yml"), root / "taxonomy.yml"),
        (
            package.joinpath("catalogs", "entities.yml"),
            root / "catalogs" / "entities.yml",
        ),
        (
            package.joinpath("catalogs", "lexical.yml"),
            root / "catalogs" / "lexical.yml",
        ),
    )
    existing = [target for _source, target in copies if target.exists()]
    if existing and not overwrite:
        names = ", ".join(str(path) for path in existing)
        raise FileExistsError(
            f"taxonomy files already exist: {names}; pass overwrite=True to replace them"
        )

    for source, target in copies:
        target.parent.mkdir(parents=True, exist_ok=True)
        target.write_bytes(source.read_bytes())
    return root / "taxonomy.yml"


__all__ = [
    "DefaultRanker",
    "DefaultTaxonomy",
    "GeneralEconomyTaxonomy",
    "Ranker",
    "Taxonomy",
    "TaxonomyBuilder",
    "copy_default_taxonomy",
]