variables.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import re
  2. from abc import ABCMeta, abstractmethod
  3. from typing import Iterator, Mapping, Optional, Pattern
  4. _posix_variable: Pattern[str] = re.compile(
  5. r"""
  6. \$\{
  7. (?P<name>[^\}:]*)
  8. (?::-
  9. (?P<default>[^\}]*)
  10. )?
  11. \}
  12. """,
  13. re.VERBOSE,
  14. )
  15. class Atom(metaclass=ABCMeta):
  16. def __ne__(self, other: object) -> bool:
  17. result = self.__eq__(other)
  18. if result is NotImplemented:
  19. return NotImplemented
  20. return not result
  21. @abstractmethod
  22. def resolve(self, env: Mapping[str, Optional[str]]) -> str: ...
  23. class Literal(Atom):
  24. def __init__(self, value: str) -> None:
  25. self.value = value
  26. def __repr__(self) -> str:
  27. return f"Literal(value={self.value})"
  28. def __eq__(self, other: object) -> bool:
  29. if not isinstance(other, self.__class__):
  30. return NotImplemented
  31. return self.value == other.value
  32. def __hash__(self) -> int:
  33. return hash((self.__class__, self.value))
  34. def resolve(self, env: Mapping[str, Optional[str]]) -> str:
  35. return self.value
  36. class Variable(Atom):
  37. def __init__(self, name: str, default: Optional[str]) -> None:
  38. self.name = name
  39. self.default = default
  40. def __repr__(self) -> str:
  41. return f"Variable(name={self.name}, default={self.default})"
  42. def __eq__(self, other: object) -> bool:
  43. if not isinstance(other, self.__class__):
  44. return NotImplemented
  45. return (self.name, self.default) == (other.name, other.default)
  46. def __hash__(self) -> int:
  47. return hash((self.__class__, self.name, self.default))
  48. def resolve(self, env: Mapping[str, Optional[str]]) -> str:
  49. default = self.default if self.default is not None else ""
  50. result = env.get(self.name, default)
  51. return result if result is not None else ""
  52. def parse_variables(value: str) -> Iterator[Atom]:
  53. cursor = 0
  54. for match in _posix_variable.finditer(value):
  55. (start, end) = match.span()
  56. name = match["name"]
  57. default = match["default"]
  58. if start > cursor:
  59. yield Literal(value=value[cursor:start])
  60. yield Variable(name=name, default=default)
  61. cursor = end
  62. length = len(value)
  63. if cursor < length:
  64. yield Literal(value=value[cursor:length])