예제 #1
0
파일: base.py 프로젝트: me-vlad/xyzcmd
    def _register_palettes(self, palettes):
        """
        Register custom plugin palettes in current skin
        """

        if palettes is None:
            return

        current_skin = self.xyz.skin.name

        for pname, p in palettes.iteritems():
            # Try current skin
            config = p.get(current_skin, None)

            # No entry for current skin, try default (None)
            if config is None:
                config = p.get(None, None)

                # No default entry - warn and continue
                if config is None:
                    xyzlog.error(_(u"Unable to register palette %s: "\
                                   u"neither entry for current skin (%s) "
                                   u"nor default one found") %
                                 (pname, current_skin))
                    continue

            palette = Palette(None, *Palette.convert(config))

            self.xyz.skin.add_custom_palette(u"ui."+self.ns.pfull,
                                             pname, palette)

            # Register new palette
            self.xyz.screen.register_palette(
                [palette.get_palette(self.xyz.driver)])
예제 #2
0
파일: skin.py 프로젝트: me-vlad/xyzcmd
    def __init__(self, name=None, author=None, version=None,
                 description=None, colors=None, rules=None):

        self.name = name
        self.author = author
        self.version = version
        self.description = description
        self.rules = self._make_rules(rules)
        self.colors = colors
        self.screen = None

        # Default fallback palette
        self._default = Palette("default", 
                                Foreground("DEFAULT"),
                                Background("DEFAULT"))
예제 #3
0
파일: dsl.py 프로젝트: me-vlad/xyzcmd
    def palette(cls, config):
        """
        Create internal palette object

        @param config: Dictionary of form:
        {
           'foreground': COLOR,
           'background': COLOR,
           'fg_attributes': [ATTR],
           'mono': [ATTR],
           'foreground_high': HG_COLOR,
           'background_high': HG_COLOR
        }
        """

        try:
            return Palette(None, *Palette.convert(config))
        except Exception, e:
            error(_(u"Error creating Palette instance: %s") % unicode(e))
예제 #4
0
파일: skin.py 프로젝트: me-vlad/xyzcmd
class Skin(object):
    """
    Skin object. Provides simple interface to defined skin rulesets.
    """

    def __init__(self, name=None, author=None, version=None,
                 description=None, colors=None, rules=None):

        self.name = name
        self.author = author
        self.version = version
        self.description = description
        self.rules = self._make_rules(rules)
        self.colors = colors
        self.screen = None

        # Default fallback palette
        self._default = Palette("default", 
                                Foreground("DEFAULT"),
                                Background("DEFAULT"))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def __str__(self):
        return u"<Skin object: %s>" % ustring(self.name)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def __repr__(self):
        return self.__str__()

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def __cmp__(self, other):
        return cmp(self.name, other.name)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    def __getitem__(self, key):
        return self.rules[key]

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def set_screen(self, screen):
        self.screen = screen

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def add_custom_palette(self, block, res, palette):
        """
        Register custom palette
        """

        if block not in self.rules:
            self.rules[block] = ODict()

        palette.name = self._make_name(block, res)

        self.rules[block][res] = palette

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def _make_rules(self, raw_rules):
        """
        Internalize rules
        """

        rules = {}

        for block, rsets in raw_rules.iteritems():
            if block not in rules:
                rules[block] = ODict()

            for (resource, palette) in rsets:
                if isinstance(resource, FSRule):
                    var = resource.raw_rule
                else:
                    var = resource

                palette.name = self._make_name(block, var)

                rules[block][resource] = palette

        return rules
                
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def _make_name(self, block, resource):
        return "%s@%s" % (resource, block)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def get_palette_list(self, display=const.DEFAULT_DISPLAY_DRIVER):
        """
        Return list of defined palettes.
        It is usually passed to register_palette() function
        """

        _list = [self._default.get_palette(display)]

        for _name, _block in self.rules.iteritems():
            for _var, _val in _block.iteritems():
                if isinstance(_val, Palette):
                    _list.append(_val.get_palette(display))

        return _list

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def attr(self, resolution, name, default=True):
        """
        Search for first matching attribute <name> according to resolution
        @param resolution: Sequence of ruleset names
        @param name: Attribute name
        @param default: If True, return default palette in case attr
                        is not found, otherwise return None
        @return: Registered palette name
        """

        return self.palette(resolution, name, default).name

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def palette(self, resolution, name, default=True):
        """
        Search for first matching palette <name> according to resolution
        """

        for _w in resolution:
            # Normalize name
            if not _w.startswith(u"ui."):
                _w = u"ui.%s" % _w

            try:
                return self.rules[_w][name]
            except KeyError:
                pass

        if default:
            return self._default
        else:
            return None

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def get_palette(self, block, name):
        try:
            return self.rules[block][name].name
        except KeyError:
            return None