/usr/share/pyshared/checkbox/lib/template.py is in checkbox 0.13.7.
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 | #
# This file is part of Checkbox.
#
# Copyright 2008 Canonical Ltd.
#
# Checkbox 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 3 of the License, or
# (at your option) any later version.
#
# Checkbox 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 Checkbox. If not, see <http://www.gnu.org/licenses/>.
#
import re
import logging
EXTENDED_STRING = "_extended"
class Template(object):
def _reader(self, file, size=4096, delimiter=r"\n{2,}"):
buffer_old = ""
while True:
buffer_new = file.read(size)
if not buffer_new:
break
lines = re.split(delimiter, buffer_old + buffer_new)
buffer_old = lines.pop(-1)
for line in lines:
yield line
yield buffer_old
def dump_file(self, elements, file, filename="<stream>"):
for element in elements:
for long_key in element.keys():
if long_key.endswith(EXTENDED_STRING):
short_key = long_key.replace(EXTENDED_STRING, "")
del element[short_key]
for key, value in element.items():
if key.endswith(EXTENDED_STRING):
key = key.replace(EXTENDED_STRING, "")
file.write("%s:\n" % key)
for line in value.split("\n"):
file.write(" %s\n" % line)
elif isinstance(value, (list, tuple)):
file.write("%s:\n" % key)
for v in value:
file.write(" %s\n" % v)
else:
file.write("%s: %s\n" % (key, value))
file.write("\n")
def dump_filename(self, elements, filename):
logging.info("Dumping elements to filename: %s", filename)
file = open(filename, "w")
return self.dump_file(elements, file, filename)
def load_file(self, file, filename="<stream>"):
elements = []
for string in self._reader(file):
if not string:
break
element = {}
def _save(field, value, extended):
extended = extended.rstrip("\n")
if field:
if element.has_key(field):
raise Exception, \
"Template %s has a duplicate field '%s'" \
" with a new value '%s'." \
% (filename, field, value)
element[field] = value
if extended:
element["%s%s" % (field, EXTENDED_STRING)] = extended
string = string.strip("\n")
field = value = extended = ""
for line in string.split("\n"):
line.strip()
if line.startswith("#"):
continue
match = re.search(r"^([-_.A-Za-z0-9@]*):\s?(.*)", line)
if match:
_save(field, value, extended)
field = match.groups()[0].lower()
value = match.groups()[1].rstrip()
extended = ""
continue
if re.search(r"^\s\.$", line):
extended += "\n\n"
continue
match = re.search(r"^\s(\s+.*)", line)
if match:
bit = match.groups()[0].rstrip()
if len(extended) and not re.search(r"[\n ]$", extended):
extended += "\n"
extended += bit + "\n"
continue
match = re.search(r"^\s(.*)", line)
if match:
bit = match.groups()[0].rstrip()
if len(extended) and not re.search(r"[\n ]$", extended):
if extended.endswith("\\"):
extended = extended[:-1].rstrip() + " "
else:
extended += "\n"
extended += bit
continue
raise Exception, "Template %s parse error at: %s" \
% (filename, line)
_save(field, value, extended)
elements.append(element)
return elements
def load_filename(self, filename):
logging.info("Loading elements from filename: %s", filename)
file = open(filename, "r")
return self.load_file(file, filename)
|