Esempio n. 1
0
    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"
Esempio n. 2
0
    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()
Esempio n. 3
0
    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()
Esempio n. 4
0
def test_validate_ok():
    validate({})
    validate({"header_": {"colname": {"bold": True}}})
Esempio n. 5
0
def test_validate_error():
    with pytest.raises(StyleValidationError):
        validate("not ok")