def _proc_group(self, style, adopt=True): """Return whether group is "default" or "override". In the case of "override", the self.fields pre-format and post-format processors will be set under the "override" key. Parameters ---------- style : dict A style that follows the schema defined in pyout.elements. adopt : bool, optional Merge `self.style` and `style`, giving priority to the latter's keys when there are conflicts. If False, treat `style` as a standalone style. """ fields = self.fields if style is not None: if adopt: style = elements.adopt(self.style, style) elements.validate(style) for column in self.columns: fields[column].add( "pre", "override", *(self.procgen.pre_from_style(style[column]))) fields[column].add( "post", "override", *(self.procgen.post_from_style(style[column]))) return "override" else: return "default"
def build(self, columns): """Build the style and fields. Parameters ---------- columns : list of str Column names. """ self.columns = columns default = dict(elements.default("default_"), **self.init_style.get("default_", {})) self.style = elements.adopt({c: default for c in columns}, self.init_style) # Store special keys in _style so that they can be validated. self.style["default_"] = default self.style["header_"] = self._compose("header_", {"align", "width"}) self.style["aggregate_"] = self._compose("aggregate_", {"align", "width"}) self.style["separator_"] = self.init_style.get( "separator_", elements.default("separator_")) lgr.debug("Validating style %r", self.style) self.style["width_"] = self.init_style.get( "width_", elements.default("width_")) elements.validate(self.style) self._setup_fields() self.hidden = {c: self.style[c]["hide"] for c in columns} self._reset_width_info()
def test_adopt_noop(): default_value = {"align": "<", "width": 10, "attrs": []} style = {"name": default_value, "path": default_value, "status": default_value} newstyle = adopt(style, None) for key, value in style.items(): assert newstyle[key] == value
def build(self, columns, table_width=None): """Build the style and fields. Parameters ---------- columns : list of str Column names. table_width : int, optional Table width to use instead of the previously specified width. """ self.columns = columns self._known_columns = set(columns) default = dict(elements.default("default_"), **self.init_style.get("default_", {})) self.style = elements.adopt({c: default for c in columns}, self.init_style) # Store special keys in _style so that they can be validated. self.style["default_"] = default self.style["header_"] = self._compose("header_", {"align", "width"}) self.style["aggregate_"] = self._compose("aggregate_", {"align", "width"}) self.style["separator_"] = self.init_style.get( "separator_", elements.default("separator_")) lgr.debug("Validating style %r", self.style) if table_width is not None: self._table_width = table_width elif self._table_width is None: self._table_width = self.init_style.get("width_", elements.default("width_")) self.style["width_"] = self._table_width elements.validate(self.style) self._setup_fields() self.hidden = {c: self.style[c]["hide"] for c in columns} self._reset_width_info()
def test_adopt(): default_value = {"align": "<", "width": 10, "attrs": []} style = {"name": default_value, "path": default_value, "status": default_value, "sep_": "non-mapping"} newstyle = adopt(style, {"path": {"width": 99}, "status": {"attrs": ["foo"]}, "sep_": "non-mapping update"}) for key, value in style.items(): if key == "path": expected = {"align": "<", "width": 99, "attrs": []} assert newstyle[key] == expected elif key == "status": expected = {"align": "<", "width": 10, "attrs": ["foo"]} assert newstyle[key] == expected elif key == "sep_": assert newstyle[key] == "non-mapping update" else: assert newstyle[key] == value