/usr/lib/python3/dist-packages/plainbox/impl/secure/origin.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 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | # This file is part of Checkbox.
#
# Copyright 2012-2014 Canonical Ltd.
# Written by:
# Zygmunt Krynicki <zygmunt.krynicki@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/>.
"""
:mod:`plainbox.impl.secure.origin` -- origin objects
====================================================
"""
import functools
import inspect
import os
from plainbox.abc import ITextSource
from plainbox.i18n import gettext as _
from plainbox.impl.symbol import SymbolDef
class OriginMode(SymbolDef):
"""
A symbol definition (which will become an enumeration in the near future)
that describes all the possible "modes" an :class:`Origin` can operate in.
"""
# NOTE: this should be an enumeration
whole_file = 'whole-file'
single_line = 'single-line'
line_range = 'line-range'
@functools.total_ordering
class Origin:
"""
Simple class for tracking where something came from
This class supports "pinpointing" something in a block of text. The block
is described by the source attribute. The actual range is described by
line_start (inclusive) and line_end (exclusive).
:ivar source:
Something that describes where the text came frome. Technically it
should implement the :class:`~plainbox.abc.ITextSource` interface.
:ivar line_start:
The number of the line where the record begins. This can be None
when the intent is to cover the whole file. This can also be equal
to line_end (when not None) if the intent is to show a single line.
:ivar line_end:
The number of the line where the record ends
"""
__slots__ = ['source', 'line_start', 'line_end']
def __init__(self, source, line_start=None, line_end=None):
self.source = source
self.line_start = line_start
self.line_end = line_end
def mode(self):
"""
Compute the "mode" of this origin instance.
:returns:
:attr:`OriginMode.whole_file`, :attr:`OriginMode.single_line`
or :attr:`OriginMode.line_range`.
The mode tells if this instance is describing the whole file,
a range of lines or just a single line. It is mostly used internally
by the implementation.
"""
if self.line_start is None and self.line_end is None:
return OriginMode.whole_file
elif self.line_start == self.line_end:
return OriginMode.single_line
else:
return OriginMode.line_range
def __repr__(self):
return "<{} source:{!r} line_start:{} line_end:{}>".format(
self.__class__.__name__,
self.source, self.line_start, self.line_end)
def __str__(self):
mode = self.mode()
if mode is OriginMode.whole_file:
return str(self.source)
elif mode is OriginMode.single_line:
return "{}:{}".format(self.source, self.line_start)
elif mode is OriginMode.line_range:
return "{}:{}-{}".format(
self.source, self.line_start, self.line_end)
else:
raise NotImplementedError
def relative_to(self, base_dir):
"""
Create a Origin with source relative to the specified base directory.
:param base_dir:
A base directory name
:returns:
A new Origin with source replaced by the result of calling
relative_to(base_dir) on the current source *iff* the current
source has that method, self otherwise.
This method is useful for obtaining user friendly Origin objects that
have short, understandable filenames.
"""
relative_source = self.source.relative_to(base_dir)
if relative_source is not self.source:
return Origin(relative_source, self.line_start, self.line_end)
else:
return self
def with_offset(self, offset):
"""
Create a new Origin by adding a offset of a specific number of lines
:param offset:
Number of lines to add (or substract)
:returns:
A new Origin object
"""
mode = self.mode()
if mode is OriginMode.whole_file:
return self
elif mode is OriginMode.single_line or mode is OriginMode.line_range:
return Origin(self.source,
self.line_start + offset, self.line_end + offset)
else:
raise NotImplementedError
def just_line(self):
"""
Create a new Origin that points to the start line
:returns:
A new Origin with the end_line equal to start_line.
This effectively makes the origin describe a single line.
"""
return Origin(self.source, self.line_start, self.line_start)
def just_file(self):
"""
create a new Origin that points to the whole file
:returns:
A new Origin with line_end and line_start both set to None.
"""
return Origin(self.source)
def __eq__(self, other):
if isinstance(other, Origin):
return ((self.source, self.line_start, self.line_end) ==
(other.source, other.line_start, other.line_end))
else:
return NotImplemented
def __gt__(self, other):
if isinstance(other, Origin):
return ((self.source, self.line_start, self.line_end) >
(other.source, other.line_start, other.line_end))
else:
return NotImplemented
@classmethod
def get_caller_origin(cls, back=0):
"""
Create an Origin instance pointing at the call site of this method.
"""
# Create an Origin instance that pinpoints the place that called
# get_caller_origin().
caller_frame, filename, lineno = inspect.stack(0)[2 + back][:3]
try:
source = PythonFileTextSource(filename)
origin = Origin(source, lineno, lineno)
finally:
# Explicitly delete the frame object, this breaks the
# reference cycle and makes this part of the code deterministic
# with regards to the CPython garbage collector.
#
# As recommended by the python documentation:
# http://docs.python.org/3/library/inspect.html#the-interpreter-stack
del caller_frame
return origin
@functools.total_ordering
class UnknownTextSource(ITextSource):
"""
A :class:`ITextSource` subclass indicating that the source of text is
unknown.
This instances of this class are constructed by gen_rfc822_records() when
no explicit source is provided and the stream has no name. The serve as
non-None values to prevent constructing :class:`PythonFileTextSource` with
origin computed from :meth:`Origin.get_caller_origin()`
"""
def __str__(self):
return _("???")
def __repr__(self):
return "{}()".format(self.__class__.__name__)
def __eq__(self, other):
if isinstance(other, UnknownTextSource):
return True
else:
return False
def __gt__(self, other):
if isinstance(other, UnknownTextSource):
return False
else:
return NotImplemented
def relative_to(self, path):
return self
@functools.total_ordering
class FileTextSource(ITextSource):
"""
A :class:`ITextSource` subclass indicating that text came from a file.
:ivar filename:
name of the file something comes from
"""
def __init__(self, filename):
self.filename = filename
def __str__(self):
return self.filename
def __repr__(self):
return "{}({!r})".format(
self.__class__.__name__, self.filename)
def __eq__(self, other):
if isinstance(other, FileTextSource):
return self.filename == other.filename
else:
return False
def __gt__(self, other):
if isinstance(other, FileTextSource):
return self.filename > other.filename
else:
return NotImplemented
def relative_to(self, base_dir):
"""
Compute a FileTextSource with the filename being a relative path from
the specified base directory.
:param base_dir:
A base directory name
:returns:
A new FileTextSource with filename relative to that base_dir
"""
return self.__class__(os.path.relpath(self.filename, base_dir))
class PythonFileTextSource(FileTextSource):
"""
A :class:`FileTextSource` subclass indicating the file was a python file.
It implements no differences but in some context it might be helpful to
differentiate on the type of the source field in the origin of a job
definition record.
:ivar filename:
name of the python filename that something comes from
"""
@functools.total_ordering
class JobOutputTextSource(ITextSource):
"""
A :class:`ITextSource` subclass indicating that text came from job output.
This class is used by
:meth:`SessionState._gen_rfc822_records_from_io_log()` to allow such
(generated) jobs to be traced back to the job that generated them.
:ivar job:
:class:`plainbox.impl.job.JobDefinition` instance that generated the
text
"""
def __init__(self, job):
self.job = job
def __str__(self):
return str(self.job.id)
def __repr__(self):
return "<{} job:{!r}>".format(self.__class__.__name__, self.job)
def __eq__(self, other):
if isinstance(other, JobOutputTextSource):
return self.job == other.job
return NotImplemented
def __gt__(self, other):
if isinstance(other, JobOutputTextSource):
return self.job > other.job
return NotImplemented
def relative_to(self, base_path):
return self
@functools.total_ordering
class CommandLineTextSource(ITextSource):
"""
A :class:`ITextSource` describing text that originated arguments to main()
:attr arg_name:
The optional name of the argument that describes the arg_value
:attr arg_value:
The argument that was passed on command line (the actual text)
"""
def __init__(self, arg_name, arg_value):
self.arg_value = arg_value
self.arg_name = arg_name
def __str__(self):
if self.arg_name is not None:
return _("command line argument {}={!a}").format(
self.arg_name, self.arg_value)
else:
return _("command line argument {!a}").format(self.arg_value)
def __repr__(self):
return "<{} arg_name:{!r} arg_value:{!r}>".format(
self.__class__.__name__, self.arg_name, self.arg_value)
def __eq__(self, other):
if isinstance(other, CommandLineTextSource):
return (self.arg_name == other.arg_name
and self.arg_value == other.arg_value)
return NotImplemented
def __gt__(self, other):
if isinstance(other, CommandLineTextSource):
if self.arg_name > other.arg_name:
return True
if self.arg_value > other.arg_value:
return True
return False
return NotImplemented
def relative_to(self, base_path):
return self
|