plugin_results.py 3.89 KB
Newer Older
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
# -*- coding: utf-8 -*-
#
#   plugin_results.py — plugin results table model
#
#   This file is part of debexpo - https://alioth.debian.org/projects/debexpo/
#
#   Copyright © 2012 Clément Schreiner <clement@mux.me>
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

"""
Holds plugin_results table model.
"""

34
35
from sqlalchemy import orm
import sqlalchemy as sa
36
from sqlalchemy.orm.collections import attribute_mapped_collection
37
from sqlalchemy.ext.associationproxy import association_proxy
38
39
from sqlalchemy.ext.declarative import (declared_attr, declarative_base,
                                        has_inherited_table)
40

41
from debexpo.model import meta
42
43
from debexpo.model.package_versions import PackageVersion

44
45
46
from debexpo.lib.utils import uncamel


47
48
class PluginResult(meta.Base):
    __tablename__ = 'plugin_results'
49

50
    # plugin that created the result: must be defined in subclasses
51
52
    plugin = None

Clément Schreiner's avatar
Clément Schreiner committed
53
54
55
56
    #
    # Table columns
    #

57
58
59
60
61
62
    id = sa.Column(sa.types.Integer, primary_key=True)
    entity = sa.Column(sa.types.String(200))
    package_version_id = sa.Column(sa.types.Integer,
                                   sa.ForeignKey('package_versions.id'),
                                   nullable=False)
    package_version = orm.relationship(PackageVersion, backref='plugin_results')
63

Clément Schreiner's avatar
Clément Schreiner committed
64
65
66
67
    _data = association_proxy(
        '_result_data', 'value',
        creator = lambda k, v: PluginResultData(key = k, value = v))

68

Clément Schreiner's avatar
Clément Schreiner committed
69
70
71
    #
    # Polymorphism configuration
    #
72

73
74
75
76
77
    @declared_attr
    def __mapper_args__(cls):
        if not has_inherited_table(cls):
            return {'polymorphic_on': 'entity'}
        else:
78
79
80
81
82
83
            d = {
                'inherits': PluginResult,
                'polymorphic_identity': uncamel(cls.__name__)
                }
            return d

Clément Schreiner's avatar
Clément Schreiner committed
84
85
86
87
88
89

    #
    # Quacking like a dict (but not really)
    #

    # FIXME: inherit MutableMapping or something and provide a complete
90
    # dictionary-like interface
Clément Schreiner's avatar
Clément Schreiner committed
91

92
93
94
    def __getitem__(self, key):
        return self._data[key]

95
96
97
    def get(self, key, default):
        return self._data.get(key, default)

98
99
100
101
102
103
    def __setitem__(self, key, value):
        self._data[key] = value

    def __delitem__(self, key):
        del self._data[key]

104
105
106
    def as_dict(self):
        return self._data

Clément Schreiner's avatar
Clément Schreiner committed
107
    #
Clément Schreiner's avatar
Clément Schreiner committed
108
    # Direct access to some attributes
Clément Schreiner's avatar
Clément Schreiner committed
109
110
    #

111
112
113
114
    @property
    def severity(self):
        return int(self.get('severity', 0))

115
116
class PluginResultData(meta.Base):
    __tablename__ = 'plugin_result_data'
117

118
119
    plugin_result_id = sa.Column(sa.ForeignKey('plugin_results.id'),
                                 primary_key=True)
120

121
122
    key = sa.Column(sa.types.String(200), primary_key=True)
    value = sa.Column(sa.types.Text())
123

124
    plugin_result = orm.relationship(PluginResult,
125
126
127
        backref = orm.backref('_result_data',
            collection_class = attribute_mapped_collection('key'),
            cascade="all, delete-orphan"))