This file is indexed.

/usr/lib/python2.7/dist-packages/oslo_reports/models/base.py is in python-oslo.reports 1.7.0-2.

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
# Copyright 2013 Red Hat, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Provides the base report model

This module defines a class representing the basic report
data model from which all data models should inherit (or
at least implement similar functionality).  Data models
store unserialized data generated by generators during
the report serialization process.
"""

import collections as col
import copy

import six


class ReportModel(col.MutableMapping):
    """A Report Data Model

    A report data model contains data generated by some
    generator method or class.  Data may be read or written
    using dictionary-style access, and may be read (but not
    written) using object-member-style access.  Additionally,
    a data model may have an associated view.  This view is
    used to serialize the model when str() is called on the
    model.  An appropriate object for a view is callable with
    a single parameter: the model to be serialized.

    If present, the object passed in as data will be transformed
    into a standard python dict.  For mappings, this is fairly
    straightforward.  For sequences, the indices become keys
    and the items become values.

    :param data: a sequence or mapping of data to associate with the model
    :param attached_view: a view object to attach to this model
    """

    def __init__(self, data=None, attached_view=None):
        self.attached_view = attached_view

        if data is not None:
            if isinstance(data, col.Mapping):
                self.data = dict(data)
            elif isinstance(data, col.Sequence):
                # convert a list [a, b, c] to a dict {0: a, 1: b, 2: c}
                self.data = dict(enumerate(data))
            else:
                raise TypeError('Data for the model must be a sequence '
                                'or mapping.')
        else:
            self.data = {}

    def __str__(self):
        self_cpy = copy.deepcopy(self)
        for key in self_cpy:
            if getattr(self_cpy[key], 'attached_view', None) is not None:
                self_cpy[key] = six.text_type(self_cpy[key])

        if self.attached_view is not None:
            return self.attached_view(self_cpy)
        else:
            raise Exception("Cannot stringify model: no attached view")

    def __repr__(self):
        if self.attached_view is not None:
            return ("<Model {cl.__module__}.{cl.__name__} {dt}"
                    " with view {vw.__module__}."
                    "{vw.__name__}>").format(cl=type(self),
                                             dt=self.data,
                                             vw=type(self.attached_view))
        else:
            return ("<Model {cl.__module__}.{cl.__name__} {dt}"
                    " with no view>").format(cl=type(self),
                                             dt=self.data)

    def __getitem__(self, attrname):
        return self.data[attrname]

    def __setitem__(self, attrname, attrval):
        self.data[attrname] = attrval

    def __delitem__(self, attrname):
        del self.data[attrname]

    def __contains__(self, key):
        return self.data.__contains__(key)

    def __getattr__(self, attrname):
        # Needed for deepcopy in Python3. That will avoid an infinite loop
        # in __getattr__ .
        if 'data' not in self.__dict__:
            self.data = {}

        try:
            return self.data[attrname]
        except KeyError:
            # we don't have that key in data, and the
            # model class doesn't have that attribute
            raise AttributeError(
                "'{cl}' object has no attribute '{an}'".format(
                    cl=type(self).__name__, an=attrname
                )
            )

    def __len__(self):
        return len(self.data)

    def __iter__(self):
        return self.data.__iter__()

    def set_current_view_type(self, tp, visited=None):
        """Set the current view type

        This method attempts to set the current view
        type for this model and all submodels by calling
        itself recursively on all values, traversing
        intervening sequences and mappings when possible,
        and ignoring all other objects.

        :param tp: the type of the view ('text', 'json', 'xml', etc)
        :param visited: a set of object ids for which the corresponding objects
                        have already had their view type set
        """

        if visited is None:
            visited = set()

        def traverse_obj(obj):
            oid = id(obj)

            # don't die on recursive structures,
            # and don't treat strings like sequences
            if oid in visited or isinstance(obj, six.string_types):
                return

            visited.add(oid)

            if hasattr(obj, 'set_current_view_type'):
                obj.set_current_view_type(tp, visited=visited)

            if isinstance(obj, col.Sequence):
                for item in obj:
                    traverse_obj(item)

            elif isinstance(obj, col.Mapping):
                for val in six.itervalues(obj):
                    traverse_obj(val)

        traverse_obj(self)