Retrieves the proxy settings from GSettings on Linux systems. Returns a proxy string like "http://host:port" or None.
()
| 619 | |
| 620 | |
| 621 | def get_proxy_from_gsettings(): |
| 622 | """ |
| 623 | Retrieves the proxy settings from GSettings on Linux systems. |
| 624 | Returns a proxy string like "http://host:port" or None. |
| 625 | """ |
| 626 | |
| 627 | def _run_gsettings_command(command_parts: list[str]) -> str | None: |
| 628 | """Helper function to run gsettings command and return cleaned string output.""" |
| 629 | try: |
| 630 | process_result = subprocess.run( |
| 631 | command_parts, |
| 632 | capture_output=True, |
| 633 | text=True, |
| 634 | check=False, # Do not raise CalledProcessError for non-zero exit codes |
| 635 | timeout=1, # Timeout for the subprocess call |
| 636 | ) |
| 637 | if process_result.returncode == 0: |
| 638 | value = process_result.stdout.strip() |
| 639 | if value.startswith("'") and value.endswith( |
| 640 | "'" |
| 641 | ): # Remove surrounding single quotes |
| 642 | value = value[1:-1] |
| 643 | |
| 644 | # If after stripping quotes, value is empty, or it's a gsettings "empty" representation |
| 645 | if not value or value == "''" or value == "@as []" or value == "[]": |
| 646 | return None |
| 647 | return value |
| 648 | else: |
| 649 | return None |
| 650 | except subprocess.TimeoutExpired: |
| 651 | return None |
| 652 | except Exception: # Broad exception as per pseudocode |
| 653 | return None |
| 654 | |
| 655 | proxy_mode = _run_gsettings_command( |
| 656 | ["gsettings", "get", "org.gnome.system.proxy", "mode"] |
| 657 | ) |
| 658 | |
| 659 | if proxy_mode == "manual": |
| 660 | # Try HTTP proxy first |
| 661 | http_host = _run_gsettings_command( |
| 662 | ["gsettings", "get", "org.gnome.system.proxy.http", "host"] |
| 663 | ) |
| 664 | http_port_str = _run_gsettings_command( |
| 665 | ["gsettings", "get", "org.gnome.system.proxy.http", "port"] |
| 666 | ) |
| 667 | |
| 668 | if http_host and http_port_str: |
| 669 | try: |
| 670 | http_port = int(http_port_str) |
| 671 | if http_port > 0: |
| 672 | return f"http://{http_host}:{http_port}" |
| 673 | except ValueError: |
| 674 | pass # Continue to HTTPS |
| 675 | |
| 676 | # Try HTTPS proxy if HTTP not found or invalid |
| 677 | https_host = _run_gsettings_command( |
| 678 | ["gsettings", "get", "org.gnome.system.proxy.https", "host"] |
no test coverage detected