From c40e297f7894f742b333a266f08c5f7cde87b2d8 Mon Sep 17 00:00:00 2001 From: waltermaffy Date: Sun, 30 Apr 2023 16:19:58 +0200 Subject: [PATCH] fix UnboundLocalError when config_file is None --- pylnbits/config.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/pylnbits/config.py b/pylnbits/config.py index 40310f1..24f91bf 100644 --- a/pylnbits/config.py +++ b/pylnbits/config.py @@ -22,14 +22,19 @@ def __init__(self, config_file: str = "", try: if config_file: + if not os.path.exists(config_file): + raise FileNotFoundError(f"Config file: {config_file} not found!") + with open(config_file, "rb") as f: cfile = safe_load(f) print(cfile) f.close() - # add file error checking - self._in_key = cfile["in_key"] - self._lnbits_url = cfile["lnbits_url"] - self._admin_key = cfile["admin_key"] + + # add file error checking + self._in_key = cfile["in_key"] + self._lnbits_url = cfile["lnbits_url"] + self._admin_key = cfile["admin_key"] + except Exception as e: print(e) return e @@ -63,11 +68,15 @@ def admin_headers(self): if __name__ == "__main__": - c = Config(config_file="/Users/bitcarrot/laisee/pylnbits/config.yml") - - print(c.in_key) - print(c.admin_key) - print(c.lnbits_url) - print(c.headers()) - print(c.invoice_headers()) - print(c.admin_headers()) + c = Config( + in_key="test", + admin_key="test", + lnbits_url="https://legend.lnbits.com" + ) + + print(f"{c.in_key=}") + print(f"{c.admin_key=}") + print(f"{c.lnbits_url=}") + print(f"{c.headers()=}") + print(f"{c.invoice_headers()=}") + print(f"{c.admin_headers()=}")