77
77
self.frequency = frequency
79
79
def __repr__(self):
80
return "%s: [%s]" % (util.obj_name(self), self.list_types())
80
return "%s: [%s]" % (type_utils.obj_name(self), self.list_types())
82
82
@abc.abstractmethod
83
83
def list_types(self):
84
84
raise NotImplementedError()
86
def handle_part(self, data, ctype, filename, payload, frequency):
87
return self._handle_part(data, ctype, filename, payload, frequency)
89
86
@abc.abstractmethod
90
def _handle_part(self, data, ctype, filename, payload, frequency):
87
def handle_part(self, *args, **kwargs):
91
88
raise NotImplementedError()
94
def run_part(mod, data, ctype, filename, payload, frequency):
91
def run_part(mod, data, filename, payload, frequency, headers):
95
92
mod_freq = mod.frequency
96
93
if not (mod_freq == PER_ALWAYS or
97
94
(frequency == PER_INSTANCE and mod_freq == PER_INSTANCE)):
99
mod_ver = mod.handler_version
100
96
# Sanity checks on version (should be an int convertable)
98
mod_ver = mod.handler_version
102
99
mod_ver = int(mod_ver)
100
except (TypeError, ValueError, AttributeError):
102
content_type = headers['Content-Type']
106
104
LOG.debug("Calling handler %s (%s, %s, %s) with frequency %s",
107
mod, ctype, filename, mod_ver, frequency)
105
mod, content_type, filename, mod_ver, frequency)
107
# Treat as v. 3 which does get a frequency + headers
108
mod.handle_part(data, content_type, filename,
109
payload, frequency, headers)
109
111
# Treat as v. 2 which does get a frequency
110
mod.handle_part(data, ctype, filename, payload, frequency)
112
mod.handle_part(data, content_type, filename,
112
115
# Treat as v. 1 which gets no frequency
113
mod.handle_part(data, ctype, filename, payload)
116
mod.handle_part(data, content_type, filename, payload)
118
raise ValueError("Unknown module version %s" % (mod_ver))
115
120
util.logexc(LOG, ("Failed calling handler %s (%s, %s, %s)"
116
121
" with frequency %s"),
117
mod, ctype, filename,
122
mod, content_type, filename,
118
123
mod_ver, frequency)
121
126
def call_begin(mod, data, frequency):
122
run_part(mod, data, CONTENT_START, None, None, frequency)
127
# Create a fake header set
129
'Content-Type': CONTENT_START,
131
run_part(mod, data, None, None, frequency, headers)
125
134
def call_end(mod, data, frequency):
126
run_part(mod, data, CONTENT_END, None, None, frequency)
135
# Create a fake header set
137
'Content-Type': CONTENT_END,
139
run_part(mod, data, None, None, frequency, headers)
129
142
def walker_handle_handler(pdata, _ctype, _filename, payload):
176
def walker_callback(pdata, ctype, filename, payload):
177
if ctype in PART_CONTENT_TYPES:
178
walker_handle_handler(pdata, ctype, filename, payload)
189
def walker_callback(data, filename, payload, headers):
190
content_type = headers['Content-Type']
191
if content_type in PART_CONTENT_TYPES:
192
walker_handle_handler(data, content_type, filename, payload)
180
handlers = pdata['handlers']
181
if ctype in pdata['handlers']:
182
run_part(handlers[ctype], pdata['data'], ctype, filename,
183
payload, pdata['frequency'])
194
handlers = data['handlers']
195
if content_type in handlers:
196
run_part(handlers[content_type], data['data'], filename,
197
payload, data['frequency'], headers)
185
199
# Extract the first line or 24 bytes for displaying in the log
186
200
start = _extract_first_or_bytes(payload, 24)
187
201
details = "'%s...'" % (_escape_string(start))
188
if ctype == NOT_MULTIPART_TYPE:
202
if content_type == NOT_MULTIPART_TYPE:
189
203
LOG.warning("Unhandled non-multipart (%s) userdata: %s",
204
content_type, details)
192
206
LOG.warning("Unhandled unknown content-type (%s) userdata: %s",
207
content_type, details)
195
LOG.debug("empty payload of type %s" % ctype)
209
LOG.debug("Empty payload of type %s", content_type)
198
212
# Callback is a function that will be called with