""" The Management - this module takes care of loading and running subordinate bureaus in the office organization. Bureaus are enabled in the file "~/.screenless/mgmt.ini": [mgmt] bureaus = ihr typing myburo and so on """ import configparser import importlib import multiprocessing import os import time def mgmt(): """ Primary management function. Main loop that starts and runs all bureaus. Some day this will do nice supervisory things like restart crashed buros. """ basepath = os.path.expanduser("~/.screenless") if not os.path.exists(basepath): os.mkdir(basepath) os.chdir(basepath) config = configparser.ConfigParser() procs = {} try: config.read("mgmt.ini") org_chart = config["mgmt"]["bureaus"].split() except KeyError: config["mgmt"] = {"bureaus": "ihr typing publicrelations photography jokes"} with open("mgmt.ini", "w") as configfile: config.write(configfile) print("created new mgmt.ini config file. please modify this to suit.") org_chart = config["mgmt"]["bureaus"].split() print("org chart:", org_chart) for buro in org_chart: lib = importlib.import_module("bureau." + buro) proc = multiprocessing.Process(target=lib.main) procs[buro] = proc proc.start() while True: for buro in org_chart: proc = procs[buro] if not proc.is_alive(): print("bureau", buro, "has crashed! Call the consultants!") #TODO this should probably restart in some sensible way time.sleep(1) if __name__ == "__main__": mgmt()