variable.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. from __future__ import annotations
  2. from typing import Any, Dict, List, Optional, Set
  3. from urllib.parse import urlparse
  4. import logging
  5. import re
  6. logger = logging.getLogger(__name__)
  7. TRUE_VALUES = {"true", "1", "yes", "on"}
  8. FALSE_VALUES = {"false", "0", "no", "off"}
  9. EMAIL_REGEX = re.compile(r"^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$")
  10. class Variable:
  11. """Represents a single templating variable with lightweight validation."""
  12. def __init__(self, data: dict[str, Any]) -> None:
  13. """Initialize Variable from a dictionary containing variable specification.
  14. Args:
  15. data: Dictionary containing variable specification with required 'name' key
  16. and optional keys: description, type, options, prompt, value, default, section, origin
  17. Raises:
  18. ValueError: If data is not a dict, missing 'name' key, or has invalid default value
  19. """
  20. # Validate input
  21. if not isinstance(data, dict):
  22. raise ValueError("Variable data must be a dictionary")
  23. if "name" not in data:
  24. raise ValueError("Variable data must contain 'name' key")
  25. # Track which fields were explicitly provided in source data
  26. self._explicit_fields: Set[str] = set(data.keys())
  27. # Initialize fields
  28. self.name: str = data["name"]
  29. self.description: Optional[str] = data.get("description") or data.get(
  30. "display", ""
  31. )
  32. self.type: str = data.get("type", "str")
  33. self.options: Optional[List[Any]] = data.get("options", [])
  34. self.prompt: Optional[str] = data.get("prompt")
  35. if "value" in data:
  36. self.value: Any = data.get("value")
  37. elif "default" in data:
  38. self.value: Any = data.get("default")
  39. else:
  40. self.value: Any = None
  41. self.origin: Optional[str] = data.get("origin")
  42. self.sensitive: bool = data.get("sensitive", False)
  43. # Optional extra explanation used by interactive prompts
  44. self.extra: Optional[str] = data.get("extra")
  45. # Flag indicating this variable should be auto-generated when empty
  46. self.autogenerated: bool = data.get("autogenerated", False)
  47. # Flag indicating this variable is required even when section is disabled
  48. self.required: bool = data.get("required", False)
  49. # Flag indicating this variable can be empty/optional
  50. self.optional: bool = data.get("optional", False)
  51. # Original value before config override (used for display)
  52. self.original_value: Optional[Any] = data.get("original_value")
  53. # Variable dependencies - can be string or list of strings in format "var_name=value"
  54. needs_value = data.get("needs")
  55. if needs_value:
  56. if isinstance(needs_value, str):
  57. self.needs: List[str] = [needs_value]
  58. elif isinstance(needs_value, list):
  59. self.needs: List[str] = needs_value
  60. else:
  61. raise ValueError(
  62. f"Variable '{self.name}' has invalid 'needs' value: must be string or list"
  63. )
  64. else:
  65. self.needs: List[str] = []
  66. # Validate and convert the default/initial value if present
  67. if self.value is not None:
  68. try:
  69. self.value = self.convert(self.value)
  70. except ValueError as exc:
  71. raise ValueError(f"Invalid default for variable '{self.name}': {exc}")
  72. def convert(self, value: Any) -> Any:
  73. """Validate and convert a raw value based on the variable type.
  74. This method performs type conversion but does NOT check if the value
  75. is required. Use validate_and_convert() for full validation including
  76. required field checks.
  77. """
  78. if value is None:
  79. return None
  80. # Treat empty strings as None to avoid storing "" for missing values.
  81. if isinstance(value, str) and value.strip() == "":
  82. return None
  83. # Type conversion mapping for cleaner code
  84. converters = {
  85. "bool": self._convert_bool,
  86. "int": self._convert_int,
  87. "float": self._convert_float,
  88. "enum": self._convert_enum,
  89. "url": self._convert_url,
  90. "email": self._convert_email,
  91. }
  92. converter = converters.get(self.type)
  93. if converter:
  94. return converter(value)
  95. # Default to string conversion
  96. return str(value)
  97. def validate_and_convert(self, value: Any, check_required: bool = True) -> Any:
  98. """Validate and convert a value with comprehensive checks.
  99. This method combines type conversion with validation logic including
  100. required field checks. It's the recommended method for user input validation.
  101. Args:
  102. value: The raw value to validate and convert
  103. check_required: If True, raises ValueError for required fields with empty values
  104. Returns:
  105. The converted and validated value
  106. Raises:
  107. ValueError: If validation fails (invalid format, required field empty, etc.)
  108. Examples:
  109. # Basic validation
  110. var.validate_and_convert("example@email.com") # Returns validated email
  111. # Required field validation
  112. var.validate_and_convert("", check_required=True) # Raises ValueError if required
  113. # Autogenerated variables - allow empty values
  114. var.validate_and_convert("", check_required=False) # Returns None for autogeneration
  115. """
  116. # First, convert the value using standard type conversion
  117. converted = self.convert(value)
  118. # Special handling for autogenerated variables
  119. # Allow empty values as they will be auto-generated later
  120. if self.autogenerated and (
  121. converted is None
  122. or (
  123. isinstance(converted, str) and (converted == "" or converted == "*auto")
  124. )
  125. ):
  126. return None # Signal that auto-generation should happen
  127. # Allow empty values for optional variables
  128. if self.optional and (
  129. converted is None or (isinstance(converted, str) and converted == "")
  130. ):
  131. return None
  132. # Check if this is a required field and the value is empty
  133. if check_required and self.is_required():
  134. if converted is None or (isinstance(converted, str) and converted == ""):
  135. raise ValueError("This field is required and cannot be empty")
  136. return converted
  137. def _convert_bool(self, value: Any) -> bool:
  138. """Convert value to boolean."""
  139. if isinstance(value, bool):
  140. return value
  141. if isinstance(value, str):
  142. lowered = value.strip().lower()
  143. if lowered in TRUE_VALUES:
  144. return True
  145. if lowered in FALSE_VALUES:
  146. return False
  147. raise ValueError("value must be a boolean (true/false)")
  148. def _convert_int(self, value: Any) -> Optional[int]:
  149. """Convert value to integer."""
  150. if isinstance(value, int):
  151. return value
  152. if isinstance(value, str) and value.strip() == "":
  153. return None
  154. try:
  155. return int(value)
  156. except (TypeError, ValueError) as exc:
  157. raise ValueError("value must be an integer") from exc
  158. def _convert_float(self, value: Any) -> Optional[float]:
  159. """Convert value to float."""
  160. if isinstance(value, float):
  161. return value
  162. if isinstance(value, str) and value.strip() == "":
  163. return None
  164. try:
  165. return float(value)
  166. except (TypeError, ValueError) as exc:
  167. raise ValueError("value must be a float") from exc
  168. def _convert_enum(self, value: Any) -> Optional[str]:
  169. if value == "":
  170. return None
  171. val = str(value)
  172. if self.options and val not in self.options:
  173. raise ValueError(f"value must be one of: {', '.join(self.options)}")
  174. return val
  175. def _convert_url(self, value: Any) -> str:
  176. val = str(value).strip()
  177. if not val:
  178. return None
  179. parsed = urlparse(val)
  180. if not (parsed.scheme and parsed.netloc):
  181. raise ValueError("value must be a valid URL (include scheme and host)")
  182. return val
  183. def _convert_email(self, value: Any) -> str:
  184. val = str(value).strip()
  185. if not val:
  186. return None
  187. if not EMAIL_REGEX.fullmatch(val):
  188. raise ValueError("value must be a valid email address")
  189. return val
  190. def to_dict(self) -> Dict[str, Any]:
  191. """Serialize Variable to a dictionary for storage."""
  192. result = {}
  193. # Always include type
  194. if self.type:
  195. result["type"] = self.type
  196. # Include value/default if not None
  197. if self.value is not None:
  198. result["default"] = self.value
  199. # Include string fields if truthy
  200. for field in ("description", "prompt", "extra", "origin"):
  201. if value := getattr(self, field):
  202. result[field] = value
  203. # Include boolean/list fields if truthy (but empty list is OK for options)
  204. if self.sensitive:
  205. result["sensitive"] = True
  206. if self.autogenerated:
  207. result["autogenerated"] = True
  208. if self.required:
  209. result["required"] = True
  210. if self.optional:
  211. result["optional"] = True
  212. if self.options is not None: # Allow empty list
  213. result["options"] = self.options
  214. # Store dependencies (single value if only one, list otherwise)
  215. if self.needs:
  216. result["needs"] = self.needs[0] if len(self.needs) == 1 else self.needs
  217. return result
  218. def get_display_value(
  219. self, mask_sensitive: bool = True, max_length: int = 30, show_none: bool = True
  220. ) -> str:
  221. """Get formatted display value with optional masking and truncation.
  222. Args:
  223. mask_sensitive: If True, mask sensitive values with asterisks
  224. max_length: Maximum length before truncation (0 = no limit)
  225. show_none: If True, display "(none)" for None values instead of empty string
  226. Returns:
  227. Formatted string representation of the value
  228. """
  229. if self.value is None or self.value == "":
  230. # Show (*auto) for autogenerated variables instead of (none)
  231. if self.autogenerated:
  232. return "[dim](*auto)[/dim]" if show_none else ""
  233. return "[dim](none)[/dim]" if show_none else ""
  234. # Mask sensitive values
  235. if self.sensitive and mask_sensitive:
  236. return "********"
  237. # Convert to string
  238. display = str(self.value)
  239. # Truncate if needed
  240. if max_length > 0 and len(display) > max_length:
  241. return display[: max_length - 3] + "..."
  242. return display
  243. def get_normalized_default(self) -> Any:
  244. """Get normalized default value suitable for prompts and display."""
  245. try:
  246. typed = self.convert(self.value)
  247. except Exception:
  248. typed = self.value
  249. # Autogenerated: return display hint
  250. if self.autogenerated and not typed:
  251. return "*auto"
  252. # Type-specific handlers
  253. if self.type == "enum":
  254. if not self.options:
  255. return typed
  256. return (
  257. self.options[0]
  258. if typed is None or str(typed) not in self.options
  259. else str(typed)
  260. )
  261. if self.type == "bool":
  262. return (
  263. typed
  264. if isinstance(typed, bool)
  265. else (None if typed is None else bool(typed))
  266. )
  267. if self.type == "int":
  268. try:
  269. return int(typed) if typed not in (None, "") else None
  270. except Exception:
  271. return None
  272. # Default: return string or None
  273. return None if typed is None else str(typed)
  274. def get_prompt_text(self) -> str:
  275. """Get formatted prompt text for interactive input.
  276. Returns:
  277. Prompt text with optional type hints and descriptions
  278. """
  279. prompt_text = self.prompt or self.description or self.name
  280. # Add type hint for semantic types if there's a default
  281. if self.value is not None and self.type in ["email", "url"]:
  282. prompt_text += f" ({self.type})"
  283. return prompt_text
  284. def get_validation_hint(self) -> Optional[str]:
  285. """Get validation hint for prompts (e.g., enum options).
  286. Returns:
  287. Formatted hint string or None if no hint needed
  288. """
  289. hints = []
  290. # Add enum options
  291. if self.type == "enum" and self.options:
  292. hints.append(f"Options: {', '.join(self.options)}")
  293. # Add extra help text
  294. if self.extra:
  295. hints.append(self.extra)
  296. return " — ".join(hints) if hints else None
  297. def is_required(self) -> bool:
  298. """Check if this variable requires a value (cannot be empty/None).
  299. A variable is considered required if:
  300. - It has an explicit 'required: true' flag (highest precedence)
  301. - OR it doesn't have a default value (value is None)
  302. AND it's not marked as autogenerated (which can be empty and generated later)
  303. AND it's not marked as optional (which can be empty)
  304. AND it's not a boolean type (booleans default to False if not set)
  305. Returns:
  306. True if the variable must have a non-empty value, False otherwise
  307. """
  308. # Optional variables can always be empty
  309. if self.optional:
  310. return False
  311. # Explicit required flag takes highest precedence
  312. if self.required:
  313. # But autogenerated variables can still be empty (will be generated later)
  314. if self.autogenerated:
  315. return False
  316. return True
  317. # Autogenerated variables can be empty (will be generated later)
  318. if self.autogenerated:
  319. return False
  320. # Boolean variables always have a value (True or False)
  321. if self.type == "bool":
  322. return False
  323. # Variables with a default value are not required
  324. if self.value is not None:
  325. return False
  326. # No default value and not autogenerated = required
  327. return True
  328. def clone(self, update: Optional[Dict[str, Any]] = None) -> "Variable":
  329. """Create a deep copy of the variable with optional field updates.
  330. This is more efficient than converting to dict and back when copying variables.
  331. Args:
  332. update: Optional dictionary of field updates to apply to the clone
  333. Returns:
  334. New Variable instance with copied data
  335. Example:
  336. var2 = var1.clone(update={'origin': 'template'})
  337. """
  338. data = {
  339. "name": self.name,
  340. "type": self.type,
  341. "value": self.value,
  342. "description": self.description,
  343. "prompt": self.prompt,
  344. "options": self.options.copy() if self.options else None,
  345. "origin": self.origin,
  346. "sensitive": self.sensitive,
  347. "extra": self.extra,
  348. "autogenerated": self.autogenerated,
  349. "required": self.required,
  350. "optional": self.optional,
  351. "original_value": self.original_value,
  352. "needs": self.needs.copy() if self.needs else None,
  353. }
  354. # Apply updates if provided
  355. if update:
  356. data.update(update)
  357. # Create new variable
  358. cloned = Variable(data)
  359. # Preserve explicit fields from original, and add any update keys
  360. cloned._explicit_fields = self._explicit_fields.copy()
  361. if update:
  362. cloned._explicit_fields.update(update.keys())
  363. return cloned