aboutsummaryrefslogtreecommitdiffstats
path: root/package/utils/xfsprogs
Commit message (Expand)AuthorAgeFilesLines
* treewide: clean up download hashesFelix Fietkau2016-12-161-1/+1
* xfsprogs: install path consistent with fs toolsAlberto Bursi2016-10-181-7/+7
* xfsprogs: activate format-security checksHauke Mehrtens2015-11-228-25/+38
* cosmetic: remove trailing whitespacesLuka Perkov2015-10-151-1/+1
* license info - revert r43155John Crispin2014-11-031-5/+1
* Add more license tags with SPDX identifiersJohn Crispin2014-11-031-1/+5
* build: disable the PKG_CHECK_FORMAT_SECURITY check for the failing packagesFelix Fietkau2014-06-301-0/+1
* xfsprogs: fix compile errors with muslFelix Fietkau2014-06-093-10/+78
* xfsprogs: move to submenu FilesystemHauke Mehrtens2013-11-171-0/+1
* xfsprogs: disable blkid support to fix build errorsFelix Fietkau2013-07-061-1/+2
* packages: clean up the package folderJohn Crispin2013-06-217-0/+331
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
from __future__ import absolute_import

class StateObject(object):
    def _get_state(self):
        raise NotImplementedError  # pragma: nocover

    def _load_state(self, state):
        raise NotImplementedError  # pragma: nocover

    @classmethod
    def _from_state(cls, state):
        raise NotImplementedError  # pragma: nocover
        # Usually, this function roughly equals to the following code:
        # f = cls()
        # f._load_state(state)
        # return f

    def __eq__(self, other):
        try:
            return self._get_state() == other._get_state()
        except AttributeError:  # we may compare with something that's not a StateObject
            return False


class SimpleStateObject(StateObject):
    """
    A StateObject with opionated conventions that tries to keep everything DRY.

    Simply put, you agree on a list of attributes and their type.
    Attributes can either be primitive types(str, tuple, bool, ...) or StateObject instances themselves.
    SimpleStateObject uses this information for the default _get_state(), _from_state(s) and _load_state(s) methods.
    Overriding _get_state or _load_state to add custom adjustments is always possible.
    """

    _stateobject_attributes = None  # none by default to raise an exception if definition was forgotten
    """
    An attribute-name -> class-or-type dict containing all attributes that should be serialized
    If the attribute is a class, this class must be a subclass of StateObject.
    """

    def _get_state(self):
        return {attr: self._get_state_attr(attr, cls)
                for attr, cls in self._stateobject_attributes.iteritems()}

    def _get_state_attr(self, attr, cls):
        """
        helper for _get_state.
        returns the value of the given attribute
        """
        val = getattr(self, attr)
        if hasattr(val, "_get_state"):
            return val._get_state()
        else:
            return val

    def _load_state(self, state):
        for attr, cls in self._stateobject_attributes.iteritems():
            self._load_state_attr(attr, cls, state)

    def _load_state_attr(self, attr, cls, state):
        """
        helper for _load_state.
        loads the given attribute from the state.
        """
        if state.get(attr, None) is None:
            setattr(self, attr, None)
            return

        curr = getattr(self, attr)
        if hasattr(curr, "_load_state"):
            curr._load_state(state[attr])
        elif hasattr(cls, "_from_state"):
            setattr(self, attr, cls._from_state(state[attr]))
        else:
            setattr(self, attr, cls(state[attr]))