Package libxyz :: Package core :: Module skin

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.core.skin

      1  #-*- coding: utf8 -*
      2  #
      3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008
      4  #
      5  # This file is part of XYZCommander.
      6  # XYZCommander is free software: you can redistribute it and/or modify
      7  # it under the terms of the GNU Lesser Public License as published by
      8  # the Free Software Foundation, either version 3 of the License, or
      9  # (at your option) any later version.
     10  # XYZCommander is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  # GNU Lesser Public License for more details.
     14  # You should have received a copy of the GNU Lesser Public License
     15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
     16
     17  from libxyz.exceptions import SkinError
     18  from fsrule import FSRule
     19  from odict import ODict
     20  from utils import ustring
     21
     22  from libxyz import const
     23
     24  from libxyz.ui.colors import Palette, Foreground, Background
     25
     26 -class SkinManager(object):
     27      """
     28      Store and manage defined skins
     29      """
     30
     31 -    def __init__(self):
     32          self._skins = {}
     33
     34      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     35
     36 -    def add(self, skin):
     37          """
     38          Add new sking to storage
     39          """
     40
     41          if not isinstance(skin, Skin):
     42              raise SkinError(_(u"Invalid skin argument: %s. "\
     43                                u"libxyz.core.Skin instance expected!"
     44                                % type(skin)))
     45          else:
     46              self._skins[skin.name] = skin
     47
     48      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     49
     50 -    def get(self, name):
     51          """
     52          Get stored skin instance
     53
     54          @return: Either Skin isntance or None if was not stored
     55          """
     56
     57          return self._skins.get(name, None)
     58
     59      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     60
     61 -    def clear(self):
     62          """
     63          Clear storage
     64          """
     65
     66          self._skins = {}
     67
     68  #++++++++++++++++++++++++++++++++++++++++++++++++
     69
     70 -class Skin(object):
     71      """
     72      Skin object. Provides simple interface to defined skin rulesets.
     73      """
     74
     75 -    def __init__(self, name=None, author=None, version=None,
     76                   description=None, colors=None, rules=None):
     77
     78          self.name = name
     79          self.author = author
     80          self.version = version
     81          self.description = description
     82          self.rules = self._make_rules(rules)
     83          self.colors = colors
     84          self.screen = None
     85
     86          # Default fallback palette
     87          self._default = Palette("default",
     88                                  Foreground("DEFAULT"),
     89                                  Background("DEFAULT"))
     90
     91      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     92
     93 -    def __str__(self):
     94          return u"<Skin object: %s>" % ustring(self.name)
     95
     96      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     97
     98 -    def __repr__(self):
     99          return self.__str__()
    100
    101      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    102
    103 -    def __getitem__(self, key):
    104          return self.rules[key]
    105
    106      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    107
    108 -    def set_screen(self, screen):
    109          self.screen = screen
    110
    111      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    112
    113 -    def _make_rules(self, raw_rules):
    114          """
    115          Internalize rules
    116          """
    117
    118          rules = {}
    119
    120          for block, rsets in raw_rules.iteritems():
    121              if block not in rules:
    122                  rules[block] = ODict()
    123
    124              for (resource, palette) in rsets:
    125                  if isinstance(resource, FSRule):
    126                      var = resource.raw_rule
    127                  else:
    128                      var = resource
    129
    130                  palette.name = self._make_name(block, var)
    131
    132                  rules[block][resource] = palette
    133
    134          return rules
    135
    136      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    137
    138 -    def _make_name(self, block, resource):
    139          return "%s@%s" % (resource, block)
    140
    141      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    142
    143 -    def get_palette_list(self, display=const.DEFAULT_DISPLAY_DRIVER):
    144          """
    145          Return list of defined palettes.
    146          It is usually passed to register_palette() function
    147          """
    148
    149          _list = [self._default.get_palette(display)]
    150
    151          for _name, _block in self.rules.iteritems():
    152              for _var, _val in _block.iteritems():
    153                  if isinstance(_val, Palette):
    154                      _list.append(_val.get_palette(display))
    155
    156          return _list
    157
    158      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    159
    160 -    def attr(self, resolution, name, default=True):
    161          """
    162          Search for first matching attribute <name> according to resolution
    163          @param resolution: Sequence of ruleset names
    164          @param name: Attribute name
    165          @param default: If True, return default palette in case attr
    166                          is not found, otherwise return None
    167          @return: Registered palette name
    168          """
    169
    170          return self.palette(resolution, name, default).name
    171
    172      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    173
    174 -    def palette(self, resolution, name, default=True):
    175          """
    176          Search for first matching palette <name> according to resolution
    177          """
    178
    179          for _w in resolution:
    180              # Normalize name
    181              if not _w.startswith(u"ui."):
    182                  _w = u"ui.%s" % _w
    183
    184              try:
    185                  return self.rules[_w][name]
    186              except KeyError:
    187                  pass
    188
    189          if default:
    190              return self._default
    191          else:
    192              return None
    193
    194      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    195
    196 -    def get_palette(self, block, name):
    197          try:
    198              return self.rules[block][name].name
    199          except KeyError:
    200              return None
    201