|
12 | 12 | from platform import platform
|
13 | 13 | from types import TracebackType
|
14 | 14 | from typing import Any, Dict, List, Optional, Tuple, Type, Union
|
| 15 | +import configparser |
| 16 | +import subprocess |
15 | 17 |
|
16 | 18 | import pyperclip
|
17 | 19 | import urwid
|
@@ -98,7 +100,7 @@ def __init__(
|
98 | 100 |
|
99 | 101 | self.show_loading()
|
100 | 102 | client_identifier = f"ZulipTerminal/{ZT_VERSION} {platform()}"
|
101 |
| - self.client = zulip.Client(config_file=config_file, client=client_identifier) |
| 103 | + self.client = zulip.Client(config_file=config_file, client=client_identifier,api_key=self.get_api_key(config_file)) |
102 | 104 | self.model = Model(self)
|
103 | 105 | self.view = View(self)
|
104 | 106 | # Start polling for events after view is rendered.
|
@@ -137,6 +139,25 @@ def raise_exception_in_main_thread(
|
137 | 139 | self._exception_info = exc_info
|
138 | 140 | self._critical_exception = critical
|
139 | 141 | os.write(self._exception_pipe, b"1")
|
| 142 | + |
| 143 | + def get_api_key(self,config_file: str) -> Optional[str]: |
| 144 | + config_file = os.path.expanduser(config_file) |
| 145 | + if config_file is not None and os.path.exists(config_file): |
| 146 | + config = configparser.ConfigParser() |
| 147 | + with open(config_file) as f: |
| 148 | + config.read_file(f, config_file) |
| 149 | + if config.has_section("api") and config.has_option("api", "passcmd"): |
| 150 | + result = subprocess.run(config["api"]["passcmd"].split(),capture_output=True) |
| 151 | + if result.returncode == 0: |
| 152 | + return result.stdout.decode().strip() |
| 153 | + else : |
| 154 | + raise RuntimeError("Error: Unable to retrieve API key.") |
| 155 | + else: |
| 156 | + raise ValueError("Error: Invalid config file format.") |
| 157 | + else: |
| 158 | + raise FileNotFoundError(f"Error: Config file '{config_file}' not found.") |
| 159 | + |
| 160 | + |
140 | 161 |
|
141 | 162 | def is_in_editor_mode(self) -> bool:
|
142 | 163 | return self._editor is not None
|
|
0 commit comments