#!/usr/bin/env python3 # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. import json import appdirs import os from pathlib import Path appname = "NotePostCLI" appauthor = "Netsyms Technologies" CONFIG_FOLDER = appdirs.user_config_dir(appname, appauthor) CONFIG_FILE = CONFIG_FOLDER + "/settings.json" config = {} def checkconfig(): config_path = Path(CONFIG_FILE) if config_path.is_file(): try: # Attempt to read and parse the config file with open(CONFIG_FILE, 'r') as config_file: confjson = json.load(config_file) if "url" not in confjson: return False if "username" not in confjson: return False if "password" not in confjson: return False return True except Exception: return False else: return False def loadconfig(): global config with open(CONFIG_FILE, 'r') as config_file: config = json.load(config_file) def saveconfig(): global config jsonstring = json.dumps(config, sort_keys=True, indent=4) if not os.path.exists(CONFIG_FOLDER): os.makedirs(CONFIG_FOLDER) with open(CONFIG_FILE, 'w') as config_file: config_file.write(jsonstring) # Protect from other users who might want to peek at our credentials os.chmod(CONFIG_FILE, 0o600) def getconfig(): global config return config