github2gerrit 0.1.6__py3-none-any.whl → 0.1.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
github2gerrit/utils.py ADDED
@@ -0,0 +1,113 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ # SPDX-FileCopyrightText: 2025 The Linux Foundation
3
+
4
+ """Common utilities used across multiple modules.
5
+
6
+ This module consolidates helper functions that were previously duplicated
7
+ across cli.py, core.py, and gitutils.py to reduce maintenance overhead
8
+ and ensure consistent behavior.
9
+ """
10
+
11
+ import logging
12
+ import os
13
+ from typing import Any
14
+
15
+
16
+ def env_bool(name: str, default: bool = False) -> bool:
17
+ """Parse boolean environment variable correctly handling string values.
18
+
19
+ Args:
20
+ name: Environment variable name
21
+ default: Default value if variable is not set
22
+
23
+ Returns:
24
+ Boolean value parsed from environment variable
25
+ """
26
+ val = os.getenv(name)
27
+ if val is None:
28
+ return default
29
+ s = val.strip().lower()
30
+ return s in ("1", "true", "yes", "on")
31
+
32
+
33
+ def env_str(name: str, default: str = "") -> str:
34
+ """Get string environment variable with default fallback.
35
+
36
+ Args:
37
+ name: Environment variable name
38
+ default: Default value if variable is not set
39
+
40
+ Returns:
41
+ String value from environment variable or default
42
+ """
43
+ val = os.getenv(name)
44
+ return val if val is not None else default
45
+
46
+
47
+ def parse_bool_env(value: str | None) -> bool:
48
+ """Parse boolean environment variable correctly handling string values.
49
+
50
+ Args:
51
+ value: String value to parse as boolean
52
+
53
+ Returns:
54
+ Boolean value parsed from string
55
+ """
56
+ if value is None:
57
+ return False
58
+ s = value.strip().lower()
59
+ return s in ("1", "true", "yes", "on")
60
+
61
+
62
+ def is_verbose_mode() -> bool:
63
+ """Check if verbose mode is enabled via environment variable.
64
+
65
+ Returns:
66
+ True if G2G_VERBOSE environment variable is set to a truthy value
67
+ """
68
+ return os.getenv("G2G_VERBOSE", "").lower() in ("true", "1", "yes")
69
+
70
+
71
+ def log_exception_conditionally(logger: logging.Logger, message: str, *args: Any) -> None:
72
+ """Log exception with traceback only if verbose mode is enabled.
73
+
74
+ Args:
75
+ logger: Logger instance to use
76
+ message: Log message format string
77
+ *args: Arguments for message formatting
78
+ """
79
+ if is_verbose_mode():
80
+ logger.exception(message, *args)
81
+ else:
82
+ logger.error(message, *args)
83
+
84
+
85
+ def append_github_output(outputs: dict[str, str]) -> None:
86
+ """Append key-value pairs to GitHub Actions output file.
87
+
88
+ This function writes outputs to the GITHUB_OUTPUT file for use by
89
+ subsequent steps in a GitHub Actions workflow. It handles multiline
90
+ values using heredoc syntax when running in GitHub Actions.
91
+
92
+ Args:
93
+ outputs: Dictionary of key-value pairs to write to output
94
+ """
95
+ gh_out = os.getenv("GITHUB_OUTPUT")
96
+ if not gh_out:
97
+ return
98
+
99
+ try:
100
+ with open(gh_out, "a", encoding="utf-8") as fh:
101
+ for key, val in outputs.items():
102
+ if not val:
103
+ continue
104
+ if "\n" in val:
105
+ fh.write(f"{key}<<G2G\n")
106
+ fh.write(f"{val}\n")
107
+ fh.write("G2G\n")
108
+ else:
109
+ fh.write(f"{key}={val}\n")
110
+ except Exception as exc:
111
+ # Use a basic logger since we can't import from other modules
112
+ # without creating circular dependencies
113
+ logging.getLogger(__name__).debug("Failed to write GITHUB_OUTPUT: %s", exc)