config_defaults_env.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Entry factories for ``config_defaults.OPTIONAL_ENV_VARS`` (pure-data leaf, stdlib only)."""
  2. import threading
  3. _FILL_LOCK = threading.Lock() # held only to publish: the fill itself runs holding no lock
  4. _FILLING = threading.local() # .active: this thread is running the fill (it reads the table)
  5. class OptionalEnvVars(dict):
  6. """``OPTIONAL_ENV_VARS``: a dict that runs one armed fill before it is first read.
  7. Some entries (provider profiles') are only known after work that must not run at import
  8. (``hermes_cli.config_provider_env``). ``fill()`` returns the entries to add and may read
  9. config, so it runs holding no lock (concurrent first readers may each run it; the first to
  10. finish publishes, under a lock held only for that). Reads made by the fill itself see the
  11. table without them. A fill that raises publishes nothing and stays armed. Writes never fill."""
  12. _fill = None
  13. _published = None
  14. def arm(self, fill, published=None) -> None:
  15. """``fill() -> dict`` of entries; ``published(entries)`` runs once they are in the table."""
  16. self._fill, self._published = fill, published
  17. def _ready(self) -> None:
  18. fill = self._fill
  19. if fill is None or getattr(_FILLING, "active", False):
  20. return
  21. _FILLING.active = True
  22. try:
  23. entries = fill()
  24. finally:
  25. _FILLING.active = False
  26. with _FILL_LOCK:
  27. if self._fill is not fill:
  28. return # another reader published first
  29. for key, value in entries.items():
  30. dict.__setitem__(self, key, value)
  31. self._fill = None
  32. if self._published is not None:
  33. self._published(entries)
  34. def __getitem__(self, key):
  35. self._ready()
  36. return dict.__getitem__(self, key)
  37. def get(self, key, default=None):
  38. self._ready()
  39. return dict.get(self, key, default)
  40. def __contains__(self, key) -> bool:
  41. self._ready()
  42. return dict.__contains__(self, key)
  43. def __iter__(self):
  44. self._ready()
  45. return dict.__iter__(self)
  46. def __len__(self) -> int:
  47. self._ready()
  48. return dict.__len__(self)
  49. def keys(self):
  50. self._ready()
  51. return dict.keys(self)
  52. def items(self):
  53. self._ready()
  54. return dict.items(self)
  55. def values(self):
  56. self._ready()
  57. return dict.values(self)
  58. def copy(self) -> dict:
  59. self._ready()
  60. return dict(dict.items(self))
  61. def _env(description, prompt, **keys):
  62. """One OPTIONAL_ENV_VARS entry; keyword order is preserved as dict key order."""
  63. return {"description": description, "prompt": prompt, **keys}
  64. _OMIT = object()
  65. def _category(category, password, advanced):
  66. """Entry factory for one category with its usual password/advanced defaults.
  67. ``url``/``help``/``tools`` are only written when passed; ``password=None`` omits the key;
  68. ``advanced`` is only written when true. Key order matches the plain ``_env`` entries.
  69. """
  70. def make(description, prompt, url=_OMIT, *, help=_OMIT, tools=_OMIT, password=password,
  71. advanced=advanced):
  72. d = {"description": description, "prompt": prompt}
  73. d.update((k, v) for k, v in (("help", help), ("url", url), ("tools", tools)) if v is not _OMIT)
  74. if password is not None:
  75. d["password"] = password
  76. d["category"] = category
  77. if advanced:
  78. d["advanced"] = True
  79. return d
  80. return make
  81. _prov = _category("provider", password=True, advanced=True)
  82. _tool = _category("tool", password=True, advanced=False)
  83. _msg = _category("messaging", password=False, advanced=False)
  84. _skill = _category("skill", password=True, advanced=True)
  85. _setting = _category("setting", password=False, advanced=False)
  86. def _base_url(name, prompt_name=None):
  87. """Provider ``*_BASE_URL`` override entry (advanced, not a secret)."""
  88. prompt = f"{prompt_name or name} base URL (leave empty for default)"
  89. return _prov(f"{name} base URL override", prompt, None, password=False)