variable.py 16 KB

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