|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from string import digits |
| 5 | + |
| 6 | +from django.conf import settings |
| 7 | +from django.core.exceptions import ImproperlyConfigured |
| 8 | + |
| 9 | +from lippukala.adapter.base import LippukalaAdapter |
| 10 | + |
| 11 | + |
| 12 | +def get_setting(name, default=None): |
| 13 | + return getattr(settings, f"LIPPUKALA_{name}", default) |
| 14 | + |
| 15 | + |
| 16 | +def get_integer_setting(name, default=0): |
| 17 | + try: |
| 18 | + value = get_setting(name, default) |
| 19 | + return int(value) |
| 20 | + except ValueError: # pragma: no cover |
| 21 | + raise ImproperlyConfigured(f"LIPPUKALA_{name} must be an integer (got {value!r})") |
| 22 | + |
| 23 | + |
| 24 | +class LippukalaSettings: |
| 25 | + def __init__(self) -> None: |
| 26 | + self.prefixes = get_setting("PREFIXES", {}) |
| 27 | + self.literate_keyspaces = get_setting("LITERATE_KEYSPACES", {}) |
| 28 | + self.code_min_n_digits = get_integer_setting("CODE_MIN_N_DIGITS", 10) |
| 29 | + self.code_max_n_digits = get_integer_setting("CODE_MAX_N_DIGITS", 10) |
| 30 | + self.code_allow_leading_zeroes = bool(get_setting("CODE_ALLOW_LEADING_ZEROES", True)) |
| 31 | + self.print_logo_path = get_setting("PRINT_LOGO_PATH") |
| 32 | + self.print_logo_size_cm = get_setting("PRINT_LOGO_SIZE_CM") |
| 33 | + |
| 34 | + if self.prefixes: |
| 35 | + self.prefix_choices = [(p, f"{p} [{t}]") for (p, t) in sorted(self.prefixes.items())] |
| 36 | + self.prefix_may_be_blank = False |
| 37 | + else: |
| 38 | + self.prefix_choices = [("", "---")] |
| 39 | + self.prefix_may_be_blank = True |
| 40 | + |
| 41 | + def validate(self) -> None: # pragma: no cover |
| 42 | + self._validate_code() |
| 43 | + self._validate_prefixes() |
| 44 | + self._validate_print() |
| 45 | + |
| 46 | + def _validate_code(self) -> None: |
| 47 | + if self.code_min_n_digits <= 5 or self.code_max_n_digits < self.code_min_n_digits: |
| 48 | + raise ImproperlyConfigured( |
| 49 | + f"The range ({self.code_min_n_digits} .. {self.code_max_n_digits}) for " |
| 50 | + f"Lippukala code digits is invalid" |
| 51 | + ) |
| 52 | + |
| 53 | + def _validate_prefixes(self): |
| 54 | + key_lengths = [len(k) for k in self.prefixes] |
| 55 | + if key_lengths and not all(k == key_lengths[0] for k in key_lengths): |
| 56 | + raise ImproperlyConfigured("All LIPPUKALA_PREFIXES keys must be the same length!") |
| 57 | + for prefix in self.prefixes: |
| 58 | + if not all(c in digits for c in prefix): |
| 59 | + raise ImproperlyConfigured( |
| 60 | + f"The prefix {prefix!r} has invalid characters. Only digits are allowed." |
| 61 | + ) |
| 62 | + for prefix, literate_keyspace in list(self.literate_keyspaces.items()): |
| 63 | + if isinstance(literate_keyspace, str): |
| 64 | + raise ImproperlyConfigured( |
| 65 | + f"A string ({literate_keyspace!r}) was passed as the " |
| 66 | + f"literate keyspace for prefix {prefix!r}" |
| 67 | + ) |
| 68 | + too_short_keys = any(len(key) <= 1 for key in literate_keyspace) |
| 69 | + maybe_duplicate = len(set(literate_keyspace)) != len(literate_keyspace) |
| 70 | + if too_short_keys or maybe_duplicate: |
| 71 | + raise ImproperlyConfigured( |
| 72 | + f"The literate keyspace for prefix {prefix!r} has invalid or duplicate entries." |
| 73 | + ) |
| 74 | + |
| 75 | + def _validate_print(self): |
| 76 | + if not self.print_logo_path: |
| 77 | + return |
| 78 | + if not os.path.isfile(self.print_logo_path): |
| 79 | + raise ImproperlyConfigured( |
| 80 | + f"PRINT_LOGO_PATH was defined, but does not exist ({self.print_logo_path!r})" |
| 81 | + ) |
| 82 | + if not all(float(s) > 0 for s in self.print_logo_size_cm): |
| 83 | + raise ImproperlyConfigured(f"PRINT_LOGO_SIZE_CM values not valid: {self.print_logo_size_cm!r}") |
| 84 | + |
| 85 | + |
| 86 | +class DefaultLippukalaAdapter(LippukalaAdapter): |
| 87 | + _settings: LippukalaSettings | None = None |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def get_settings(cls) -> LippukalaSettings: |
| 91 | + if not cls._settings: |
| 92 | + cls._settings = LippukalaSettings() |
| 93 | + cls._settings.validate() |
| 94 | + return cls._settings |
| 95 | + |
| 96 | + def get_prefixes(self) -> dict[str, str]: |
| 97 | + return self.get_settings().prefixes |
| 98 | + |
| 99 | + def get_literate_keyspace(self, prefix: str | None) -> list[str] | None: |
| 100 | + literate_keyspaces = self.get_settings().literate_keyspaces |
| 101 | + return literate_keyspaces.get(prefix) |
| 102 | + |
| 103 | + def get_code_digit_range(self, prefix: str) -> range: |
| 104 | + s = self.get_settings() |
| 105 | + return range(s.code_min_n_digits, s.code_max_n_digits + 1) |
| 106 | + |
| 107 | + def get_code_allow_leading_zeroes(self, prefix: str) -> bool: |
| 108 | + return self.get_settings().code_allow_leading_zeroes |
| 109 | + |
| 110 | + def get_print_logo_path(self, prefix: str) -> str | None: |
| 111 | + return self.get_settings().print_logo_path |
| 112 | + |
| 113 | + def get_print_logo_size_cm(self, prefix: str) -> tuple[float, float]: |
| 114 | + return self.get_settings().print_logo_size_cm |
0 commit comments