verso-core 0.1.39__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. verso/core/__init__.py +1 -0
  2. verso/core/cli.py +230 -0
  3. verso/core/corpus_data.py +149 -0
  4. verso/core/i18n.py +477 -0
  5. verso/core/locales/en.toml +231 -0
  6. verso/core/locales/ru.toml +232 -0
  7. verso/core/messages.py +15 -0
  8. verso/core/pipeline/__init__.py +15 -0
  9. verso/core/pipeline/config.py +83 -0
  10. verso/core/pipeline/load.py +296 -0
  11. verso/core/pipeline/records.py +340 -0
  12. verso/core/places.py +352 -0
  13. verso/core/plugins.py +419 -0
  14. verso/core/py.typed +0 -0
  15. verso/core/ratchet.py +491 -0
  16. verso/core/records/__init__.py +14 -0
  17. verso/core/records/decode.py +224 -0
  18. verso/core/records/encode.py +337 -0
  19. verso/core/records/fields.py +521 -0
  20. verso/core/records/graph.py +75 -0
  21. verso/core/records/ids.py +232 -0
  22. verso/core/records/notes.py +174 -0
  23. verso/core/records/replay.py +362 -0
  24. verso/core/records/schema.py +307 -0
  25. verso/core/records/sets.py +22 -0
  26. verso/core/records/ui.py +521 -0
  27. verso/core/targets/__init__.py +1 -0
  28. verso/core/targets/geni/__init__.py +15 -0
  29. verso/core/targets/geni/about.py +29 -0
  30. verso/core/targets/geni/consolidation_conflicts.py +300 -0
  31. verso/core/targets/geni/date.py +125 -0
  32. verso/core/targets/geni/event.py +41 -0
  33. verso/core/targets/geni/held.py +20 -0
  34. verso/core/targets/geni/profile.py +189 -0
  35. verso/core/targets/geni/redundant_additional_info.py +106 -0
  36. verso/core/targets/geni/redundant_ignores.py +101 -0
  37. verso/core/targets/geni/redundant_overrides.py +122 -0
  38. verso/core/targets/geni/redundant_preferences.py +179 -0
  39. verso/core/targets/geni/union.py +99 -0
  40. verso/core/tools.py +57 -0
  41. verso/core/workspace.py +392 -0
  42. verso/core/written.py +86 -0
  43. verso_core-0.1.39.dist-info/METADATA +31 -0
  44. verso_core-0.1.39.dist-info/RECORD +49 -0
  45. verso_core-0.1.39.dist-info/WHEEL +5 -0
  46. verso_core-0.1.39.dist-info/entry_points.txt +2 -0
  47. verso_core-0.1.39.dist-info/licenses/LICENSE +201 -0
  48. verso_core-0.1.39.dist-info/licenses/NOTICE +13 -0
  49. verso_core-0.1.39.dist-info/top_level.txt +1 -0
