Skip to content

Commit 50d0d67

Browse files
committed
refactor: file repo
1 parent 2bce9d0 commit 50d0d67

File tree

11 files changed

+237
-209
lines changed

11 files changed

+237
-209
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ Transliterate using specified schema:
4242
Or pick schema by name
4343

4444
```python
45-
>>> schema = iuliia.Schemas.get("wikipedia")
46-
>>> schema.translate(source)
45+
>>> iuliia.schemas.has("wikipedia")
46+
True
47+
>>> schema = iuliia.schemas.get("wikipedia")
48+
>>> schema.translate("Юлия Щеглова")
4749
'Yuliya Shcheglova'
4850
```
4951

5052
List all supported schemas:
5153

5254
```python
53-
import iuliia
54-
for name, schema in iuliia.Schemas.items():
55+
for name, schema in iuliia.schemas.items():
5556
print("{0:<20}{1}".format(name, schema.description))
5657
```
5758

iuliia/__init__.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
"""
44

55
from .engine import translate
6+
from .repo import FileRepo
67
from .schema import Schema
7-
from .schemas import Schemas
88

99
__all__ = [
1010
"translate",
11+
"FileRepo",
1112
"Schema",
12-
"Schemas",
13+
"schemas",
1314
"ALA_LC",
1415
"ALA_LC_ALT",
1516
"BGN_PCGN",
@@ -40,31 +41,33 @@
4041
"YANDEX_MONEY",
4142
]
4243

43-
ALA_LC = Schemas.ala_lc.value
44-
ALA_LC_ALT = Schemas.ala_lc_alt.value
45-
BGN_PCGN = Schemas.bgn_pcgn.value
46-
BGN_PCGN_ALT = Schemas.bgn_pcgn_alt.value
47-
BS_2979 = Schemas.bs_2979.value
48-
BS_2979_ALT = Schemas.bs_2979_alt.value
49-
GOST_16876 = Schemas.gost_16876.value
50-
GOST_16876_ALT = Schemas.gost_16876_alt.value
51-
GOST_52290 = Schemas.gost_52290.value
52-
GOST_52535 = Schemas.gost_52535.value
53-
GOST_7034 = Schemas.gost_7034.value
54-
GOST_779 = Schemas.gost_779.value
55-
GOST_779_ALT = Schemas.gost_779_alt.value
56-
ICAO_DOC_9303 = Schemas.icao_doc_9303.value
57-
ISO_9_1954 = Schemas.iso_9_1954.value
58-
ISO_9_1968 = Schemas.iso_9_1968.value
59-
ISO_9_1968_ALT = Schemas.iso_9_1968_alt.value
60-
MOSMETRO = Schemas.mosmetro.value
61-
MVD_310 = Schemas.mvd_310.value
62-
MVD_310_FR = Schemas.mvd_310_fr.value
63-
MVD_782 = Schemas.mvd_782.value
64-
SCIENTIFIC = Schemas.scientific.value
65-
TELEGRAM = Schemas.telegram.value
66-
UNGEGN_1987 = Schemas.ungegn_1987.value
67-
UZ = Schemas.uz.value
68-
WIKIPEDIA = Schemas.wikipedia.value
69-
YANDEX_MAPS = Schemas.yandex_maps.value
70-
YANDEX_MONEY = Schemas.yandex_money.value
44+
schemas = FileRepo()
45+
46+
ALA_LC: Schema = schemas.get("ala_lc")
47+
ALA_LC_ALT: Schema = schemas.get("ala_lc_alt")
48+
BGN_PCGN: Schema = schemas.get("bgn_pcgn")
49+
BGN_PCGN_ALT: Schema = schemas.get("bgn_pcgn_alt")
50+
BS_2979: Schema = schemas.get("bs_2979")
51+
BS_2979_ALT: Schema = schemas.get("bs_2979_alt")
52+
GOST_16876: Schema = schemas.get("gost_16876")
53+
GOST_16876_ALT: Schema = schemas.get("gost_16876_alt")
54+
GOST_52290: Schema = schemas.get("gost_52290")
55+
GOST_52535: Schema = schemas.get("gost_52535")
56+
GOST_7034: Schema = schemas.get("gost_7034")
57+
GOST_779: Schema = schemas.get("gost_779")
58+
GOST_779_ALT: Schema = schemas.get("gost_779_alt")
59+
ICAO_DOC_9303: Schema = schemas.get("icao_doc_9303")
60+
ISO_9_1954: Schema = schemas.get("iso_9_1954")
61+
ISO_9_1968: Schema = schemas.get("iso_9_1968")
62+
ISO_9_1968_ALT: Schema = schemas.get("iso_9_1968_alt")
63+
MOSMETRO: Schema = schemas.get("mosmetro")
64+
MVD_310: Schema = schemas.get("mvd_310")
65+
MVD_310_FR: Schema = schemas.get("mvd_310_fr")
66+
MVD_782: Schema = schemas.get("mvd_782")
67+
SCIENTIFIC: Schema = schemas.get("scientific")
68+
TELEGRAM: Schema = schemas.get("telegram")
69+
UNGEGN_1987: Schema = schemas.get("ungegn_1987")
70+
UZ: Schema = schemas.get("uz")
71+
WIKIPEDIA: Schema = schemas.get("wikipedia")
72+
YANDEX_MAPS: Schema = schemas.get("yandex_maps")
73+
YANDEX_MONEY: Schema = schemas.get("yandex_money")

iuliia/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ def main():
1111
if len(sys.argv) < 3:
1212
print("usage: iuliia SCHEMA SOURCE")
1313
print("Supported schemas:")
14-
print("\n".join(iuliia.Schemas.names()))
14+
print("\n".join(iuliia.schemas.names()))
1515
sys.exit(1)
1616

1717
schema_name = sys.argv[1]
18-
schema = iuliia.Schemas.get(schema_name)
18+
schema = iuliia.schemas.get(schema_name)
1919
if schema is None:
2020
print(f"Schema '{schema_name}' does not exist. Supported schemas:")
21-
print("\n".join(iuliia.Schemas.names()))
21+
print("\n".join(iuliia.schemas.names()))
2222
sys.exit(1)
2323

2424
source = sys.argv[2]

iuliia/lazy.py

Lines changed: 0 additions & 78 deletions
This file was deleted.

iuliia/repo.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
File-based schema repository.
3+
"""
4+
5+
import json
6+
from pathlib import Path
7+
from .schema import Schema, TranslitSchema
8+
from .validator import Validator
9+
10+
SCHEMA_DIR = Path(__file__).parent / "schemas"
11+
12+
13+
class FileSchema:
14+
"""
15+
File-based transliteration schema.
16+
Translates Cyrillic text into Latin using a given set of rules (mappings).
17+
Lazy loads schema from JSON file as needed.
18+
"""
19+
20+
def __init__(self, name: str, path: str | Path):
21+
self.name = name
22+
self.path = Path(path)
23+
self._schema: TranslitSchema | None = None
24+
25+
@property
26+
def description(self) -> str | None:
27+
"""Schema description."""
28+
if self._schema is None:
29+
self._schema = self._load()
30+
return self._schema.description
31+
32+
@property
33+
def samples(self) -> list[list[str]]:
34+
"""Schema samples."""
35+
if self._schema is None:
36+
self._schema = self._load()
37+
return self._schema.samples
38+
39+
def translate(self, source: str) -> str:
40+
"""
41+
Translate source Cyrillic string into Latin.
42+
Translates the source string word by word.
43+
"""
44+
if self._schema is None:
45+
self._schema = self._load()
46+
return self._schema.translate(source)
47+
48+
def _load(self) -> TranslitSchema:
49+
"""Load schema by its name."""
50+
if not self.path.exists():
51+
raise ValueError(f"Schema path does not exist: {self.path}")
52+
53+
# Load and validate schema definition.
54+
defn = {}
55+
with open(self.path, encoding="utf-8") as file:
56+
defn = json.load(file)
57+
Validator(defn).run()
58+
59+
return TranslitSchema(
60+
name=defn.get("name", ""),
61+
description=defn.get("description"),
62+
mapping=defn.get("mapping", {}),
63+
prev_mapping=defn.get("prev_mapping"),
64+
next_mapping=defn.get("next_mapping"),
65+
ending_mapping=defn.get("ending_mapping"),
66+
samples=defn.get("samples"),
67+
)
68+
69+
def __str__(self):
70+
return self.name
71+
72+
def __repr__(self):
73+
return f"{self.__class__.__name__}('{self.name}')"
74+
75+
76+
class FileRepo:
77+
"""
78+
File-based transliteration schema repository.
79+
"""
80+
81+
def __init__(self, base_dir: str | Path = SCHEMA_DIR):
82+
self.base_dir = base_dir
83+
self.schemas = {}
84+
for path in Path(base_dir).iterdir():
85+
if not path.suffix == ".json":
86+
continue
87+
name = path.stem
88+
self.schemas[name] = FileSchema(name, path)
89+
90+
def has(self, name: str) -> bool:
91+
"""Check if schema exists."""
92+
return name in self.schemas
93+
94+
def get(self, name: str) -> Schema:
95+
"""Get schema by name."""
96+
if name not in self.schemas:
97+
raise ValueError(f"Schema not found: {name}")
98+
return self.schemas[name]
99+
100+
def names(self) -> list[str]:
101+
"""Return schema names sorted by name."""
102+
return sorted(self.schemas.keys())
103+
104+
def items(self) -> list[tuple[str, Schema]]:
105+
"""Return (name, schema) tuples sorted by name."""
106+
return sorted(self.schemas.items(), key=lambda item: item[0])

iuliia/schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ class Schema(Protocol):
1212
using a given set of rules (mappings).
1313
"""
1414

15+
@property
16+
def name(self) -> str | None:
17+
"""Schema name."""
18+
19+
@property
20+
def description(self) -> str | None:
21+
"""Schema description."""
22+
23+
@property
24+
def samples(self) -> list[list[str]]:
25+
"""Schema samples."""
26+
1527
def translate(self, source: str) -> str:
1628
"""
1729
Translate source Cyrillic string into Latin.

iuliia/schemas.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)