|
| 1 | +import json as __json |
| 2 | +import platform as __platform |
| 3 | +import os as __os |
| 4 | +import sys as __sys |
| 5 | +import logging as __logging |
| 6 | +import gnupg as __gnupg |
| 7 | + |
| 8 | + |
| 9 | +__logger = __logging.getLogger(__name__) |
| 10 | +__logger.setLevel(__logging.DEBUG) |
| 11 | + |
| 12 | +handler = __logging.StreamHandler(__sys.stderr) |
| 13 | +handler.setLevel(__logging.DEBUG) |
| 14 | +formatter = __logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 15 | +handler.setFormatter(formatter) |
| 16 | + |
| 17 | +# Comment out this line below if you do no more want any logging messages |
| 18 | +__logger.addHandler(handler) |
| 19 | + |
| 20 | +# __logger.removeHandler(handler) |
| 21 | + |
| 22 | +DEFAULT_CONFIG_FILE = [ |
| 23 | + ( |
| 24 | + __os.path.join(__os.environ["USERPROFILE"], ".config", "mpqp.json.gpg") |
| 25 | + if "Windows" in __platform.system() |
| 26 | + else __os.path.join( |
| 27 | + ( |
| 28 | + __os.environ["XDG_CONFIG_HOME"] |
| 29 | + if "XDG_CONFIG_HOME" in __os.environ.keys() |
| 30 | + else __os.environ["HOME"] |
| 31 | + ), |
| 32 | + "mpqp.gpg", |
| 33 | + ) |
| 34 | + ), |
| 35 | + ( |
| 36 | + __os.path.join(__os.environ["USERPROFILE"], ".mpqp.json.gpg") |
| 37 | + if "Windows" in __platform.system() |
| 38 | + else __os.path.join(__os.environ["HOME"], ".mpqp.json.gpg") |
| 39 | + ), |
| 40 | + ( |
| 41 | + __os.path.join(__os.environ["USERPROFILE"], ".config", "mpqp.json") |
| 42 | + if "Windows" in __platform.system() |
| 43 | + else __os.path.join( |
| 44 | + ( |
| 45 | + __os.environ["XDG_CONFIG_HOME"] |
| 46 | + if "XDG_CONFIG_HOME" in __os.environ.keys() |
| 47 | + else __os.environ["HOME"] |
| 48 | + ), |
| 49 | + "mpqp.json", |
| 50 | + ) |
| 51 | + ), |
| 52 | + ( |
| 53 | + __os.path.join(__os.environ["USERPROFILE"], ".mpqp.json") |
| 54 | + if "Windows" in __platform.system() |
| 55 | + else __os.path.join(__os.environ["HOME"], ".mpqp.json") |
| 56 | + ), |
| 57 | +] |
| 58 | + |
| 59 | +__logger.info("Default config file list set to " + str(DEFAULT_CONFIG_FILE)) |
| 60 | + |
| 61 | + |
| 62 | +data = None |
| 63 | +ibm = None |
| 64 | +aws = None |
| 65 | +qlm = None |
| 66 | + |
| 67 | + |
| 68 | +def load(configFile: str | None = None): |
| 69 | + if configFile is None: |
| 70 | + for f in DEFAULT_CONFIG_FILE: |
| 71 | + __logger.info("Trying file \"%s\"" % f) |
| 72 | + if __os.path.isfile(f): |
| 73 | + configFile = f |
| 74 | + break |
| 75 | + |
| 76 | + if configFile is not None: |
| 77 | + configStr: str | None = None |
| 78 | + |
| 79 | + if configFile.endswith("gpg"): |
| 80 | + gpg = __gnupg.GPG(use_agent=True) |
| 81 | + gpg.encoding = "utf-8" |
| 82 | + decryptData = gpg.decrypt_file(open(configFile, mode="rb"), passphrase=None) |
| 83 | + |
| 84 | + if decryptData.ok: # pyright: ignore[reportAttributeAccessIssue] |
| 85 | + configStr = str(decryptData) |
| 86 | + else: |
| 87 | + __logger.error( |
| 88 | + "Decryption failed with message \"%s\"" |
| 89 | + % decryptData.status # pyright: ignore[reportAttributeAccessIssue] |
| 90 | + ) |
| 91 | + else: |
| 92 | + with open(configFile, mode="rb") as fi: |
| 93 | + configStr = fi.read() |
| 94 | + |
| 95 | + if configStr is not None: |
| 96 | + global data, ibm, aws, qlm |
| 97 | + data = __json.loads(configStr) |
| 98 | + ibm = data['IBM'] if 'IBM' in data.keys() else None |
| 99 | + aws = data['AWS'] if 'AWS' in data.keys() else None |
| 100 | + qlm = data['QLM'] if 'QLM' in data.keys() else None |
| 101 | + |
| 102 | + |
| 103 | +use_profile = None |
0 commit comments