def save_json_file(data, path): guidance.offer() try: formatted_data = str(json.dumps(data, ensure_ascii=False)) with io.open(path, "wt", encoding="utf-8") as f: f.write(formatted_data) except Exception: simple_log(True)
def save_toml_file(data, path): guidance.offer() try: formatted_data = str(tomlkit.dumps(data)) with io.open(path, "wt", encoding="utf-8") as f: f.write(formatted_data) except Exception: simple_log(True)
def _save(data, path): """ Only to be used for settings file. :param data: :param path: :return: """ guidance.offer() try: formatted_data = str(tomlkit.dumps(data)) with io.open(path, "wt", encoding="utf-8") as f: f.write(formatted_data) except Exception as e: printer.out("Error saving toml file: {} {}".format(e, _SETTINGS_PATH))
def load_json_file(path): guidance.offer() result = {} try: with io.open(path, "rt", encoding="utf-8") as json_file: result = json.load(json_file) except IOError as e: if e.errno == 2: # The file doesn't exist. save_json_file(result, path) else: raise except Exception: simple_log(True) return result
def load_toml_file(path): guidance.offer() result = {} try: with io.open(path, "rt", encoding="utf-8") as f: result = tomlkit.loads(f.read()).value except IOError as e: if e.errno == 2: # The file doesn't exist. save_toml_file(result, path) else: raise except Exception: simple_log(True) return result
def _init(path): guidance.offer() result = {} try: with io.open(path, "rt", encoding="utf-8") as f: result = tomlkit.loads(f.read()).value except ValueError as e: printer.out("\n\n {} while loading settings file: {} \n\n".format(repr(e), path)) printer.out(sys.exc_info()) except IOError as e: printer.out("\n\n {} while loading settings file: {} \nAttempting to recover...\n\n".format(repr(e), path)) default_settings = _get_defaults() result, num_default_added = _deep_merge_defaults(result, default_settings) if num_default_added > 0: printer.out("Default settings values added: {} ".format(num_default_added)) _save(result, _SETTINGS_PATH) return result
def _init(path): guidance.offer() result = {} try: with io.open(path, "rt", encoding="utf-8") as f: result = tomlkit.loads(f.read()).value except ValueError as e: printer.out("\n\n" + repr(e) + " while loading settings file: " + path + "\n\n") printer.out(sys.exc_info()) except IOError as e: printer.out("\n\n" + repr(e) + " while loading settings file: " + path + "\nAttempting to recover...\n\n") default_settings = _get_defaults() result, num_default_added = _deep_merge_defaults(result, default_settings) # Temporary piece of code to seamlessly migrate clipboards to JSON if result["paths"]["SAVED_CLIPBOARD_PATH"].endswith(".toml"): old_clipboard = result["paths"]["SAVED_CLIPBOARD_PATH"] import json clipboard = {} new_path = old_clipboard[:-4] + "json" printer.out("\n\n Migrating clipboard from {} to {}".format( old_clipboard, new_path)) with io.open(old_clipboard, "rt", encoding="utf-8") as f: clipboard = tomlkit.loads(f.read()).value formatted_data = unicode(json.dumps(clipboard, ensure_ascii=False)) with io.open(new_path, "wt", encoding="utf-8") as f: f.write(formatted_data) result["paths"]["SAVED_CLIPBOARD_PATH"] = new_path if os.path.exists(old_clipboard): os.remove(old_clipboard) if not num_default_added: _save(result, _SETTINGS_PATH) if num_default_added > 0: printer.out("Default settings values added: %d " % num_default_added) _save(result, _SETTINGS_PATH) return result