~glitter-team/glitter/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# !/usr/bin/python
# -*- coding: utf-8 -*-

# Glitter Toolkit

__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
__licenses__ = ["LICENSE.LGPL"]
__description__ = "Glitter theme" 

import os
import sys

import gobject
import simplejson

from style import Style

_DEFAULT_THEME = None

class Theme(gobject.GObject):
    """ 
    A Glitter theme holds the styles for individual widgets in their various 
    states, theme resources and other misc configuration data.
    
    """
    
    def __init__(self, name):
        """ Initialize theme """
    
        super(Theme, self).__init__()
        
        self.name = name
        self.theme_path = None
        
        self.widget_styles = {}
    
        self._init_common()
        self._init_styles()
    
    def _init_common(self):
        """ Common initialization procedures """
        
        theme_names = []
        themes_path = os.path.join(
                        os.path.dirname(os.path.abspath(__file__)),
                        'data',
                        'themes'
                      )
        for entry in os.listdir(themes_path):
            if os.path.isdir(os.path.join(themes_path, entry)):
                theme_names.append(entry)
        
        if self.name in theme_names:
            self.theme_path = os.path.join(
                                os.path.dirname(os.path.abspath(__file__)),
                                'data',
                                'themes',
                                self.name
                              )
        else:
            raise ValueError("Theme not found.")
            
    def _init_styles(self):
        """ Initialize widget styles """
        
        theme_styles_path = os.path.join(
                              os.path.dirname(os.path.abspath(__file__)),
                              'data',
                              'themes',
                              self.name,
                              'styles'
                            )
        # Parse widget styles from our JSON (ECMA 262-3) descriptions
        for entry in os.listdir(theme_styles_path):
            if os.path.isfile(os.path.join(theme_styles_path, entry)):
                json_file = os.path.join(theme_styles_path, entry)
                widget, ext = os.path.splitext(entry)
                if ext == '.json':
                    if not widget in self.widget_styles:
                        self.widget_styles[widget] = {}
                    json_file = open(json_file)
                    style_description = simplejson.load(json_file)
                    for state in style_description:
                        props = {}
                        for key in style_description[state]:
                            value = style_description[state][key]
                            if type(value) is list:
                                value = tuple(value)
                            props[str(key)] = value
                            
                        self.widget_styles[widget][state] = Style(**props)
                        
    def get_style(self, widget, state):
        """ Retrieve the style for given widget in given state """
  
        widget = widget.lower()
        widget_styles = self.widget_styles.get(widget)
        if not widget_styles:
            return
        style = widget_styles.get(state)
        if not style:
            return
        
        return style
        
    def get_path(self):
        """ Retrieve theme path """
        
        return self.theme_path
    
    @classmethod
    def get_default(cls):
        """ Retrieve default theme """
        
        global _DEFAULT_THEME
        
        if not _DEFAULT_THEME:
            default_theme_name = 'nublo'
            _DEFAULT_THEME = cls(default_theme_name)
        
        return _DEFAULT_THEME
        
    @staticmethod
    def set_default(theme):
        """ Sets default theme
        
        theme -- (glitter.Theme)
        """
        
        global _DEFAULT_THEME
        
        _DEFAULT_THEME = theme