This file is indexed.

/usr/lib/python2.7/dist-packages/lazygal/tpl.py is in lazygal 0.8.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Lazygal, a lazy static web gallery generator.
# Copyright (C) 2007-2012 Alexandre Rossi <alexandre.rossi@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os
import glob
import logging
import json
import codecs
import locale

from genshi.core import START
from genshi.template import TemplateLoader, MarkupTemplate, NewTextTemplate
from genshi.template import TemplateNotFound
from genshi.template.eval import UndefinedError
from genshi.input import XMLParser
import __init__

import timeutils
import pathutils


DEFAULT_THEME = 'default'
USER_THEME_DIR = os.path.expanduser(os.path.join('~', '.lazygal', 'themes'))
THEME_SHARED_FILE_PREFIX = 'SHARED_'
THEME_MANIFEST = 'manifest.json'


class LazygalTemplate(object):

    def __init__(self, loader, genshi_tpl):
        self.loader = loader
        self.path = genshi_tpl.filepath
        self.genshi_tpl = genshi_tpl

    def __complement_values(self, values):
        values['gen_datetime'] = timeutils.Datetime()
        values['gen_date'] = values['gen_datetime'].strftime('%c')
        values['lazygal_version'] = __init__.__version__
        return values

    def __generate(self, values):
        # The cryptic values['_'] = _ is the way to pass the gettext
        # translation function to the templates : the _() callable is assigned
        # to the '_' keyword arg.
        values['_'] = _
        return self.genshi_tpl.generate(**values)

    def instanciate(self, values):
        self.__complement_values(values)
        # encoding=None gives us a unicode string instead of an utf-8 encoded
        # string. This is because we are not out of lazygal yet.
        return self.__generate(values).render(self.serialization_method,
                                              encoding=None)

    def dump(self, values, dest):
        self.__complement_values(values)

        page = open(dest, 'w')
        try:
            self.__generate(values).render(method=self.serialization_method,
                                           out=page, encoding='utf-8')
        except UnicodeDecodeError:
            problematic_vars = []
            for key, value in values.items():
                if type(value) is not unicode:
                    try:
                        str(value).decode('utf-8')
                    except UnicodeDecodeError:
                        problematic_vars.append(key)
            print 'Problematic template vars : %s' % ', '.join(problematic_vars)
            raise
        except UndefinedError, e:
            print 'W: %s' % e
            raise
        finally:
            page.close()


class XmlTemplate(LazygalTemplate):

    serialization_method = 'xhtml'
    genshi_tpl_class = MarkupTemplate

    def subtemplates(self, tpl=None):
        if tpl is None:
            tpl = self

        subtemplates = []
        f = open(tpl.path, 'r')
        try:
            for kind, data, pos in XMLParser(f, filename=tpl.path):
                if kind is START:
                    tag, attrib = data
                    if tag.namespace == 'http://www.w3.org/2001/XInclude'\
                            and tag.localname == 'include':
                        subtpl_ident = attrib.get('href')
                        try:
                            subtpl = self.loader.load(subtpl_ident)
                        except TemplateNotFound:
                            # This will fail later, here we just need to ignore
                            # template idents that are dynamically computed.
                            pass
                        else:
                            subtemplates.append(subtpl)
        finally:
            f.close()

        for subtemplate in subtemplates:
            for new_subtpl in self.subtemplates(subtemplate):
                if new_subtpl not in subtemplates:
                    subtemplates.append(new_subtpl)

        return subtemplates


class PlainTemplate(LazygalTemplate):

    serialization_method = 'text'
    genshi_tpl_class = NewTextTemplate


class TplFactory(object):

    known_exts = {
        '.thtml': XmlTemplate,
        '.tcss' : PlainTemplate,
        '.tjs'  : PlainTemplate,
    }

    def __init__(self, default_tpl_dir, tpl_dir):
        # We use lenient mode here because we want an easy way to check whether
        # a template variable is defined, or the empty string, thus defined()
        # will only work for the 'whether it is defined' part of the test.
        self.loader = TemplateLoader([tpl_dir, default_tpl_dir],
                                     variable_lookup='lenient')

    def is_known_template_type(self, file):
        filename, ext = os.path.splitext(os.path.basename(file))
        return ext in self.known_exts.keys()

    def load(self, tpl_ident):
        if self.is_known_template_type(tpl_ident):
            filename, ext = os.path.splitext(os.path.basename(tpl_ident))
            tpl_class = self.known_exts[ext]
            tpl = self.loader.load(tpl_ident, cls=tpl_class.genshi_tpl_class)
            return tpl_class(self, tpl)
        else:
            raise ValueError(_('Unknown template type for %s' % tpl_ident))


