This file is indexed.

/usr/share/pyshared/lazr/lifecycle/snapshot.py is in python-lazr.lifecycle 1.0-0ubuntu2.

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
# Copyright 2009 Canonical Ltd.  All rights reserved.
#
# This file is part of lazr.lifecycle
#
# lazr.lifecycle is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# lazr.lifecycle 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 Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with lazr.lifecycle.  If not, see <http://www.gnu.org/licenses/>.

"""Provides object snapshotting functionality.

This is particularly useful in calculating deltas.
"""

__metaclass__ = type

__all__ = ['SnapshotCreationError',
           'Snapshot',
           ]

from zope.component import queryAdapter
from zope.interface.interfaces import IInterface
from zope.interface import directlyProvides
from zope.schema.interfaces import IField
from zope.security.proxy import removeSecurityProxy

from lazr.lifecycle.interfaces import ISnapshotValueFactory

_marker = object()


class SnapshotCreationError(Exception):
    """Something went wrong while creating a snapshot."""


class Snapshot:
    """Provides a simple snapshot of the given object.

    The snapshot will have the attributes listed in names. It
    will also provide the interfaces listed in providing. If no names
    are supplied but an interface is provided, all Fields of that
    interface will be included in the snapshot.

    The attributes are copied by passing them through a ISnapshotValueFactory.
    The default implementation of that adapter just returns the value itself.
    """
    def __init__(self, ob, names=None, providing=None):
        ob = removeSecurityProxy(ob)

        if names is None and providing is None:
            raise SnapshotCreationError(
                "You have to specify either 'names' or 'providing'.")

        if IInterface.providedBy(providing):
            providing = [providing]

        if names is None:
            names = set()
            for iface in providing:
                for name in iface.names(all=True):
                    field = iface[name]
                    if IField.providedBy(field):
                        names.add(name)

        for name in names:
            value = getattr(ob, name, _marker)
            if value is _marker:
                raise AssertionError("Attribute %s not in object %r"
                                     % (name, ob))
            snapshot_value = queryAdapter(
                value, ISnapshotValueFactory, default=_marker)
            if snapshot_value is _marker:
                snapshot_value = value
            setattr(self, name, snapshot_value)

        if providing is not None:
            directlyProvides(self, providing)