import sys import json import time import threading import core.theme import core.event import util.format def dump_json(obj): return obj.dict() def assign(src, dst, key, src_key=None, default=None): if not src_key: if key.startswith("_"): src_key = key else: src_key = key.replace("_", "-") # automagically replace _ with - for k in src_key if isinstance(src_key, list) else [src_key]: if k in src: dst[key] = src[k] return if default is not None: dst[key] = default class block(object): __COMMON_THEME_FIELDS = [ "separator", "separator-block-width", "default-separators", "border-top", "border-left", "border-right", "border-bottom", "fg", "bg", "padding", "prefix", "suffix", ] def __init__(self, theme, module, widget): self.__attributes = {} for key in self.__COMMON_THEME_FIELDS: tmp = theme.get(key, widget) if tmp is not None: self.__attributes[key] = tmp self.__attributes["name"] = module.id self.__attributes["instance"] = widget.id self.__attributes["prev-bg"] = theme.get("bg", "previous") def set(self, key, value): self.__attributes[key] = value def get(self, key, default=None): return self.__attributes.get(key, default) def is_pango(self, attr): if isinstance(attr, dict) and "pango" in attr: return True return False def pangoize(self, text): if not self.is_pango(text): return text self.__attributes["markup"] = "pango" attr = dict(text["pango"]) text = attr.get("full_text", "") if "full_text" in attr: del attr["full_text"] result = " 0: self.__offset -= 1 def scroll_right(self): self.__offset += 1 def blocks(self, module): blocks = [] if module.minimized: blocks.extend(self.separator_block(module, module.widgets()[0])) blocks.append(self.__content_block(module, module.widgets()[0])) self.__widgetcount += 1 return blocks width = self.__config.get("output.width", 0) for widget in module.widgets(): if module.scroll() == True and width > 0: self.__widgetcount += 1 if self.__widgetcount-1 < self.__offset: continue if self.__widgetcount-1 >= self.__offset + width: continue if widget.module and self.__config.autohide(widget.module.name): if not any( state in widget.state() for state in ["warning", "critical", "no-autohide"] ): continue if module.hidden(): continue if widget.hidden: continue if "critical" in widget.state() and self.__config.errorhide(widget.module.name): continue blocks.extend(self.separator_block(module, widget)) blocks.append(self.__content_block(module, widget)) core.event.trigger("next-widget") core.event.trigger("output.done", self.__offset, self.__widgetcount) return blocks def update(self, affected_modules=None, redraw_only=False, force=False): with self.__lock: self.update2(affected_modules, redraw_only, force) def update2(self, affected_modules=None, redraw_only=False, force=False): now = time.time() for module in self.__modules: if affected_modules and not module.id in affected_modules: continue if not affected_modules and module.next_update: if now < module.next_update and not force: continue if not redraw_only: module.update_wrapper() if module.parameter("interval", "") != "never": module.next_update = now + util.format.seconds( module.parameter("interval", self.__config.interval()) ) else: module.next_update = sys.maxsize for widget in module.widgets(): if not widget.id in self.__content: self.__content[widget.id] = { "minimized": False } self.__content[widget.id]["text"] = widget.full_text() def statusline(self): blocks = [] self.__widgetcount = 0 for module in self.__modules: blocks.extend(self.blocks(module)) return {"blocks": blocks, "suffix": ","} def wait(self, interval): time.sleep(interval) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4