class Theme(object):

    def __init__(self, themes_dir, name):
        self.name = name

        # First try user directory
        self.tpl_dir = os.path.join(USER_THEME_DIR, self.name)
        if not os.path.exists(self.tpl_dir):
            # Fallback to system themes
            self.tpl_dir = os.path.join(themes_dir, self.name)
            if not os.path.exists(self.tpl_dir):
                raise ValueError(_('Theme %s not found') % self.name)
        self.tpl_dir = os.path.abspath(self.tpl_dir)

        self.__load_manifest()

        self.tpl_loader = TplFactory(os.path.join(themes_dir, DEFAULT_THEME),
                                     self.tpl_dir)

        # Load styles templates
        for style in self.get_avail_styles():
            style_filename = style['filename']
            try:
                self.tpl_loader.load(style_filename)
            except ValueError:
                # Not a known emplate ext, ignore
                pass

        # find out theme kind
        try:
            self.tpl_loader.load('dynindex.thtml')
        except TemplateNotFound:
            self.kind = 'static'
        else:
            self.kind = 'dynamic'

    def __load_manifest(self):
        theme_manifest_path = os.path.join(self.tpl_dir, THEME_MANIFEST)
        logging.debug(_('Loading %s for theme %s')
                      % (THEME_MANIFEST, self.name))
        theme_manifest = {}
        try:
            with codecs.open(theme_manifest_path, 'r',
                             locale.getpreferredencoding()) as f:
                theme_manifest.update(json.load(f))
        except IOError:
            logging.debug(_('Theme %s does not have a %s')
                          % (self.name, THEME_MANIFEST))
        except ValueError:
            logging.error(_('Theme %s : %s parsing error')
                          % (self.name, THEME_MANIFEST))
            raise

        if 'shared' not in theme_manifest:
            theme_manifest['shared'] = []
        theme_manifest['shared'].append({'path': THEME_SHARED_FILE_PREFIX + '*'})
        self.shared_files = []
        for shared_file_entry in theme_manifest['shared']:
            entry_path = os.path.join(self.tpl_dir, shared_file_entry['path'])
            entry_path = os.path.normpath(entry_path)
            entry_dest = 'dest' in shared_file_entry and shared_file_entry['dest']
            for sf in glob.glob(entry_path):
                sf_name = os.path.basename(sf)
                if sf_name.startswith(THEME_SHARED_FILE_PREFIX):
                    sf_name = sf_name[len(THEME_SHARED_FILE_PREFIX):]

                if entry_dest:
                    # There is dest info in manifest for this file
                    if entry_dest[-1] == os.sep: # dest is a dir
                        dest = os.path.join(entry_dest, sf_name)
                    else:
                        dest = entry_dest
                else:
                    if pathutils.is_subdir_of(self.tpl_dir, sf):
                        sf_dir = os.path.dirname(sf)
                        dest = os.path.join(sf_dir[len(self.tpl_dir)+1:], sf_name)
                    else:
                        dest = sf_name

                self.shared_files.append((sf, dest))

    def get_avail_styles(self, default_style=None):
        style_files_mask = os.path.join(self.tpl_dir,
                                        THEME_SHARED_FILE_PREFIX + '*' + 'css')
        styles = []
        for style_tpl_file in glob.glob(style_files_mask):
            style = {}
            tpl_filename = os.path.basename(style_tpl_file).split('.')[0]
            style['filename'] = tpl_filename[len(THEME_SHARED_FILE_PREFIX):]
            style['name'] = style['filename'].replace('_', ' ')
            if default_style is not None:
                if style['filename'] == default_style:
                    style['rel'] = 'stylesheet'
                else:
                    style['rel'] = 'alternate stylesheet'
            styles.append(style)
        return styles


# vim: ts=4 sw=4 expandtab