This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration_client.py
168 lines (133 loc) · 5.61 KB
/
integration_client.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
from sqlalchemy.orm.session import Session
from core.model import (
CoverageRecord,
DataSource,
ExternalIntegration,
PresentationCalculationPolicy,
)
from core.coverage import (
CatalogCoverageProvider,
WorkCoverageProvider,
)
from core.metadata_layer import (
Metadata,
)
from core.mirror import MirrorUploader
from coverage_utils import MetadataWranglerReplacementPolicy
class WorkPresentationCoverageProvider(WorkCoverageProvider):
"""A CoverageProvider to reset the presentation for a Work as it
achieves metadata coverage.
It would be good to have this subclass
core.coverage.WorkClassificationCoverageProvider,
but it needs to operate on all works, not just works that have
been made presentation-ready.
"""
SERVICE_NAME = "Work Presentation Coverage Provider"
OPERATION = "recalculate-presentation"
DEFAULT_BATCH_SIZE = 25
_policy = None
def __init__(self, *args, **kwargs):
if not 'registered_only' in kwargs:
kwargs['registered_only'] = True
super(WorkPresentationCoverageProvider, self).__init__(*args, **kwargs)
@property
def policy(self):
# We're going to be aggressive about recalculating the presentation
# for this work because either the work is currently not calculated
# at all, or new metadata has been added that may impact the work, or
# something went wrong trying to calculate it last time.
if not self._policy:
self._policy = PresentationCalculationPolicy(
regenerate_opds_entries=True
)
return self._policy
def process_item(self, work):
work.calculate_presentation(
policy=self.policy, exclude_search=True,
default_fiction=None, default_audience=None,
)
work.set_presentation_ready(exclude_search=True)
return work
class CalculatesWorkPresentation(object):
"""A mixin for IdentifierCoverageProvider (and its subclasses) that
registers a Work to have its presentation calculated or recalculated.
"""
INCALCULABLE_WORK = "500: Work could not be calculated for %r"
def register_work_for_calculation(self, identifier):
"""Registers the given identifier's work for presentation calculation
with the WorkPresentationCoverageProvider.
:return: None, if successful, or CoverageFailure
"""
work = self.get_work(identifier)
if not work:
return self.no_work_found_failure(identifier)
failure = self.update_work_presentation(work, identifier)
if failure:
return failure
def get_work(self, identifier):
"""Finds or calculates a work for a given identifier.
:return: Work or None
"""
work = identifier.work
if work:
return work
# Calculate the work directly from a LicensePool.
license_pools = identifier.licensed_through
if license_pools:
pool = license_pools[0]
work, created = pool.calculate_work(exclude_search=True)
if work:
return work
# A work couldn't be found or created.
return None
def no_work_found_failure(self, identifier):
"""Returns a CoverageFailure in the case that a Work can't be
found or created for an identifier.
"""
return self.failure(identifier, self.INCALCULABLE_WORK % identifier)
def update_work_presentation(self, work, identifier):
"""Register this work to have its presentation calculated
:return: None, if successful, or CoverageFailure
"""
try:
self.presentation_calculation_pre_hook(work)
except Exception as e:
return self.failure(identifier, repr(e), transient=True)
WorkPresentationCoverageProvider.register(work, force=True)
def presentation_calculation_pre_hook(self, work):
"""An optional hook method to prepare the discovered Work for
presentation calculation.
"""
pass
class IntegrationClientCoverImageCoverageProvider(CatalogCoverageProvider,
CalculatesWorkPresentation
):
"""Mirrors and scales cover images we heard about from an IntegrationClient."""
SERVICE_NAME = "Integration Client Coverage Provider"
DATA_SOURCE_NAME = DataSource.INTERNAL_PROCESSING
OPERATION = CoverageRecord.IMPORT_OPERATION
PROTOCOL = ExternalIntegration.OPDS_FOR_DISTRIBUTORS
COVERAGE_COUNTS_FOR_EVERY_COLLECTION = False
def __init__(self, collection, *args, **kwargs):
_db = Session.object_session(collection)
replacement_policy = kwargs.pop('replacement_policy', None)
if not replacement_policy:
replacement_policy = MetadataWranglerReplacementPolicy.from_db(_db)
# Only process identifiers that have been registered for coverage.
kwargs['registered_only'] = kwargs.get('registered_only', True)
super(IntegrationClientCoverImageCoverageProvider, self).__init__(
collection, *args, replacement_policy=replacement_policy, **kwargs
)
@property
def data_source(self):
"""Use the collection's name as the data source name."""
return DataSource.lookup(self._db, self.collection.name, autocreate=True)
def process_item(self, identifier):
edition = self.edition(identifier)
metadata = Metadata.from_edition(edition)
metadata.apply(edition, self.collection,
replace=self.replacement_policy)
failure = self.register_work_for_calculation(identifier)
if failure:
return failure
return identifier