~ubuntu-branches/ubuntu/utopic/lazygal/utopic

« back to all changes in this revision

Viewing changes to lazygal/tpl.py

  • Committer: Package Import Robot
  • Author(s): Michal Čihař, Michal Čihař, Jakub Wilk
  • Date: 2013-06-06 12:05:08 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20130606120508-e3g94vl8w9pw7za7
Tags: 0.8-1
[ Michal Čihař ]
* New upstream release.
  - Uses new Genshi templates (Closes: #696682).
  - Correctly handles wronly encoded artist in EXIF (Closes: #696648).
  - Uses GExiv2 (LP: #1074028).
* Depend on GExiv2 instead of pyexiv2.
* Bump standards to 3.9.4.
* Use debhelper 9.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
 
18
18
import os
 
19
import glob
19
20
from genshi.core import START
20
 
from genshi.template import TemplateLoader, MarkupTemplate, TextTemplate,\
21
 
                            TemplateNotFound
 
21
from genshi.template import TemplateLoader, MarkupTemplate, NewTextTemplate
 
22
from genshi.template import TemplateNotFound
22
23
from genshi.template.eval import UndefinedError
23
24
from genshi.input import XMLParser
24
25
import __init__
26
27
import timeutils
27
28
 
28
29
 
29
 
DEFAULT_TEMPLATE = 'default'
 
30
DEFAULT_THEME = 'default'
 
31
USER_THEME_DIR = os.path.expanduser(os.path.join('~', '.lazygal', 'themes'))
 
32
THEME_SHARED_FILE_PREFIX = 'SHARED_'
30
33
 
31
34
 
32
35
class LazygalTemplate(object):
96
99
                if kind is START:
97
100
                    tag, attrib = data
98
101
                    if tag.namespace == 'http://www.w3.org/2001/XInclude'\
99
 
                    and tag.localname == 'include':
 
102
                            and tag.localname == 'include':
100
103
                        subtpl_ident = attrib.get('href')
101
104
                        try:
102
105
                            subtpl = self.loader.load(subtpl_ident)
117
120
        return subtemplates
118
121
 
119
122
 
120
 
class PlainTemplate(LazygalTemplate, TextTemplate):
 
123
class PlainTemplate(LazygalTemplate):
121
124
 
122
125
    serialization_method = 'text'
123
 
    genshi_tpl_class = TextTemplate
 
126
    genshi_tpl_class = NewTextTemplate
124
127
 
125
128
 
126
129
class TplFactory(object):
127
130
 
128
131
    known_exts = {
129
 
        '.thtml' : XmlTemplate,
130
 
        '.tcss'  : PlainTemplate,
 
132
        '.thtml': XmlTemplate,
 
133
        '.tcss' : PlainTemplate,
 
134
        '.tjs'  : PlainTemplate,
131
135
    }
132
136
 
133
137
    def __init__(self, default_tpl_dir, tpl_dir):
135
139
        # a template variable is defined, or the empty string, thus defined()
136
140
        # will only work for the 'whether it is defined' part of the test.
137
141
        self.loader = TemplateLoader([tpl_dir, default_tpl_dir],
138
 
                                      variable_lookup='lenient')
 
142
                                     variable_lookup='lenient')
139
143
 
140
144
    def is_known_template_type(self, file):
141
145
        filename, ext = os.path.splitext(os.path.basename(file))
151
155
            raise ValueError(_('Unknown template type for %s' % tpl_ident))
152
156
 
153
157
 
 
158
class Theme(object):
 
159
 
 
160
    def __init__(self, themes_dir, name):
 
161
        self.name = name
 
162
 
 
163
        # First try user directory
 
164
        self.tpl_dir = os.path.join(USER_THEME_DIR, self.name)
 
165
        if not os.path.exists(self.tpl_dir):
 
166
            # Fallback to system themes
 
167
            self.tpl_dir = os.path.join(themes_dir, self.name)
 
168
            if not os.path.exists(self.tpl_dir):
 
169
                raise ValueError(_('Theme %s not found') % self.name)
 
170
 
 
171
        self.tpl_loader = TplFactory(os.path.join(themes_dir, DEFAULT_THEME),
 
172
                                     self.tpl_dir)
 
173
 
 
174
        # Load styles templates
 
175
        for style in self.get_avail_styles():
 
176
            style_filename = style['filename']
 
177
            try:
 
178
                self.tpl_loader.load(style_filename)
 
179
            except ValueError:
 
180
                # Not a known emplate ext, ignore
 
181
                pass
 
182
 
 
183
        # find out theme kind
 
184
        try:
 
185
            self.tpl_loader.load('dynindex.thtml')
 
186
        except TemplateNotFound:
 
187
            self.kind = 'static'
 
188
        else:
 
189
            self.kind = 'dynamic'
 
190
 
 
191
    def get_avail_styles(self, default_style=None):
 
192
        style_files_mask = os.path.join(self.tpl_dir,
 
193
                                        THEME_SHARED_FILE_PREFIX + '*' + 'css')
 
194
        styles = []
 
195
        for style_tpl_file in glob.glob(style_files_mask):
 
196
            style = {}
 
197
            tpl_filename = os.path.basename(style_tpl_file).split('.')[0]
 
198
            style['filename'] = tpl_filename[len(THEME_SHARED_FILE_PREFIX):]
 
199
            style['name'] = style['filename'].replace('_', ' ')
 
200
            if default_style is not None:
 
201
                if style['filename'] == default_style:
 
202
                    style['rel'] = 'stylesheet'
 
203
                else:
 
204
                    style['rel'] = 'alternate stylesheet'
 
205
            styles.append(style)
 
206
        return styles
 
207
 
 
208
 
154
209
# vim: ts=4 sw=4 expandtab