-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathsnippets.py
399 lines (332 loc) · 14.5 KB
/
snippets.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding: utf-8 -*-
import concurrent
import contextlib
import inspect
import logging
import re
import sys
import uuid
from concurrent.futures import ProcessPoolExecutor
from lxml import etree, html
from psycopg2 import sql
from psycopg2.extensions import quote_ident
from psycopg2.extras import Json
with contextlib.suppress(ImportError):
from odoo.sql_db import db_connect
from .const import NEARLYWARN
from .exceptions import MigrationError
from .helpers import table_of_model
from .misc import import_script, log_progress
from .pg import column_exists, column_type, get_max_workers, table_exists
_logger = logging.getLogger(__name__)
utf8_parser = html.HTMLParser(encoding="utf-8")
class Snippet:
def __init__(self, name, tag="*", klass="", selector=""):
self.name = name
self.tag = tag
self.klass = klass or name
self.selector = selector or f'//{tag}[hasclass("{self.klass}")]'
def add_snippet_names(cr, table, column, snippets, select_query):
"""
Execute the select_query then for each snippet contained in arch add the right data-snippet attribute on the right element.
:param str table: The table we are working on
:param str column: The column we are working on
:param list snippets: list of all snippets to migrate
:param str select_query: a query that when executed will return (id, list of snippets contained in the arch, arch)
"""
_logger.info("Add snippet names on %s.%s", table, column)
cr.execute(select_query)
it = log_progress(cr.fetchall(), _logger, qualifier="rows", size=cr.rowcount, log_hundred_percent=True)
def quote(ident):
return quote_ident(ident, cr._cnx)
for res_id, regex_matches, arch in it:
regex_matches = [match[0] for match in regex_matches] # noqa: PLW2901
arch = arch.replace("\r", "") # otherwise html parser below will transform \r -> # noqa: PLW2901
body = html.fromstring(arch, parser=utf8_parser)
changed = False
for snippet in snippets:
if snippet.klass in regex_matches:
body_snippets = body.xpath(snippet.selector)
for body_snippet in body_snippets:
body_snippet.attrib["data-snippet"] = snippet.name
changed = True
if changed:
body = etree.tostring(body, encoding="unicode")
cr.execute(f"UPDATE {quote(table)} SET {quote(column)} = %s WHERE id = %s", [body, res_id])
def add_snippet_names_on_html_field(cr, table, column, snippets, regex):
"""Search for all the snippets in the fields mentionned (should be html fields) and add the corresponding data-snippet on them."""
query = cr.mogrify(
sql.SQL(
"""
SELECT id, array((SELECT regexp_matches({column}, %(regex)s, 'g'))), {column}
FROM {table}
WHERE {column} ~ %(regex)s
"""
).format(column=sql.Identifier(column), table=sql.Identifier(table)),
{"regex": regex},
).decode()
where = cr.mogrify(sql.SQL("{column} ~ %s").format(column=sql.Identifier(column)), [regex]).decode()
ids_ranges = determine_chunk_limit_ids(cr, table, [column], where)
for id0, id1 in ids_ranges:
add_snippet_names(cr, table, column, snippets, query + f" AND id BETWEEN {id0} AND {id1}")
def get_regex_from_snippets_list(snippets):
return "(%s)" % "|".join(snippet.klass for snippet in snippets)
def get_html_fields(cr):
# yield (table, column) of stored html fields (that needs snippets updates)
for table, columns in html_fields(cr):
for column in columns:
yield table, quote_ident(column, cr._cnx)
def html_fields(cr):
cr.execute(
"""
SELECT f.model, array_agg(f.name)
FROM ir_model_fields f
JOIN ir_model m ON m.id = f.model_id
WHERE f.ttype = 'html'
AND f.store = true
AND m.transient = false
AND f.model NOT LIKE 'ir.actions%'
AND f.model != 'mail.message'
GROUP BY f.model
"""
)
for model, columns in cr.fetchall():
table = table_of_model(cr, model)
if not table_exists(cr, table):
# an SQL VIEW
continue
existing_columns = [column for column in columns if column_exists(cr, table, column)]
if existing_columns:
yield table, existing_columns
def parse_style(attr):
"""
Convert an HTML style attribute's text into a dict mapping property names to property values.
:param str attr: value of an HTML style attribute
:return: dict of CSS property values per property name
"""
# Captures two groups:
# - identifier: sequence of word character or hyphen that is followed by a colon
# - value: sequence of:
# - any non semicolon character or
# - sequence of any non single quote character or escaped single quote
# surrounded by single quotes or
# - sequence of any non double quote character or escaped double quote
# surrounded by double quotes
regex = r"""
([\w\-]+)\s*:\s*((?:[^;\"']|'(?:[^']|(?:\\'))*'|\"(?:[^\"]|(?:\\\"))*\")+)
""".strip()
return dict(re.findall(regex, attr))
def format_style(styles):
"""
Convert a dict of CSS property names to property values into an HTML style attribute string.
:param dict styles: CSS property value per property name
:return: str HTML style attribute
"""
style = "; ".join(["%s: %s" % entry for entry in styles.items()])
if len(style) > 0 and style[-1] != ";":
style += ";"
return style
def html_converter(transform_callback, selector=None):
"""
Create an upgrade converter for a single HTML text content or for HTML elements that match a selector.
:param func transform_callback: transforms an HTML tree and returns True if
a change happened
:param str selector: targets the elements to loop on
:return: object HTMLConverter with callback
"""
return HTMLConverter(make_pickleable_callback(transform_callback), selector)
def make_pickleable_callback(callback):
"""
Make a callable importable.
`ProcessPoolExecutor.map` arguments needs to be pickleable
Functions can only be pickled if they are importable.
However, the callback's file is not importable due to the dash in the filename.
We should then put the executed function in its own importable file.
"""
callback_filepath = inspect.getfile(callback)
name = f"_upgrade_{uuid.uuid4().hex}"
mod = sys.modules[name] = import_script(callback_filepath, name=name)
try:
return getattr(mod, callback.__name__)
except AttributeError:
error_msg = (
f"The converter callback `{callback.__name__}` is a nested function in `{callback.__module__}`.\n"
"Move it outside the `migrate()` function to make it top-level."
)
raise MigrationError(error_msg) from None
class BaseConverter:
def __init__(self, callback, selector=None):
self.callback = callback
self.selector = selector
def for_html(self):
return HTMLConverter(self.callback, self.selector)
def for_qweb(self):
return QWebConverter(self.callback, self.selector)
def has_changed(self, els):
if self.selector:
converted = [self.callback(el) for el in els.xpath(self.selector)]
return any(converted)
return self.callback(els)
def __call__(self, content):
# Remove `<?xml ...>` header
if not content:
return (False, content)
content = re.sub(r"^<\?xml .+\?>\s*", "", content.strip())
# Wrap in <wrap> node before parsing to preserve external comments and multi-root nodes,
# except for when this looks like a full html doc, because in this case the wrap tag breaks the logic in
# https://github.com/lxml/lxml/blob/2ac88908ffd6df380615c0af35f2134325e4bf30/src/lxml/html/html5parser.py#L184
els = self._loads(content if content.strip()[:5].lower() == "<html" else f"<wrap>{content}</wrap>")
has_changed = self.has_changed(els)
new_content = re.sub(r"(^<wrap>|</wrap>$|^<wrap/>$)", "", self._dumps(els).strip()) if has_changed else content
return (has_changed, new_content)
def _loads(self, string):
raise NotImplementedError
def _dumps(self, node):
raise NotImplementedError
class HTMLConverter(BaseConverter):
def for_html(self):
return self
def _loads(self, string):
return html.fromstring(string, parser=utf8_parser)
def _dumps(self, node):
return html.tostring(node, encoding="unicode")
class QWebConverter(BaseConverter):
def for_qweb(self):
return self
def _loads(self, string):
return html.fromstring(string, parser=html.XHTMLParser(encoding="utf-8"))
def _dumps(self, node):
return etree.tostring(node, encoding="unicode")
class Convertor:
def __init__(self, converters, callback, dbname=None, update_query=None):
self.converters = converters
self.callback = callback
self.dbname = dbname
self.update_query = update_query
def __call__(self, query):
# backwards compatibility
if not (self.dbname and self.update_query and isinstance(query, str)):
return self._convert_row(query)
# called with a query to fetch a number of rows
with db_connect(self.dbname).cursor() as cr:
cr.execute(query)
for changes in filter(None, map(self._convert_row, cr.fetchall())):
cr.execute(self.update_query, changes)
return None
def _convert_row(self, row):
converters = self.converters
columns = self.converters.keys()
converter_callback = self.callback
res_id, *contents = row
changes = {}
for column, content in zip(columns, contents):
if content and converters[column]:
# jsonb column; convert all keys
new_content = {}
has_changed, new_content["en_US"] = converter_callback(content.pop("en_US"))
if has_changed:
for lang, value in content.items():
_, new_content[lang] = converter_callback(value)
new_content = Json(new_content)
else:
has_changed, new_content = converter_callback(content)
changes[column] = new_content
if has_changed:
changes["id"] = res_id
return changes if "id" in changes else None
def convert_html_columns(cr, table, columns, converter_callback, where_column="IS NOT NULL", extra_where="true"):
r"""
Convert HTML content for the given table column.
:param cursor cr: database cursor
:param str table: table name
:param str column: column name
:param func converter_callback: conversion function that converts the HTML
text content and returns a tuple with a boolean that indicates whether a
change happened and the new content must be saved
:param str where_column: filtering such as
- "like '%abc%xyz%'"
- "~* '\yabc.*xyz\y'"
:param str extra_where: extra filtering on the where clause
"""
assert "id" not in columns
converters = {column: "->>'en_US'" if column_type(cr, table, column) == "jsonb" else "" for column in columns}
select = ", ".join(f'"{column}"' for column in columns)
where = " OR ".join(f'"{column}"{converters[column]} {where_column}' for column in columns)
base_select_query = f"""
SELECT id, {select}
FROM {table}
WHERE ({where})
AND ({extra_where})
"""
split_queries = [
(base_select_query + "\n AND id BETWEEN {} AND {}".format(*x))
for x in determine_chunk_limit_ids(cr, table, columns, "({}) AND ({})".format(where, extra_where))
]
update_sql = ", ".join(f'"{column}" = %({column})s' for column in columns)
update_query = f"UPDATE {table} SET {update_sql} WHERE id = %(id)s"
cr.commit()
with ProcessPoolExecutor(max_workers=get_max_workers()) as executor:
convert = Convertor(converters, converter_callback, cr.dbname, update_query)
futures = [executor.submit(convert, query) for query in split_queries]
for future in log_progress(
concurrent.futures.as_completed(futures),
logger=_logger,
qualifier=f"{table} updates",
size=len(split_queries),
estimate=False,
log_hundred_percent=True,
):
# just for raising any worker exception
future.result()
cr.commit()
def determine_chunk_limit_ids(cr, table, column_arr, where):
bytes_per_chunk = 10 * 1024 * 1024
columns = ", ".join(quote_ident(column, cr._cnx) for column in column_arr if column != "id")
cr.execute(
f"""
WITH info AS (
SELECT id,
sum(pg_column_size(({columns}, id))) OVER (ORDER BY id) / {bytes_per_chunk} AS chunk
FROM {table}
WHERE {where}
) SELECT min(id), max(id) FROM info GROUP BY chunk
"""
)
return cr.fetchall()
def convert_html_content(
cr,
converter_callback,
where_column="IS NOT NULL",
**kwargs,
):
r"""
Convert HTML content.
:param cursor cr: database cursor
:param func converter_callback: conversion function that converts the HTML
text content and returns a tuple with a boolean that indicates whether a
change happened and the new content must be saved
:param str where_column: filtering such as
- "like '%abc%xyz%'"
- "~* '\yabc.*xyz\y'"
:param dict kwargs: extra keyword arguments to pass to :func:`convert_html_column`
"""
if hasattr(converter_callback, "for_html"): # noqa: SIM108
html_converter = converter_callback.for_html()
else:
# trust the given converter to handle HTML
html_converter = converter_callback
for table, columns in html_fields(cr):
convert_html_columns(cr, table, columns, html_converter, where_column=where_column, **kwargs)
if hasattr(converter_callback, "for_qweb"):
qweb_converter = converter_callback.for_qweb()
else:
_logger.log(NEARLYWARN, "Cannot adapt converter callback %r for qweb; using it directly", converter_callback)
qweb_converter = converter_callback
convert_html_columns(
cr,
"ir_ui_view",
["arch_db"],
qweb_converter,
where_column=where_column,
**dict(kwargs, extra_where="type = 'qweb'"),
)