async def output(name, block, line): final_string = stage(name, fmt_block(name, block, line)) assert final_string if getopt("lemonbar") is True: await lemonbar.write(final_string) if getopt("print_wrapper") is True: stdout(final_string)
def init(): """Initialize the global BLOCKS dict by creating the dict and the keys according to the blocks configured. The values of static blocks are immediately stored. For non-static blocks the block name will be stored, which acts as a placeholder until the value of the block is updated. By creating the keys the order of the blocks is preserved in the order they are configured.""" blocks = {} opts = getopt() if opts.blocks: for name in opts.blocks: try: blocks[name] = config["blocks"][name] except KeyError: print("Block not found: {}".format(name)) else: blocks = config["blocks"] for name, block in blocks.items(): if "static" in block: BLOCKS[name] = block["static"] else: # displayed as long as the module has not produced any output BLOCKS[name] = name # LOADING... return blocks
async def consume(self, generator: asyncio.streams.StreamReader): async for bstring in generator: try: self.proc.stdin.write(bstring) await self.proc.stdin.drain() except AttributeError as e: raise e if getopt("print_actions"): print(clean(bstring))
async def main(): blocks = init() procs = list(map(lambda x: run(x, blocks[x]), blocks)) if getopt("lemonbar"): await lemonbar.init() await shell.init() procs.append(shell.consume(lemonbar.proc.stdout)) await asyncio.sleep(.02) above() await asyncio.gather(*procs)
def add_actions(actions): for action in actions: if not isinstance(action, Action): if getopt("debug"): logger.debug("Action created: %s", action) action = Action(action, actions[action]) if action.button < 1 or action.button > 7: raise ValueError( "Invalid button assignment for action: {0}".format( actions[action])) pfx.append("%{{A{0}:{1}:}}".format(action.button, action.command)) sfx.append("%{A}")
def add_colors(fg=None, bg=None, rev=False): theme = get_colors("terminal") if getopt("debug"): logger.debug("Theme: %s", theme) logger.debug("Adding colors: fg=%s bg=%s rev=%s", fg, bg, rev) if fg: c = fg if fg[0] == "#" else theme[fg] pfx.append("%{{F{0}}}".format(c)) sfx.append("%{F-}") if bg: c = bg if not bg[0] == "#" else theme[bg] pfx.append("%{{B{0}}}".format(theme[c])) sfx.append("%{B-}") if rev: pfx.append("%{R}") sfx.append("%{R-}")
async def main(): blocks = init() procs = list(map(lambda x: run(x, blocks[x]), blocks)) if getopt("lemonbar"): await lemonbar.init() await shell.init() procs.append(shell.consume(lemonbar.proc.stdout)) await asyncio.sleep(.02) above() await asyncio.gather(*procs) if __name__ == "__main__": print("A {}".format(__name__), file=sys.stderr) #tracemalloc.start() if getopt("daemon"): fpid = os.fork() # fork to 0 if fpid != 0: sys.exit(0) # exit old process signal.signal(signal.SIGINT, signal_handler) asyncio.run(main()) # loops forever else: print("B {}".format(__name__), file=sys.stderr)
def fmt(self, line: str, **kwargs): """Format the output string (ie a block) to be parsed by lemonbar.""" actions = kwargs.get("actions", []) colors = kwargs.get("colors", {}) pad = kwargs.get("pad", None) pfx = kwargs.get("pfx", []) sfx = kwargs.get("sfx", []) def add_actions(actions): for action in actions: if not isinstance(action, Action): if getopt("debug"): logger.debug("Action created: %s", action) action = Action(action, actions[action]) if action.button < 1 or action.button > 7: raise ValueError( "Invalid button assignment for action: {0}".format( actions[action])) pfx.append("%{{A{0}:{1}:}}".format(action.button, action.command)) sfx.append("%{A}") def add_colors(fg=None, bg=None, rev=False): theme = get_colors("terminal") if getopt("debug"): logger.debug("Theme: %s", theme) logger.debug("Adding colors: fg=%s bg=%s rev=%s", fg, bg, rev) if fg: c = fg if fg[0] == "#" else theme[fg] pfx.append("%{{F{0}}}".format(c)) sfx.append("%{F-}") if bg: c = bg if not bg[0] == "#" else theme[bg] pfx.append("%{{B{0}}}".format(theme[c])) sfx.append("%{B-}") if rev: pfx.append("%{R}") sfx.append("%{R-}") add_actions(actions) add_colors(**colors) if sfx: sfx.reverse() try: if pad: fmt_out = "".join(pfx + [pad + str(line) + pad] + sfx) else: fmt_out = "".join(pfx + [str(line)] + sfx) if getopt("debug"): logger.debug('Fmt out: "%s" (%s)', fmt_out, len(fmt_out)) return fmt_out except TypeError as e: raise e