|
1 | 1 | """Implementation of units.""" |
2 | 2 | from deprecation import deprecated |
3 | 3 | import functools |
4 | | -from importlib.resources import read_text |
| 4 | +from importlib_resources import files |
5 | 5 | import os |
6 | 6 | from pathlib import Path |
7 | 7 | import re |
|
28 | 28 | ] |
29 | 29 |
|
30 | 30 |
|
31 | | -def _deploy_default_files() -> str: |
| 31 | +def _deploy_default_files() -> Tuple[Path, Path]: |
32 | 32 | """Copy the units & constants file into a temporary directory.""" |
33 | | - units_path = Path(_TEMP_DIRECTORY.name) / "citrine_en.txt" |
34 | | - units_path.write_text(read_text("gemd.units", "citrine_en.txt"), encoding="utf-8") |
| 33 | + resources = files("gemd.units") |
| 34 | + target_dir = Path(_TEMP_DIRECTORY.name) |
| 35 | + target_paths = tuple(target_dir / f for f in ("citrine_en.txt", "constants_en.txt")) |
| 36 | + for target in target_paths: |
| 37 | + source = resources.joinpath(target.name) |
| 38 | + target.write_text(source.read_text(), encoding="utf-8") |
35 | 39 |
|
36 | | - constants_path = Path(_TEMP_DIRECTORY.name) / "constants_en.txt" |
37 | | - constants_path.write_text(read_text("gemd.units", "constants_en.txt"), encoding="utf-8") |
| 40 | + return target_paths |
38 | 41 |
|
39 | | - return str(units_path) |
40 | 42 |
|
41 | | - |
42 | | -DEFAULT_FILE = _deploy_default_files() |
| 43 | +DEFAULT_FILE, DEFAULT_CONSTANTS = _deploy_default_files() |
43 | 44 | _ALLOWED_OPERATORS = {".", "+", "-", "*", "/", "//", "^", "**", "(", ")"} |
44 | 45 |
|
45 | 46 |
|
@@ -393,15 +394,14 @@ def change_definitions_file(filename: str = None): |
393 | 394 | if filename is None: |
394 | 395 | target = DEFAULT_FILE |
395 | 396 | else: |
396 | | - # TODO: Handle case where user provides a units file but no constants file |
397 | 397 | target = Path(filename).expanduser().resolve(strict=True) |
398 | 398 |
|
399 | 399 | current_dir = Path.cwd() |
400 | 400 | try: |
401 | | - path = Path(target) |
402 | | - os.chdir(path.parent) |
403 | | - # Need to re-verify path because of some slippiness around tmp on MacOS |
404 | | - _REGISTRY = _ScaleFactorRegistry(filename=Path.cwd() / path.name, |
| 401 | + os.chdir(target.parent) |
| 402 | + # Need to re-verify path because of some slippiness around tmp on macOS |
| 403 | + updated = (Path.cwd() / target.name).resolve(strict=True) |
| 404 | + _REGISTRY = _ScaleFactorRegistry(filename=updated, |
405 | 405 | preprocessors=[_space_after_minus_preprocessor, |
406 | 406 | _scientific_notation_preprocessor, |
407 | 407 | _scaling_preprocessor |
|
0 commit comments