|
12 | 12 | import time |
13 | 13 | import webbrowser |
14 | 14 | from datetime import datetime |
| 15 | +from functools import partial |
15 | 16 | from pathlib import Path |
16 | 17 | from queue import Queue |
17 | 18 | from typing import List, Literal |
|
41 | 42 | log = logging.getLogger("murfey.client") |
42 | 43 |
|
43 | 44 |
|
| 45 | +def read_config() -> configparser.ConfigParser: |
| 46 | + config = configparser.ConfigParser() |
| 47 | + try: |
| 48 | + mcch = os.environ.get("MURFEY_CLIENT_CONFIG_HOME") |
| 49 | + murfey_client_config_home = Path(mcch) if mcch else Path.home() |
| 50 | + with open(murfey_client_config_home / ".murfey") as configfile: |
| 51 | + config.read_file(configfile) |
| 52 | + except FileNotFoundError: |
| 53 | + log.warning( |
| 54 | + f"Murfey client configuration file {murfey_client_config_home / '.murfey'} not found" |
| 55 | + ) |
| 56 | + if "Murfey" not in config: |
| 57 | + config["Murfey"] = {} |
| 58 | + return config |
| 59 | + |
| 60 | + |
| 61 | +token = read_config()["Murfey"].get("token", "") |
| 62 | + |
| 63 | +requests.get = partial(requests.get, headers={"Authorization": f"Bearer {token}"}) |
| 64 | +requests.post = partial(requests.post, headers={"Authorization": f"Bearer {token}"}) |
| 65 | +requests.delete = partial(requests.delete, headers={"Authorization": f"Bearer {token}"}) |
| 66 | + |
| 67 | + |
44 | 68 | def _enable_webbrowser_in_cygwin(): |
45 | 69 | """Helper function to make webbrowser.open() work in CygWin""" |
46 | 70 | if "cygwin" in platform.system().lower() and shutil.which("cygstart"): |
@@ -317,20 +341,6 @@ def main_loop( |
317 | 341 | time.sleep(15) |
318 | 342 |
|
319 | 343 |
|
320 | | -def read_config() -> configparser.ConfigParser: |
321 | | - config = configparser.ConfigParser() |
322 | | - try: |
323 | | - mcch = os.environ.get("MURFEY_CLIENT_CONFIG_HOME") |
324 | | - murfey_client_config_home = Path(mcch) if mcch else Path.home() |
325 | | - with open(murfey_client_config_home / ".murfey") as configfile: |
326 | | - config.read_file(configfile) |
327 | | - except FileNotFoundError: |
328 | | - pass |
329 | | - if "Murfey" not in config: |
330 | | - config["Murfey"] = {} |
331 | | - return config |
332 | | - |
333 | | - |
334 | 344 | def write_config(config: configparser.ConfigParser): |
335 | 345 | mcch = os.environ.get("MURFEY_CLIENT_CONFIG_HOME") |
336 | 346 | murfey_client_config_home = Path(mcch) if mcch else Path.home() |
|
0 commit comments