1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""A generator which creates a template-based output from the current
20
from bzrlib import errors
21
from bzrlib.lazy_regex import lazy_compile
22
from bzrlib.version_info_formats import (
28
class Template(object):
29
"""A simple template engine.
32
>>> t.add('test', 'xxx')
33
>>> print list(t.process('{test}'))
35
>>> print list(t.process('{test} test'))
37
>>> print list(t.process('test {test}'))
39
>>> print list(t.process('test {test} test'))
40
['test ', 'xxx', ' test']
41
>>> print list(t.process('{test}\\\\n'))
43
>>> print list(t.process('{test}\\n'))
47
_tag_re = lazy_compile('{(\w+)}')
52
def add(self, name, value):
53
self._data[name] = value
55
def process(self, tpl):
56
tpl = tpl.decode('string_escape')
59
match = self._tag_re.search(tpl, pos)
64
start, end = match.span()
70
data = self._data[name]
72
raise errors.MissingTemplateVariable(name)
73
if not isinstance(data, basestring):
78
class CustomVersionInfoBuilder(VersionInfoBuilder):
79
"""Create a version file based on a custom template."""
81
def generate(self, to_file):
82
if self._template is None:
83
raise errors.NoTemplate()
86
info.add('build_date', create_date_str())
87
info.add('branch_nick', self._branch.nick)
89
revision_id = self._get_revision_id()
90
if revision_id is None:
93
info.add('revno', self._branch.revision_id_to_revno(revision_id))
94
info.add('revision_id', revision_id)
95
rev = self._branch.repository.get_revision(revision_id)
96
info.add('date', create_date_str(rev.timestamp, rev.timezone))
99
self._extract_file_revisions()
107
to_file.writelines(info.process(self._template))