verso/core/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """The library's core: what any corpus needs — the workspace, the pipeline, the records JSON, the ratchet, the Geni models."""
verso/core/cli.py ADDED
@@ -0,0 +1,230 @@
1
+ """`verso <команда>`: one command for each module of the library that runs as a program.
2
+
3
+ A command is its module's name with hyphens — `verso generate-tf` is
4
+ `python -m verso.ru.cli.generate_tf` —, but for five names too general to stand alone
5
+ (`records-schema`, `ui-schema`, `verify-batch`, `validate-import`, `import-sheets`). The module runs as
6
+ `__main__` through runpy, so it reads its arguments, prints and exits as under `python -m`,
7
+ but that its usage names the program `verso <команда>`.
8
+ `--lang LANG` speaks LANG: it is `VERSO_LANG=LANG` for this command (`verso.core.i18n`).
9
+ `--workspace DIR` runs it in that workspace: it is `VERSO_WORKSPACE=DIR` for this command
10
+ — a `workspace.use()` here would not reach `verso workspace`, which runpy runs as a second
11
+ copy of the module, and this module imports nothing of the library for that reason.
12
+
13
+ The core's four commands are here; each installed plugin brings its own (`Plugin.commands`),
14
+ found by the registry without a workspace: `verso --help` lists them all, the core's
15
+ first, then each plugin's by its name. The tables are strings: the core imports none of the
16
+ commands' modules.
17
+ """
18
+ from __future__ import annotations
19
+
20
+ import argparse
21
+ import os
22
+ import re
23
+ import runpy
24
+ import sys
25
+ from collections.abc import Generator, Mapping
26
+ from contextlib import contextmanager
27
+ from pathlib import Path
28
+ from typing import Any
29
+
30
+ from verso.core.i18n import Message
31
+ from verso.core.messages import t
32
+
33
+ # verso.core.workspace's ENV and CONFIG; importing the module would make `verso workspace` a
34
+ # second copy of it (runpy warns, and the copy's own use() stack would be another one).
35
+ _ENV = "VERSO_WORKSPACE"
36
+ _CONFIG = "verso.toml"
37
+
38
+ # The core's own commands; each installed plugin brings its own (`Plugin.commands`).
39
+ COMMANDS: dict[str, str] = {
40
+ "records-schema": "verso.core.records.schema",
41
+ "tools": "verso.core.tools",
42
+ "ui-schema": "verso.core.records.ui",
43
+ "workspace": "verso.core.workspace",
44
+ }
45
+
46
+
47
+ def _say(message: object) -> str:
48
+ """A message's text in the workspace's language: the workspace is imported here, to say
49
+ it, and never before a command runs — `verso workspace` runs that module as `__main__`."""
50
+ import verso.core.workspace # noqa: F401 — it tells verso.core.i18n where the language comes from
51
+ return str(message)
52
+
53
+
54
+ def _plugins() -> list[tuple[str, Mapping[str, str]]]:
55
+ """Each installed plugin's name and commands, by name. The registry reads no workspace:
56
+ a command is found wherever verso is run, `verso --help` with no workspace at all."""
57
+ from verso.core import plugins
58
+ return [(name, plugin.commands) for name, plugin in plugins.installed().items()]
59
+
60
+
61
+ def commands() -> dict[str, str]:
62
+ """Every command: the core's, then each installed plugin's — refused when two name one."""
63
+ from verso.core.plugins import PluginError
64
+ out, owner = dict(COMMANDS), dict.fromkeys(COMMANDS, "")
65
+ for name, given in _plugins():
66
+ for command, module in given.items():
67
+ if command in owner:
68
+ where = t("cli.where_plugins", first=owner[command], second=name) if owner[command] \
69
+ else t("cli.where_core", plugin=name)
70
+ raise PluginError(t("cli.command_twice", command=command, where=where))
71
+ out[command], owner[command] = module, name
72
+ return out
73
+
74
+
75
+ class _Parser(argparse.ArgumentParser):
76
+ """verso's own parser: its texts are the catalog's, put together only when the help or the
77
+ usage is printed — a command that prints neither never asks for the language, nor so reads the
78
+ workspace (`verso workspace` runs that module as `__main__`)."""
79
+
80
+ def __init__(self, groups: list[tuple[Message, dict[str, str]]], **kwargs: Any) -> None:
81
+ super().__init__(**kwargs)
82
+ self.groups = groups
83
+ self.lazy: list[tuple[object, str, Message]] = [] # (what, its attribute, the text)
84
+
85
+ def text(self, what: object, attribute: str, message: Message) -> None:
86
+ self.lazy.append((what, attribute, message))
87
+
88
+ @contextmanager
89
+ def _rendered(self) -> Generator[None]:
90
+ width = max(len(name) for _, given in self.groups for name in given)
91
+ listing = "\n\n".join(f"{_say(title)}:\n" + "\n".join(f" {name:<{width}} {module}" for name, module in given.items())
92
+ for title, given in self.groups if given)
93
+ if len(self.groups) == 1:
94
+ listing += f"\n\n{_say(t('cli.no_plugins'))}"
95
+ texts = [*self.lazy, (self, "epilog", t("cli.epilog", listing=listing))]
96
+ before = [(what, attribute, getattr(what, attribute)) for what, attribute, _ in texts]
97
+ for what, attribute, message in texts:
98
+ setattr(what, attribute, _say(message))
99
+ try:
100
+ yield
101
+ finally:
102
+ for what, attribute, value in before:
103
+ setattr(what, attribute, value)
104
+
105
+ def format_help(self) -> str:
106
+ with self._rendered():
107
+ return super().format_help()
108
+
109
+ def format_usage(self) -> str:
110
+ with self._rendered():
111
+ return super().format_usage()
112
+
113
+
114
+ def build_parser() -> argparse.ArgumentParser:
115
+ groups = [(t("cli.core_group"), dict(COMMANDS)),
116
+ *((t("cli.plugin_group", name=name), dict(given)) for name, given in _plugins())]
117
+ parser = _Parser(groups, prog="verso", formatter_class=argparse.RawDescriptionHelpFormatter)
118
+ parser.text(parser, "description", t("cli.description"))
119
+ parser.text(parser.add_argument("--workspace", type=Path, metavar="DIR"), "help", t("cli.workspace_help"))
120
+ parser.text(parser.add_argument("--lang", metavar="LANG"), "help", t("cli.lang_help"))
121
+ parser.text(parser.add_argument("command", nargs="?"), "metavar", t("cli.command_metavar"))
122
+ parser.text(parser.add_argument("args", nargs=argparse.REMAINDER), "metavar", t("cli.args_metavar"))
123
+ return parser
124
+
125
+
126
+ # How a parser names a module run as a program: Python 3.14's default under `-m`
127
+ # («python3 -m verso.ru.cli.svyatcy»), or a module's own prog written so («python -m <module>»).
128
+ _AS_A_MODULE = re.compile(r"^python[\d.]* -m \S+")
129
+
130
+
131
+ @contextmanager
132
+ def _named(program: str) -> Generator[None]:
133
+ """While a command runs, a parser that would name the program as a module names it
134
+ `program`; a subcommand's parser follows, as argparse builds its name from its parent's."""
135
+ init = argparse.ArgumentParser.__init__
136
+
137
+ def named(self: argparse.ArgumentParser, *args: Any, **kwargs: Any) -> None:
138
+ init(self, *args, **kwargs)
139
+ self.prog = _AS_A_MODULE.sub(lambda _: program, self.prog, count=1)
140
+
141
+ setattr(argparse.ArgumentParser, "__init__", named) # noqa: B010 — a method assigned, which pyright refuses
142
+ try:
143
+ yield
144
+ finally:
145
+ setattr(argparse.ArgumentParser, "__init__", init) # noqa: B010
146
+
147
+
148
+ def _exit_code(code: object) -> int:
149
+ """What the process would exit with, raising SystemExit(code)."""
150
+ if code is None:
151
+ return 0
152
+ if isinstance(code, int):
153
+ return code
154
+ print(code, file=sys.stderr)
155
+ return 1
156
+
157
+
158
+ def _missing_plugin() -> str | None:
159
+ """Why a command may be missing: the workspace enables a plugin nobody installed (`make
160
+ bootstrap` after a pull); None when it does not, or there is no workspace here."""
161
+ from verso.core import plugins, workspace
162
+ try:
163
+ plugins.enabled()
164
+ except plugins.PluginError as error:
165
+ return str(error)
166
+ except workspace.WorkspaceError:
167
+ return None
168
+ return None
169
+
170
+
171
+ def _early(argv: list[str]) -> dict[str, str]:
172
+ """verso's own `--lang` and `--workspace`, read before the parser prints anything (its help,
173
+ its usage, an error): the options before the command, as the parser takes them."""
174
+ out: dict[str, str] = {}
175
+ words = iter(argv)
176
+ for word in words:
177
+ if not word.startswith("-"):
178
+ break # the command: what follows is its own
179
+ name, eq, value = word.partition("=")
180
+ if name in ("--lang", "--workspace"):
181
+ value = value if eq else next(words, "")
182
+ if value:
183
+ out[name[2:]] = value
184
+ return out
185
+
186
+
187
+ def main(argv: list[str] | None = None) -> int:
188
+ from verso.core import i18n
189
+ from verso.core.plugins import PluginError
190
+ early = _early(sys.argv[1:] if argv is None else argv)
191
+ if "lang" in early:
192
+ os.environ[i18n.ENV] = early["lang"]
193
+ if "workspace" in early and (Path(early["workspace"]) / _CONFIG).is_file():
194
+ os.environ[_ENV] = str(Path(early["workspace"]).resolve())
195
+ try: # a language nobody speaks: `--lang`, `VERSO_LANG`
196
+ i18n.check()
197
+ except i18n.CatalogError as error:
198
+ print(f"verso: {error}", file=sys.stderr)
199
+ return 2
200
+ try: # a plugin's entry point that does not load, two plugins' command
201
+ parser = build_parser()
202
+ args = parser.parse_args(argv)
203
+ if args.command is None:
204
+ parser.print_help()
205
+ return 2
206
+ module = commands().get(args.command)
207
+ except PluginError as error:
208
+ print(f"verso: {_say(error)}", file=sys.stderr)
209
+ return 2
210
+ if module is None:
211
+ print(_say(t("cli.no_command", command=args.command)), file=sys.stderr)
212
+ if (why := _missing_plugin()) is not None:
213
+ print(f"verso: {why}", file=sys.stderr)
214
+ return 2
215
+ if args.workspace is not None:
216
+ if not (args.workspace / _CONFIG).is_file():
217
+ print(_say(t("cli.workspace_no_config", workspace=args.workspace, config=_CONFIG)), file=sys.stderr)
218
+ return 2
219
+ os.environ[_ENV] = str(args.workspace.resolve())
220
+ sys.argv = [f"verso {args.command}", *args.args]
221
+ with _named(sys.argv[0]):
222
+ try:
223
+ runpy.run_module(module, run_name="__main__", alter_sys=True)
224
+ except SystemExit as done:
225
+ return _exit_code(done.code)
226
+ return 0
227
+
228
+
229
+ if __name__ == "__main__":
230
+ raise SystemExit(main())
@@ -0,0 +1,149 @@
1
+ """What the core reads of a village's own data file, and the helpers a plugin reads its
2
+ corpus data with (the rest of it is the plugins').
3
+
4
+ `<village>/village.toml` holds one village's settings (`village()`), read from the village's
5
+ directory itself: the translator is handed a path, and a test's village in a temporary
6
+ directory is no village of verso.toml. The core reads `schema` and `[geni]`, the Geni
7
+ projects its models take; every other table is an enabled plugin's, parsed by the plugin's
8
+ `village_tables` hook (ru: `[census]`, `[timeline]`, `[sheets]`, `[csv]`), and a table no
9
+ enabled plugin reads is an unknown key. The corpus's own files (ru: `estates.toml`,
10
+ `timeline.toml`) are the plugins' to read, through `corpus_file()`.
11
+
12
+ A file that is not there is no settings (a new user's library, a test's village);
13
+ a file that is there and says something this code does not know is an error naming
14
+ the file and the key — a default quietly taken instead would be worse than a stop.
15
+ """
16
+ from __future__ import annotations
17
+
18
+ import tomllib
19
+ from collections.abc import Callable, Mapping
20
+ from dataclasses import dataclass, field
21
+ from pathlib import Path
22
+ from types import MappingProxyType
23
+ from typing import Any
24
+
25
+ from verso.core import plugins, workspace
26
+ from verso.core.workspace import WorkspaceError
27
+ from verso.core.messages import t
28
+
29
+ VILLAGE_FILE = "village.toml"
30
+ VILLAGE_SCHEMA = 1
31
+
32
+ _EMPTY: Mapping[str, str] = MappingProxyType({})
33
+
34
+
35
+ class CorpusDataError(WorkspaceError):
36
+ """A corpus data file this code cannot read; the message starts with its path."""
37
+
38
+
39
+ @dataclass(frozen=True)
40
+ class Village:
41
+ """A village's settings (`village.toml`); every part optional."""
42
+ projects: tuple[str, ...] = () # [geni] projects
43
+ project_rules: Mapping[str, str] = field(default=_EMPTY) # [geni.project_rules]
44
+ # The enabled plugins' tables, parsed by their hooks, by name; a table not written is absent.
45
+ tables: Mapping[str, Any] = field(default=MappingProxyType({}))
46
+
47
+ def table(self, name: str) -> Any:
48
+ """A plugin's table as its hook parsed it; None where the file does not write it."""
49
+ return self.tables.get(name)
50
+
51
+
52
+ # The keys of the core's part of a village.toml, by table ("" is the top level).
53
+ VILLAGE_KEYS: dict[str, frozenset[str]] = {
54
+ "": frozenset({"schema", "geni"}),
55
+ "geni": frozenset({"projects", "project_rules"}),
56
+ }
57
+
58
+
59
+ def read_toml(path: Path) -> dict[str, Any]:
60
+ try:
61
+ return tomllib.loads(path.read_text(encoding="utf-8"))
62
+ except tomllib.TOMLDecodeError as e:
63
+ raise CorpusDataError(t("corpus_data.not_toml", path=path, e=e)) from None
64
+
65
+
66
+ def check_keys(path: Path, table: str, data: Mapping[str, Any], allowed: Mapping[str, frozenset[str]]) -> None:
67
+ unknown = sorted(set(data) - allowed[table])
68
+ if unknown:
69
+ raise CorpusDataError(t("corpus_data.unknown_key", path=path, table=table + '.' if table else '', unknown=unknown[0]))
70
+
71
+
72
+ def wrong(path: Path, key: str, expected: object) -> CorpusDataError:
73
+ return CorpusDataError(t("corpus_data.wrong", path=path, key=key, expected=expected))
74
+
75
+
76
+ def table(path: Path, data: Mapping[str, Any], key: str) -> dict[str, Any]:
77
+ value = data.get(key, {})
78
+ if not isinstance(value, dict):
79
+ raise wrong(path, key, t("corpus_data.expected_table"))
80
+ return value # pyright: ignore[reportUnknownVariableType]
81
+
82
+
83
+ def strings(path: Path, key: str, value: Any) -> tuple[str, ...]:
84
+ if not isinstance(value, list) or not all(isinstance(v, str) for v in value): # pyright: ignore[reportUnknownVariableType]
85
+ raise wrong(path, key, t("corpus_data.expected_strings"))
86
+ return tuple(value) # pyright: ignore[reportUnknownArgumentType]
87
+
88
+
89
+ def string(path: Path, key: str, value: Any) -> str:
90
+ if not isinstance(value, str):
91
+ raise wrong(path, key, t("corpus_data.expected_string"))
92
+ return value
93
+
94
+
95
+ def string_map(path: Path, key: str, value: Mapping[str, Any]) -> Mapping[str, str]:
96
+ if not all(isinstance(v, str) for v in value.values()):
97
+ raise wrong(path, key, t("corpus_data.expected_string_values"))
98
+ return MappingProxyType(dict(value))
99
+
100
+
101
+ def check_schema(path: Path, data: Mapping[str, Any], newest: int) -> None:
102
+ schema = data.get("schema")
103
+ if not isinstance(schema, int) or isinstance(schema, bool):
104
+ raise wrong(path, "schema", t("corpus_data.expected_integer"))
105
+ if schema > newest:
106
+ raise CorpusDataError(t("corpus_data.schema_newer", path=path, schema=schema, newest=newest))
107
+
108
+
109
+ def corpus_file(name: str) -> Path | None:
110
+ """`name` in the workspace's `[corpus] dir`; None without a corpus or the file."""
111
+ corpus = workspace.current().corpus_dir
112
+ path = corpus / name if corpus is not None else None
113
+ return path if path is not None and path.is_file() else None
114
+
115
+
116
+ def _plugin_tables() -> tuple[dict[str, Callable[[Path, Mapping[str, Any]], Any]], WorkspaceError | None]:
117
+ """The village.toml tables the enabled plugins read, in the plugins' order; none without a
118
+ workspace — a village read outside one has only the core's part —, and then why."""
119
+ try:
120
+ enabled = plugins.enabled()
121
+ except WorkspaceError as error:
122
+ return {}, error
123
+ out: dict[str, Callable[[Path, Mapping[str, Any]], Any]] = {}
124
+ for plugin in enabled:
125
+ for name, parse in plugin.village_tables.items():
126
+ out.setdefault(name, parse)
127
+ return out, None
128
+
129
+
130
+ def village(village_dir: str | Path) -> Village:
131
+ """The settings of the village at `village_dir` (its `village.toml`); `Village()` without one."""
132
+ path = Path(village_dir) / VILLAGE_FILE
133
+ if not path.is_file():
134
+ return Village()
135
+ data = read_toml(path)
136
+ check_schema(path, data, VILLAGE_SCHEMA)
137
+ hooks, nowhere = _plugin_tables()
138
+ if nowhere is not None and (others := sorted(set(data) - VILLAGE_KEYS[""])):
139
+ raise CorpusDataError(t("corpus_data.no_workspace", path=path, key=others[0], why=nowhere))
140
+ check_keys(path, "", data, {"": VILLAGE_KEYS[""] | frozenset(hooks)})
141
+ geni = table(path, data, "geni")
142
+ check_keys(path, "geni", geni, VILLAGE_KEYS)
143
+ rules = geni.get("project_rules", {})
144
+ if not isinstance(rules, dict):
145
+ raise wrong(path, "geni.project_rules", t("corpus_data.expected_table"))
146
+ projects = strings(path, "geni.projects", geni.get("projects", []))
147
+ project_rules = string_map(path, "geni.project_rules", rules) # pyright: ignore[reportUnknownArgumentType]
148
+ tables = {name: parse(path, table(path, data, name)) for name, parse in hooks.items() if name in data}
149
+ return Village(projects=projects, project_rules=project_rules, tables=MappingProxyType(tables))