variable.py 14 KB

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