/usr/lib/python3/dist-packages/plainbox/impl/unit/exporter.py is in python3-plainbox 0.25-1.
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 | # This file is part of Checkbox.
#
# Copyright 2015 Canonical Ltd.
# Written by:
# Sylvain Pineau <sylvain.pineau@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# 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/>.
"""Exporter Entry Unit."""
import json
import logging
import os.path
import re
import pkg_resources
from plainbox.i18n import gettext as _
from plainbox.impl.symbol import SymbolDef
from plainbox.impl.unit.unit_with_id import UnitWithId
from plainbox.impl.unit.validators import CorrectFieldValueValidator
from plainbox.impl.unit.validators import PresentFieldValidator
from plainbox.impl.unit.validators import TranslatableFieldValidator
from plainbox.impl.unit.validators import UntranslatableFieldValidator
from plainbox.impl.validation import Problem
from plainbox.impl.validation import Severity
logger = logging.getLogger("plainbox.unit.exporter")
__all__ = ('ExporterUnit', )
class ExporterUnit(UnitWithId):
"""
Unit representing a session exporter.
This unit is used to define mechanisms for exporting session state data
into any format.
"""
def __str__(self):
return self.summary
def __repr__(self):
return "<ExporterUnit id:{!r} entry_point:{!r}>".format(
self.id, self.entry_point)
@property
def support(self):
if not self.check():
return ExporterUnitSupport(self)
else:
return None
@property
def summary(self):
"""
Summary of this exporter.
.. note::
This value is not translated, see :meth:`tr_summary()` for
a translated equivalent.
"""
return self.get_record_value('summary', '')
def tr_summary(self):
"""Get the translated version of :meth:`summary`."""
return self.get_translated_record_value('summary', '')
@property
def entry_point(self):
"""Exporter EntryPoint to call."""
return self.get_record_value('entry_point')
@property
def file_extension(self):
"""Filename extension when the exporter stream is saved to a file."""
return self.get_record_value('file_extension')
@property
def options(self):
"""Configuration options to send to the exporter class."""
return self.get_record_value('options')
@property
def data(self):
"""Data to send to the exporter class."""
return self.get_record_value('data')
class Meta:
name = 'exporter'
class fields(SymbolDef):
"""Symbols for each field that an Exporter can have."""
summary = 'summary'
entry_point = 'entry_point'
file_extension = 'file_extension'
options = 'options'
data = 'data'
field_validators = {
fields.summary: [
PresentFieldValidator(severity=Severity.advice),
TranslatableFieldValidator,
# We want the summary to be a single line
CorrectFieldValueValidator(
lambda summary: summary.count("\n") == 0,
Problem.wrong, Severity.warning,
message=_("please use only one line"),
onlyif=lambda unit: unit.summary is not None),
# We want the summary to be relatively short
CorrectFieldValueValidator(
lambda summary: len(summary) <= 80,
Problem.wrong, Severity.warning,
message=_("please stay under 80 characters"),
onlyif=lambda unit: unit.summary is not None),
],
fields.entry_point: [
PresentFieldValidator,
UntranslatableFieldValidator,
CorrectFieldValueValidator(
lambda entry_point: pkg_resources.load_entry_point(
'plainbox', 'plainbox.exporter', entry_point),
Problem.wrong, Severity.error),
],
fields.file_extension: [
PresentFieldValidator,
UntranslatableFieldValidator,
CorrectFieldValueValidator(
lambda extension: re.search("^[\w\.\-]+$", extension),
Problem.syntax_error, Severity.error),
],
fields.options: [
UntranslatableFieldValidator,
],
fields.data: [
UntranslatableFieldValidator,
CorrectFieldValueValidator(
lambda value, unit: json.loads(value),
Problem.syntax_error, Severity.error,
onlyif=lambda unit: unit.data),
CorrectFieldValueValidator(
lambda value, unit: os.path.isfile(os.path.join(
unit.provider.data_dir,
json.loads(value)['template'])),
Problem.wrong, Severity.error,
message=_("Jinja2 template not found"),
onlyif=lambda unit: unit.entry_point == 'jinja2'),
],
}
class ExporterUnitSupport():
"""
Helper class that distills exporter data into more usable form.
This class serves to offload some of the code from :class:`ExporterUnit`
branch. It takes a single exporter unit and extracts all the interesting
information out of it. Subsequently it exposes that data so that some
methods on the exporter unit class itself can be implemented in an easier
way.
"""
def __init__(self, exporter):
self._data = self._get_data(exporter)
self._data_dir = exporter.provider.data_dir
self.exporter_cls = self._get_exporter_cls(exporter)
self._option_list = self._get_option_list(exporter)
self.file_extension = exporter.file_extension
self.summary = exporter.tr_summary()
if exporter.entry_point == 'jinja2':
self._template = self._data['template']
@property
def data(self):
return self._data
@property
def data_dir(self):
return self._data_dir
@property
def option_list(self):
return self._option_list
@property
def template(self):
return self._template
def _get_data(self, exporter):
"""Data to send to the exporter class."""
if exporter.data:
return json.loads(exporter.data)
else:
return {}
def _get_option_list(self, exporter):
"""Option list to send to the exporter class."""
if exporter.options:
return re.split(r'[;,\s]+', exporter.options)
else:
return []
def _get_exporter_cls(self, exporter):
"""Return the exporter class."""
return pkg_resources.load_entry_point(
'plainbox', 'plainbox.exporter', exporter.entry_point)
|