import json import os FILENAME = 'extra-commands.json' class ExtraCommands(dict): def __init__(self): self.last_mtime = 0 def reload(self): with open(FILENAME) as f: commands = json.load(f) self.clear() self.update(commands) def reload_if_needed(self): mtime = os.stat(FILENAME).st_mtime if mtime > self.last_mtime: self.last_mtime = mtime self.reload() def save(self): with open(FILENAME, 'w') as f: json.dump(self, f, indent=4, sort_keys=True)