variable.py 12 KB

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