Retrieves the proxy settings from GSettings on Linux systems. Returns a proxy string like "http://host:port" or None.
()
| 196 | |
| 197 | |
| 198 | def get_proxy_from_gsettings() -> Optional[str]: |
| 199 | """ |
| 200 | Retrieves the proxy settings from GSettings on Linux systems. |
| 201 | Returns a proxy string like "http://host:port" or None. |
| 202 | """ |
| 203 | |
| 204 | def _run_gsettings_command(command_parts: List[str]) -> Optional[str]: |
| 205 | """Helper function to run gsettings command and return cleaned string output.""" |
| 206 | try: |
| 207 | process_result = subprocess.run( |
| 208 | command_parts, |
| 209 | capture_output=True, |
| 210 | text=True, |
| 211 | check=False, # Do not raise CalledProcessError for non-zero exit codes |
| 212 | timeout=1, # Timeout for the subprocess call |
| 213 | ) |
| 214 | if process_result.returncode == 0: |
| 215 | value = process_result.stdout.strip() |
| 216 | if value.startswith("'") and value.endswith( |
| 217 | "'" |
| 218 | ): # Remove surrounding single quotes |
| 219 | value = value[1:-1] |
| 220 | |
| 221 | # If after stripping quotes, value is empty, or it's a gsettings "empty" representation |
| 222 | if not value or value == "''" or value == "@as []" or value == "[]": |
| 223 | return None |
| 224 | return value |
| 225 | else: |
| 226 | return None |
| 227 | except subprocess.TimeoutExpired: |
| 228 | return None |
| 229 | except Exception: # Broad exception as per pseudocode |
| 230 | return None |
| 231 | |
| 232 | proxy_mode = _run_gsettings_command( |
| 233 | ["gsettings", "get", "org.gnome.system.proxy", "mode"] |
| 234 | ) |
| 235 | |
| 236 | if proxy_mode == "manual": |
| 237 | # Try HTTP proxy first |
| 238 | http_host = _run_gsettings_command( |
| 239 | ["gsettings", "get", "org.gnome.system.proxy.http", "host"] |
| 240 | ) |
| 241 | http_port_str = _run_gsettings_command( |
| 242 | ["gsettings", "get", "org.gnome.system.proxy.http", "port"] |
| 243 | ) |
| 244 | |
| 245 | if http_host and http_port_str: |
| 246 | try: |
| 247 | http_port = int(http_port_str) |
| 248 | if http_port > 0: |
| 249 | return f"http://{http_host}:{http_port}" |
| 250 | except ValueError: |
| 251 | pass # Continue to HTTPS |
| 252 | |
| 253 | # Try HTTPS proxy if HTTP not found or invalid |
| 254 | https_host = _run_gsettings_command( |
| 255 | ["gsettings", "get", "org.gnome.system.proxy.https", "host"] |