def load_state (filename): STATE_VARS = { 'last_timestamp': str, 'last_revid': int, } DEFAULTS = { 'last_timestamp': None, 'last_revid': 0, } try: state = load_vars(STATE_VARS, filename) except VariablesMissing as e: # If the file was empty, use defaults. # Otherwise, something is wrong. Re-raise. if e.variables == set(STATE_VARS.keys()): state = DEFAULTS else: raise e return state
last_sent = time.time() CONF_VARS = { 'nick': str, 'realname': str, 'target': str, 'irc_host': str, 'irc_port': int, 'pid_file': str, 'fifo_file': str, } CONF_FILE = 'mecha.conf' conf = load_vars(CONF_VARS, CONF_FILE) pid_file = conf['pid_file'] try: with open(pid_file, 'x') as f: f.write('{:d}\n'.format(os.getpid())) except FileExistsError: print('PID file exists at {:s}.\nNot running.'.format(pid_file), file=sys.stderr) sys.exit(1) fifo = conf['fifo_file'] try: addr = (conf['irc_host'], conf['irc_port'])