-
Notifications
You must be signed in to change notification settings - Fork 2k
/
package_relationship.py
170 lines (146 loc) · 6.21 KB
/
package_relationship.py
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
# encoding: utf-8
import vdm.sqlalchemy
from sqlalchemy import orm, types, Column, Table, ForeignKey
import meta
import core
import package as _package
import types as _types
import domain_object
# i18n only works when this is run as part of pylons,
# which isn't the case for paster commands.
try:
from ckan.common import _
_()
except:
def _(txt):
return txt
__all__ = ['PackageRelationship', 'package_relationship_table',
]
package_relationship_table = Table('package_relationship', meta.metadata,
Column('id', types.UnicodeText, primary_key=True, default=_types.make_uuid),
Column('subject_package_id', types.UnicodeText, ForeignKey('package.id')),
Column('object_package_id', types.UnicodeText, ForeignKey('package.id')),
Column('type', types.UnicodeText),
Column('comment', types.UnicodeText),
Column('state', types.UnicodeText, default=core.State.ACTIVE),
)
class PackageRelationship(core.StatefulObjectMixin,
domain_object.DomainObject):
'''The rule with PackageRelationships is that they are stored in the model
always as the "forward" relationship - i.e. "child_of" but never
as "parent_of". However, the model functions provide the relationships
from both packages in the relationship and the type is swapped from
forward to reverse accordingly, for meaningful display to the user.'''
# List of (type, corresponding_reverse_type)
# e.g. (A "depends_on" B, B has a "dependency_of" A)
# don't forget to add specs to Solr's schema.xml
types = [(u'depends_on', u'dependency_of'),
(u'derives_from', u'has_derivation'),
(u'links_to', u'linked_from'),
(u'child_of', u'parent_of'),
]
types_printable = \
[(_(u'depends on %s'), _(u'is a dependency of %s')),
(_(u'derives from %s'), _(u'has derivation %s')),
(_(u'links to %s'), _(u'is linked from %s')),
(_(u'is a child of %s'), _(u'is a parent of %s')),
]
inferred_types_printable = \
{'sibling':_('has sibling %s')}
def __str__(self):
return '<%sPackageRelationship %s %s %s>' % ("*" if self.active != core.State.ACTIVE else "",
self.subject.name, self.type, self.object.name)
def __repr__(self):
return str(self)
def as_dict(self, package=None, ref_package_by='id'):
"""Returns full relationship info as a dict from the point of view
of the given package if specified.
e.g. {'subject':u'annakarenina',
'type':u'depends_on',
'object':u'warandpeace',
'comment':u'Since 1843'}"""
subject_pkg = self.subject
object_pkg = self.object
relationship_type = self.type
if package and package == object_pkg:
subject_pkg = self.object
object_pkg = self.subject
relationship_type = self.forward_to_reverse_type(self.type)
subject_ref = getattr(subject_pkg, ref_package_by)
object_ref = getattr(object_pkg, ref_package_by)
return {'subject':subject_ref,
'type':relationship_type,
'object':object_ref,
'comment':self.comment}
def as_tuple(self, package):
'''Returns basic relationship info as a tuple from the point of view
of the given package with the object package object.
e.g. rel.as_tuple(warandpeace) gives (u'depends_on', annakarenina)
meaning warandpeace depends_on annakarenina.'''
assert isinstance(package, _package.Package), package
if self.subject == package:
type_str = self.type
other_package = self.object
elif self.object == package:
type_str = self.forward_to_reverse_type(self.type)
other_package = self.subject
else:
# FIXME do we want a more specific error
raise Exception('Package %s is not in this relationship: %s' % \
(package, self))
return (type_str, other_package)
@classmethod
def by_subject(cls, package):
return meta.Session.query(cls).filter(cls.subject_package_id==package.id)
@classmethod
def by_object(cls, package):
return meta.Session.query(cls).filter(cls.object_package_id==package.id)
@classmethod
def get_forward_types(cls):
if not hasattr(cls, 'fwd_types'):
cls.fwd_types = [fwd for fwd, rev in cls.types]
return cls.fwd_types
@classmethod
def get_reverse_types(cls):
if not hasattr(cls, 'rev_types'):
cls.rev_types = [rev for fwd, rev in cls.types]
return cls.rev_types
@classmethod
def get_all_types(cls):
if not hasattr(cls, 'all_types'):
cls.all_types = []
for fwd, rev in cls.types:
cls.all_types.append(fwd)
cls.all_types.append(rev)
return cls.all_types
@classmethod
def reverse_to_forward_type(cls, reverse_type):
for fwd, rev in cls.types:
if rev == reverse_type:
return fwd
@classmethod
def forward_to_reverse_type(cls, forward_type):
for fwd, rev in cls.types:
if fwd == forward_type:
return rev
@classmethod
def reverse_type(cls, forward_or_reverse_type):
for fwd, rev in cls.types:
if fwd == forward_or_reverse_type:
return rev
if rev == forward_or_reverse_type:
return fwd
@classmethod
def make_type_printable(cls, type_):
for i, types in enumerate(cls.types):
for j in range(2):
if type_ == types[j]:
return cls.types_printable[i][j]
raise TypeError(type_)
meta.mapper(PackageRelationship, package_relationship_table, properties={
'subject':orm.relation(_package.Package, primaryjoin=\
package_relationship_table.c.subject_package_id==_package.Package.id,
backref='relationships_as_subject'),
'object':orm.relation(_package.Package, primaryjoin=package_relationship_table.c.object_package_id==_package.Package.id,
backref='relationships_as_object'),
})