-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathchecks_odoo_module_xml.py
More file actions
1001 lines (906 loc) · 48.5 KB
/
checks_odoo_module_xml.py
File metadata and controls
1001 lines (906 loc) · 48.5 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import re
from collections import defaultdict, namedtuple
from copy import deepcopy
from pathlib import Path
from typing import Dict, List
from lxml import etree
from packaging.version import Version
from oca_pre_commit_hooks import node_xml, utils
from oca_pre_commit_hooks.base_checker import BaseChecker
DFLT_DEPRECATED_TREE_ATTRS = ["colors", "fonts", "string"]
DFTL_MIN_PRIORITY = 99
XML_HEADER_EXPECTED = b'<?xml version="1.0" encoding="UTF-8" ?>'
XML_HEADER_RE = re.compile(rb"^<\?xml[^>]*\?>", re.IGNORECASE | re.MULTILINE)
NUMBER_RE = re.compile(r"^(?P<integer>[+-]?\d+)(?P<decimal>\.\d+)?$")
XML_PYTHON_ATTRS_RE = re.compile(
r"^(t-.*|data-.*|decoration-.*|"
+ "|".join(
map(
re.escape,
{
"add",
"attrs",
"class",
"colors",
"context",
"digits",
"domain",
"eval",
"filter_domain",
"options",
"placeholder",
"precision",
"remove",
"search",
"statusbar_colors",
"studio_groups",
"thumbnails",
},
)
)
+ ")$"
)
# Same as Odoo: https://github.com/odoo/odoo/commit/9cefa76988ff94c3d590c6631b604755114d0297
def _hasclass(context, *cls):
"""Checks if the context node has all the classes passed as arguments"""
node_classes = set(context.context_node.attrib.get("class", "").split())
return node_classes.issuperset(cls)
etree.FunctionNamespace(None)["hasclass"] = _hasclass
# Store the shortname for the XML File and one of its Elements
FileElementPair = namedtuple("FileElementPair", ["filename", "element"])
class ChecksOdooModuleXML(BaseChecker):
xpath_deprecated_data = etree.XPath("/odoo[count(./*) < 2]/data|/openerp[count(./*) < 2]/data")
xpath_oe_structure_woid = etree.XPath(
"//*[hasclass('oe_structure') and (not(@id) or not(contains(@id, 'oe_structure')))]"
)
# Add menuitem as record since that they have the same checks for now, maybe in the future separate them
xpath_record = etree.XPath("/odoo//record | /openerp//record | /odoo//menuitem | /openerp//menuitem")
xpath_view_arch_xml = etree.XPath("field[@name='arch' and @type='xml'][1]")
xpath_ir_fields = etree.XPath("field[@name='name' or @name='user_id']")
xpath_template = etree.XPath("/odoo//template|/openerp//template")
xpath_view_replaces = etree.XPath(".//*[@position='replace'][1]")
xpath_char_links = etree.XPath(".//link[@href]|.//script[@src]")
xpath_view_priority = etree.XPath("field[@name='priority'][1]")
xpath_field_name = etree.XPath("field[@name='name'][1]")
xpath_record_fields_wname = etree.XPath("field[@name]")
xpath_comment = etree.XPath("//comment()")
xpath_openerp = etree.XPath("/openerp")
xpath_xpath = etree.XPath("//xpath")
xpath_oe_chatter = etree.XPath("//div[hasclass('oe_chatter')]")
tree_deprecate_attrs = {"string", "colors", "fonts"}
xpath_tree_deprecated = etree.XPath(f'.//tree[{"|".join(f"@{a}" for a in tree_deprecate_attrs)}]')
qweb_deprecated_directives = {
"t-esc-options",
"t-field-options",
"t-raw-options",
}
qweb_deprecated_attrs = "|".join(f"@{d}" for d in qweb_deprecated_directives)
xpath_qweb_deprecated = etree.XPath(
f"/odoo//template//*[{qweb_deprecated_attrs}] | " f"/openerp//template//*[{qweb_deprecated_attrs}]"
)
qweb_deprecated_directives15 = {"t-esc", "t-raw"}
qweb_deprecated_attrs15 = "|".join(f"@{d}" for d in qweb_deprecated_directives15)
xpath_qweb_deprecated15 = etree.XPath(
f"/odoo//template//*[{qweb_deprecated_attrs15}] | " f"/openerp//template//*[{qweb_deprecated_attrs15}]"
)
@staticmethod
def _get_boolean_field_by_model(model_name):
return utils.DFLT_BOOLEAN_FIELDS + (utils.DFLT_BOOLEAN_FIELDS_BY_MODEL.get(model_name) or [])
@staticmethod
def _get_numeric_field_by_model(model_name):
return utils.DFLT_NUMERIC_FIELDS + (utils.DFLT_NUMERIC_FIELDS_BY_MODEL.get(model_name) or [])
def _get_first_tag(self, fileobj_xml):
buffer = []
for lineno, line in enumerate(fileobj_xml):
line_stripped = line.strip(b" \n")
if not buffer and line_stripped.startswith(b"<"):
buffer.append(line_stripped)
buffer_lineno = lineno + 1
if buffer and line_stripped.endswith(b">"):
if line_stripped not in buffer:
buffer.append(line_stripped)
return b" ".join(buffer), buffer_lineno
return "", 0
def update_node(self, manifest_data):
"""Update the etree node of the manifest_data.
Useful when the file is modified by autofix and a new line is inserted/removed.
It will update the sourceline of the nodes"""
with open(manifest_data["filename"], "rb") as f_xml:
node = etree.parse(f_xml)
manifest_data.update({"node": node})
f_xml.seek(0)
first_tag, first_tag_lineno = self._get_first_tag(f_xml)
manifest_data.update(
{
"node": node,
"first_tag": first_tag,
"first_tag_lineno": first_tag_lineno,
}
)
return node
def __init__(self, manifest_datas, module_name, enable, disable, module_version, autofix):
super().__init__(enable, disable, module_name, module_version, autofix)
self.manifest_datas = manifest_datas or []
self.autofix = autofix
for manifest_data in self.manifest_datas:
try:
node = self.update_node(manifest_data)
manifest_data.update(
{
"file_error": None,
"disabled_checks": self._get_disabled_checks(node, manifest_data),
}
)
except (FileNotFoundError, etree.XMLSyntaxError, UnicodeDecodeError) as xml_err:
manifest_data.update(
{
"node": etree.Element("__empty__"),
"file_error": str(xml_err).replace(manifest_data["filename"], ""),
"disabled_checks": set(),
}
)
def _get_disabled_checks(self, node, manifest_data):
"""Get the check-name disable comments from etree XML node
e.g. <!-- oca-hooks:disable=check-name -->
"""
all_checks_disabled = set()
for comment_node in self.xpath_comment(node):
checks_disabled, use_deprecated = utils.checks_disabled(comment_node.text)
all_checks_disabled |= set(checks_disabled)
if use_deprecated:
print(
f"{manifest_data['filename_short']}:{comment_node.sourceline} WARNING. DEPRECATED. Use oca-disable instead."
)
return all_checks_disabled
def getattr_checks(self, manifest_data, prefix):
disable_node = manifest_data["disabled_checks"]
yield from utils.getattr_checks(self, prefix, disable_node)
@classmethod
def _get_priority(cls, view):
try:
priority_node = cls.xpath_view_priority(view)[0]
return int(priority_node.get("eval", priority_node.text) or 0)
except (IndexError, ValueError):
# IndexError: If the field is not found
# ValueError: If the value found is not valid integer
return 0
@classmethod
def _is_replaced_field(cls, view):
try:
arch = cls.xpath_view_arch_xml(view)[0]
except IndexError:
return False
replaces = cls.xpath_view_replaces(arch)
return bool(replaces)
@utils.only_required_for_checks("xml-header-missing", "xml-header-wrong")
def check_xml_header(self):
"""* Check xml-header-missing
Generated when the XML file is missing the XML declaration header '<?xml version="1.0" encoding="UTF-8" ?>'
* Check xml-header-wrong
Generated when the XML file declaration header is different than expected (case sensitive).
"""
for manifest_data in self.manifest_datas:
first_tag = manifest_data.get("first_tag")
if first_tag is None:
# Error reading the file, skip checks
continue
if not first_tag.startswith(b"<?xml "):
if self.is_message_enabled("xml-header-missing", manifest_data["disabled_checks"]):
self.register_error(
code="xml-header-missing",
message="XML missing header",
filepath=manifest_data["filename_short"],
line=1,
)
if self.autofix:
with open(manifest_data["filename"], "rb") as f_xml:
content = b'<?xml version="1.0" encoding="UTF-8" ?>\n' + f_xml.read()
utils.perform_fix(manifest_data["filename"], content)
self.update_node(manifest_data) # update sourceline after insert a new line
elif self.is_message_enabled("xml-header-wrong", manifest_data["disabled_checks"]):
new_content = XML_HEADER_RE.sub(XML_HEADER_EXPECTED, first_tag, count=1)
if new_content != first_tag:
self.register_error(
code="xml-header-wrong",
message=(
f'XML header expected \'{XML_HEADER_EXPECTED.decode("UTF-8")}\' '
f'but received \'{first_tag.decode("UTF-8")}\''
),
filepath=manifest_data["filename_short"],
line=manifest_data["first_tag_lineno"],
)
if self.autofix:
with open(manifest_data["filename"], "rb") as f_xml:
content = f_xml.read()
utils.perform_fix(
manifest_data["filename"], XML_HEADER_RE.sub(XML_HEADER_EXPECTED, content, count=1)
)
self.update_node(manifest_data) # update sourceline after remove a possible line
# Not set only_required_for_checks because of the calls to visit_xml_record... methods
def check_xml_records(self):
"""* Check xml-record-missing-id
Generated when a <record> tag has no id.
* Check xml-duplicate-record-id
If a module has duplicated record_id AKA xml_ids
file1.xml
<record id="xmlid_name1"
file2.xml
<record id="xmlid_name1"
* Check xml-duplicate-fields in all record nodes
<record id="xmlid_name1"...
<field name="field_name1"...
<field name="field_name1"...
"""
xmlids_section: Dict[str, List[FileElementPair]] = defaultdict(list)
xml_fields = defaultdict(list)
for manifest_data in self.manifest_datas:
for record in self.xpath_record(manifest_data["node"]):
record_id = record.get("id")
if not record_id and self.is_message_enabled(
"xml-record-missing-id", manifest_data["disabled_checks"]
):
self.register_error(
code="xml-record-missing-id",
message="Record has no id, add a unique one to create a new record, use an existing one to update it",
filepath=manifest_data["filename_short"],
line=record.sourceline,
)
if self.is_message_enabled("xml-duplicate-record-id", manifest_data["disabled_checks"]):
# xmlids_duplicated
xmlid_key = (
f"{manifest_data['data_section']}/{record_id}"
f"_noupdate_{record.getparent().get('noupdate', '0')}"
)
xmlids_section[xmlid_key].append(FileElementPair(manifest_data["filename_short"], record))
# fields_duplicated
if self.is_message_enabled("xml-duplicate-fields", manifest_data["disabled_checks"]):
for field in self.xpath_record_fields_wname(record):
xml_fields[(field.get("name"), field.getparent())].append((manifest_data, field))
# call "visit_xml_record_*" methods to re-use the same node xpath loop
for meth in self.getattr_checks(manifest_data, "visit_xml_record"):
meth(manifest_data, record)
# xmlids_duplicated (empty dict if check is not enabled)
for __, records in xmlids_section.items():
if len(records) < 2:
continue
self.register_error(
code="xml-duplicate-record-id",
message=f"Duplicate xml record id `{records[0].element.get('id')}`",
filepath=records[0].filename,
line=records[0].element.sourceline,
extra_positions=[(record.filename, record.element.sourceline) for record in records[1:]],
)
# fields_duplicated (empty dict if check is not enabled)
for field_key, fields in xml_fields.items():
if len(fields) < 2:
continue
self.register_error(
code="xml-duplicate-fields",
message=f"Duplicate xml field `{field_key[0]}`",
filepath=fields[0][0]["filename_short"],
line=fields[0][1].sourceline,
extra_positions=[(field[0]["filename_short"], field[1].sourceline) for field in fields[1:]],
)
@utils.only_required_for_checks("xml-syntax-error")
def check_xml_syntax_error(self):
"""* Check xml-syntax-error
Check syntax of XML files declared in the Odoo manifest"""
for manifest_data in self.manifest_datas:
if not manifest_data["file_error"]:
continue
self.register_error(
code="xml-syntax-error",
message=manifest_data["file_error"],
filepath=manifest_data["filename_short"],
line=1,
)
def _check_xml_field_eval(self, manifest_data, record):
if not (
self.is_message_enabled("xml-field-bool-without-eval", manifest_data["disabled_checks"])
or self.is_message_enabled("xml-field-numeric-without-eval", manifest_data["disabled_checks"])
):
# skip if none of the checks are enabled
return
for field in record.xpath(".//field[@name and not(@eval) and text() and not(@type)]"):
field_name = field.get("name")
field_text = field.text.title()
model_name = None
if (record := field.getparent()) is not None:
model_name = record.get("model")
if not model_name:
continue
needs_fix = False
if (
self.is_message_enabled("xml-field-bool-without-eval", manifest_data["disabled_checks"])
and field_text in ["True", "False"]
and field_name in self._get_boolean_field_by_model(model_name)
):
self.register_error(
code="xml-field-bool-without-eval",
message=f"Field `{field_name}` with boolean value without `eval` attribute",
filepath=manifest_data["filename_short"],
line=field.sourceline,
)
needs_fix = True
elif (
self.is_message_enabled("xml-field-numeric-without-eval", manifest_data["disabled_checks"])
and NUMBER_RE.match(field_text)
and field_name in self._get_numeric_field_by_model(model_name)
):
self.register_error(
code="xml-field-numeric-without-eval",
message=f"Field `{field_name}` with numeric value without `eval` attribute",
filepath=manifest_data["filename_short"],
line=field.sourceline,
)
needs_fix = True
if needs_fix and self.autofix:
field_copy = deepcopy(field)
field.attrib["eval"] = field_text
field.text = None
node_content = node_xml.NodeContent(manifest_data["filename"], field)
field_regex = (
rf"<{re.escape(field.tag)}\s+name\s*=\s*(?P<q>[\"']){re.escape(field_name)}(?P=q).*".encode()
)
spaces_match = re.search(field_regex, node_content.content_node)
if spaces_match:
content_node2 = node_content.content_node.replace(
spaces_match.group(), etree.tostring(field).rstrip(b" \n")
)
if content_node2 != node_content.content_node:
node_content.content_node = content_node2.replace(b"/>", b" />")
utils.perform_fix(manifest_data["filename"], bytes(node_content))
else:
field = field_copy # revert changes if not fixed
@utils.only_required_for_checks(
"xml-field-bool-without-eval",
"xml-field-numeric-without-eval",
"xml-id-position-first",
"xml-redundant-module-name",
)
def visit_xml_record(self, manifest_data, record):
"""* Check xml-redundant-module-name
If the module is called "module_a" and the xmlid is
`<record id="module_a.xmlid_name1" ...`
The "module_a." is redundant it could be replaced to only
`<record id="xmlid_name1" ...`
* Check xml-id-position-first
If the record id is not in the first position
`<record ... id="xmlid_name1"`
It should be the first
`<record id="xmlid_name1" ...`
* Check xml-field-bool-without-eval
if the record is boolean but without eval attribute
`<field name="active">True</field>`
instead of
`<field name="active" eval="True" />`
* Check xml-field-numeric-without-eval
if the record is integer but without eval attribute
`<field name="sequence">100</field>`
instead of
`<field name="sequence" eval="100" />`
"""
self._check_xml_field_eval(manifest_data, record)
record_id = record.get("id")
if not record_id:
return
xmlid_module, xmlid_name = record_id.split(".") if "." in record_id else ["", record_id]
if xmlid_module == self.module_name and self.is_message_enabled(
"xml-redundant-module-name", manifest_data["disabled_checks"]
):
self.register_error(
code="xml-redundant-module-name",
message=f'Redundant module name `<{record.tag} id="{record_id}"`',
info=f'Use `<{record.tag} id="{xmlid_name}"` instead',
filepath=manifest_data["filename_short"],
line=record.sourceline,
)
if self.autofix:
node_content = node_xml.NodeContent(manifest_data["filename"], record)
pattern = rb'\bid\s*=\s*(?P<q>["\'])(?P<id>' + re.escape(record_id.encode()) + rb")(?P=q)"
content_node2 = re.sub(
pattern, rb"id=\g<q>" + xmlid_name.encode() + rb"\g<q>", node_content.content_node, count=1
)
if content_node2 != node_content.content_node:
# Modify the record attrib to propagate the change to other checks
record.attrib["id"] = xmlid_name
node_content.content_node = content_node2
utils.perform_fix(manifest_data["filename"], bytes(node_content))
first_attr = record.keys()[0]
if first_attr != "id" and self.is_message_enabled("xml-id-position-first", manifest_data["disabled_checks"]):
self.register_error(
code="xml-id-position-first",
message=f'The "id" attribute must be first `<{record.tag} id="{record_id}" {first_attr}=...`',
info=f'Use `<{record.tag} id="{record_id}" {first_attr}=...` instead',
filepath=manifest_data["filename_short"],
line=record.sourceline,
)
if self.autofix:
self.autofix_id_position_first(record, first_attr, manifest_data)
def autofix_id_position_first(self, node, first_attr, manifest_data):
attrs = dict(node.attrib)
node_content = node_xml.NodeContent(manifest_data["filename"], node)
# Build regex pattern to match the tag with all its known attributes
# sourceline is the last line of the last attribute, so we need to search backwards
tag_name = re.escape(node.tag)
# Create a pattern that matches all known attributes in any order
# Each attribute: attrname="attrvalue" with optional whitespace
attr_patterns = []
# Use the first attribute spaces since that id will be the new first attribute
keys = [f"spaces_before_{first_attr}", "id"]
for attr_name, attr_value in attrs.items():
escaped_name = re.escape(attr_name)
escaped_value = re.escape(attr_value)
# Match attribute with flexible whitespace and quotes
attr_patterns.append(
rf'(?P<spaces_before_{attr_name}>\s*)(?P<{attr_name}>{escaped_name}\s*=\s*(?P<quote_{attr_name}>["\'])({escaped_value})(?P=quote_{attr_name}))'
)
if attr_name == "id":
# skip the first key "id" because is already added
continue
if attr_name == first_attr:
# Use the same spaces_before_id for the first attribute
# <record name="test_name"
# id="test_id"
# />
# <record id="test_id"
# name="test_name"
# />
keys.extend(["spaces_before_id", attr_name])
continue
keys.extend([f"spaces_before_{attr_name}", attr_name])
# Pattern for the complete opening tag
# <tag_name whitespace attr1 whitespace attr2 ... whitespace>
# Using DOTALL to match across lines
attrs_regex = r"".join(attr_patterns)
pattern = (
rf"(?P<open_{node.tag}><{tag_name})" # Opening tag with space
rf"{attrs_regex}" # All attributes with whitespace between them
rf"(?P<close_{node.tag}>\s*(/?)>)" # Optional self-closing and closing >
)
# Search with multiline and dotall flags
match = re.search(pattern, node_content.content_node.decode(), re.DOTALL | re.MULTILINE)
if match:
keys = [f"open_{node.tag}"] + keys + [f"close_{node.tag}"]
match_dict = match.groupdict()
recreate = "".join(match_dict[k] for k in keys)
original = match.group()
content_node2 = node_content.content_node.replace(original.encode(), recreate.encode(), 1)
if content_node2 != node_content.content_node:
# Modify the record attrib to propagate the change to other checks
id_value = attrs.pop("id")
node.attrib.clear()
new_attrs = {"id": id_value, **attrs}
node.attrib.update(new_attrs)
node_content.content_node = content_node2
utils.perform_fix(manifest_data["filename"], bytes(node_content))
@utils.only_required_for_checks("xml-view-dangerous-replace-low-priority", "xml-deprecated-tree-attribute")
def visit_xml_record_view(self, manifest_data, record):
"""* Check xml-view-dangerous-replace-low-priority in ir.ui.view
<field name="priority" eval="10"/>
...
<field name="name" position="replace"/>
* Check xml-deprecated-tree-attribute
The tree-view declaration is using a deprecated attribute.
"""
if record.get("model") != "ir.ui.view":
return
# view_dangerous_replace_low_priority
if self.is_message_enabled("xml-view-dangerous-replace-low-priority", manifest_data["disabled_checks"]):
priority = self._get_priority(record)
is_replaced_field = self._is_replaced_field(record)
# TODO: Add self.config.min_priority instead of DFTL_MIN_PRIORITY
if is_replaced_field and priority < DFTL_MIN_PRIORITY:
self.register_error(
code="xml-view-dangerous-replace-low-priority",
message=f"Dangerous use of `replace` from view with priority {priority} < {DFTL_MIN_PRIORITY}",
info='Only replace as a last resort. Try `position="attributes"`, `position="move"` or `invisible="1"` first',
filepath=manifest_data["filename_short"],
line=record.sourceline,
)
# deprecated_tree_attribute
if self.is_message_enabled("xml-deprecated-tree-attribute", manifest_data["disabled_checks"]):
for deprecate_attr_node in self.xpath_tree_deprecated(record):
deprecate_attr_str = ",".join(set(deprecate_attr_node.attrib.keys()) & self.tree_deprecate_attrs)
self.register_error(
code="xml-deprecated-tree-attribute",
message=f'Deprecated "<tree {deprecate_attr_str}=..."',
filepath=manifest_data["filename_short"],
line=deprecate_attr_node.sourceline,
)
@utils.only_required_for_checks("xml-create-user-wo-reset-password")
def visit_xml_record_user(self, manifest_data, record):
"""* Check xml-create-user-wo-reset-password
records of user without `context="{'no_reset_password': True}"`
This context avoid send email and mail log warning
"""
# xml_create_user_wo_reset_password
if record.get("model") != "res.users":
return
if record.xpath("field[@name='name'][1]") and "no_reset_password" not in (record.get("context") or ""):
# if exists field="name" then is a new record
# then should be context
self.register_error(
code="xml-create-user-wo-reset-password",
message="record res.users without `context=\"{'no_reset_password': True}\"`",
filepath=manifest_data["filename_short"],
line=record.sourceline,
)
@utils.only_required_for_checks("xml-not-valid-char-link")
def check_xml_not_valid_char_link(self):
"""* Check xml-not-valid-char-link
The resource in in src/href contains a not valid character."""
for manifest_data in self.manifest_datas:
if not self.is_message_enabled("xml-not-valid-char-link", manifest_data["disabled_checks"]):
continue
for node in self.xpath_char_links(manifest_data["node"]):
resource = node.get("href", "") or node.get("src", "")
ext = os.path.splitext(os.path.basename(resource))[1]
if resource.startswith("/") and not re.search("^[.][a-zA-Z]+$", ext):
self.register_error(
code="xml-not-valid-char-link",
message="The resource in in src/href contains a not valid character",
filepath=manifest_data["filename_short"],
line=node.sourceline,
)
def verify_qweb_replace(self, template, manifest_data):
try:
priority = int(template.get("priority"))
except (ValueError, TypeError):
priority = 0
for child in template.iterchildren():
# TODO: Add self.config.min_priority instead of DFTL_MIN_PRIORITY
if child.get("position") == "replace" and priority < DFTL_MIN_PRIORITY:
self.register_error(
code="xml-dangerous-qweb-replace-low-priority",
message=f"Dangerous use of `replace` from view with priority {priority} < {DFTL_MIN_PRIORITY}",
info='Only replace as a last resort. Try `position="attributes"`, `position="move"` or `t-if="False"` first',
filepath=manifest_data["filename_short"],
line=child.sourceline,
)
@staticmethod
def get_template_xmlid(template, manifest_data):
template_id = template.get("id")
if not template_id: # pragma: no cover
return ""
return f"{manifest_data['data_section']}/{template_id}_noupdate_{template.getparent().get('noupdate', '0')}"
def verify_template_prettier_incompatible(self, template, manifest_data):
"""There are text tags incompatible with prettier xml autofix
More info https://github.com/OCA/odoo-pre-commit-hooks/issues/149"""
target_attrs = {"t-out", "t-esc", "t-raw"}
# Tags that wrap inline content and are affected by prettier formatting
inline_wrapper_tags = {"textarea", "b", "strong", "i", "em", "span", "a", "u", "s", "small", "mark"}
for wrapper_tag in inline_wrapper_tags:
for node_wrapper in template.xpath(f".//{wrapper_tag}"):
children = node_wrapper.getchildren()
if len(children) != 1:
continue
node_wrapper_child = children[0]
# Check if child has template attributes
found_attr = set(node_wrapper_child.attrib) & target_attrs
if not found_attr:
continue
# Only check for t or span tags with template attributes
if node_wrapper_child.tag not in ("t", "span"):
continue
# Check if wrapper has ONLY whitespace (or nothing) before the child
wrapper_text = node_wrapper.text or ""
has_meaningful_text = wrapper_text.strip()
# The problem occurs when:
# 1. There's a template attribute on the child
# 2. The wrapper has no meaningful text before the child (only whitespace/newlines)
# This means prettier will reformat and add unwanted newlines
if not has_meaningful_text:
found_attr = found_attr.pop()
self.register_error(
code="xml-template-prettier-incompatible",
message=(
f"Node `<{node_wrapper.tag} ...><{node_wrapper_child.tag} {found_attr}=...` "
"incompatible for Prettier XML auto-fix. To prevent unexpected text insertion "
f"prefer `<{node_wrapper.tag} {found_attr}=...>` (move attribute to parent) or "
"using 'style' attribute instead of tag "
'e.g. <tag style="font-weight: bold">Black Text... instead of <b><tag...'
),
filepath=manifest_data["filename_short"],
line=node_wrapper_child.sourceline,
)
# TODO: Autofix using the same node
def has_escaped_double_quotes(self, filename) -> bool:
"""Check if filename contains escaped double quotes " -> "
This is used to detect cases where prettier auto-fix converts
attributes like attr='""' into attr="""", which is
technically valid XML but not human-readable.
:param filename: Path to the XML file to inspect
:return: True if escaped double quotes are found, False otherwise
"""
filename_path = Path(filename)
if not filename_path.is_file():
return False
with filename_path.open("rb") as f_xml:
for line in f_xml:
if b""" in line:
return True
return False
def is_compatible_single_quote(self, py_code):
try:
compile(py_code, "<string>", "exec")
except Exception: # pylint:disable=broad-exception-caught
# Skip if it is already failed
return False
if py_code == (new_py_code := py_code.replace('"', "'")):
# Skip if no changes
return False
try:
compile(new_py_code, "<string>", "exec")
except Exception: # pylint:disable=broad-exception-caught
# Skip if after changing it fails
return False
return new_py_code
@utils.only_required_for_checks("xml-double-quotes-py")
def check_xml_double_quotes_py(self):
"""* Check xml-double-quotes-py
Detect XML attributes containing escaped double quotes " -> (")
for python expressions Python expressions (e.g. domain, context, eval, t-* or data-* attrs).
that originate from Prettier auto-fix and reduce human readability.
When Prettier rewrites values like attr='""' into attr=""""
the result is valid XML but harder to read and maintain.
The check:
- Scans XML files that contain escaped double quotes (")
- Restricts analysis to attributes known to embed Python code
- Verifies that both the original and the proposed value are valid Python expressions
- Suggests replacing double quotes with single quotes when safe
This avoids false positives and prevents introducing syntax errors
while improving readability and Prettier compatibility."""
for manifest_data in self.manifest_datas:
if not self.is_message_enabled(
"xml-double-quotes-py", manifest_data["disabled_checks"]
) or not self.has_escaped_double_quotes(manifest_data["filename"]):
continue
for elem in manifest_data["node"].iter():
if (
(py_code := elem.text)
and XML_PYTHON_ATTRS_RE.match(elem.get("name") or "")
and (new_py_code := self.is_compatible_single_quote(py_code))
):
# Process text <field name="context">{}</field>
node_content = node_xml.NodeContent(manifest_data["filename"], elem)
if b""" not in node_content.content_node:
continue
self.register_error(
code="xml-double-quotes-py",
message='Escaped double quotes " for python code detected',
info=f"Use single quote instead: `{new_py_code}`",
filepath=manifest_data["filename_short"],
line=elem.sourceline,
)
during2 = node_content.content_node.replace(b""", b"'")
if self.autofix and during2 != node_content.content_node:
# Modify the xml node to propagate the change to other checks
elem.text = new_py_code
node_content.content_node = during2
utils.perform_fix(manifest_data["filename"], bytes(node_content))
for attr_name, attr_value in elem.attrib.items():
# Process attributes <field domain="[]" context="{}" ../>
if not XML_PYTHON_ATTRS_RE.match(attr_name):
# Skip if it is not a known attribute to embed Python code
continue
if not (new_py_code := self.is_compatible_single_quote(attr_value)):
continue
node_content = node_xml.NodeContent(manifest_data["filename"], elem)
if b""" not in node_content.content_node:
continue
self.register_error(
code="xml-double-quotes-py",
message='Escaped double quotes " for python code detected use',
info=f"Use single quote instead: `{new_py_code}`",
filepath=manifest_data["filename_short"],
line=elem.sourceline,
)
during2 = node_content.content_node.replace(b""", b"'")
if self.autofix and during2 != node_content.content_node:
# Modify the xml node to propagate the change to other checks
elem.attrib[attr_name] = new_py_code
node_content.content_node = during2
utils.perform_fix(manifest_data["filename"], bytes(node_content))
@utils.only_required_for_checks(
"xml-dangerous-qweb-replace-low-priority",
"xml-duplicate-template-id",
"xml-id-position-first",
"xml-superfluous-attributeless",
"xml-template-prettier-incompatible",
)
def check_xml_templates(self):
"""* Check xml-dangerous-qweb-replace-low-priority
Dangerous qweb view defined with low priority
* Check xml-duplicate-template-id
Triggered when two templates share the same ID
* Check xml-template-prettier-incompatible
Indentify nodes incompatible with Prettier XML auto-fix generating possible unexpected text insertion
"""
template_ids: Dict[str, List[FileElementPair]] = defaultdict(list)
pattern = re.compile(rb"<span>(.*?)</span>")
for manifest_data in self.manifest_datas:
for template in self.xpath_template(manifest_data["node"]):
if self.is_message_enabled(
"xml-dangerous-qweb-replace-low-priority", manifest_data["disabled_checks"]
):
self.verify_qweb_replace(template, manifest_data)
if self.is_message_enabled("xml-duplicate-template-id", manifest_data["disabled_checks"]):
template_id = self.get_template_xmlid(template, manifest_data)
if not template_id: # pragma: no cover
continue
template_ids[template_id].append(FileElementPair(manifest_data["filename_short"], template))
if self.is_message_enabled("xml-superfluous-attributeless", manifest_data["disabled_checks"]):
for node_attrless in template.xpath(".//span[not(@*)]"):
self.register_error(
code="xml-superfluous-attributeless",
message=f"Remove superfluous attributeless `<{node_attrless.tag}`",
info=f"Serve no purpose and cause formatting inconsistencies with Prettier",
filepath=manifest_data["filename_short"],
line=node_attrless.sourceline,
)
if self.autofix:
node_content = node_xml.NodeContent(manifest_data["filename"], node_attrless)
new_content_node = pattern.sub(rb"\1", node_content.content_node, count=1)
if new_content_node != node_content.content_node:
# Modify the record attrib to propagate the change to other checks
node_content.content_node = new_content_node
utils.perform_fix(manifest_data["filename"], bytes(node_content))
# TODO: check if the "for" affects updating the nodes
self.update_node(manifest_data) # update sourceline after delete a node
if self.is_message_enabled("xml-template-prettier-incompatible", manifest_data["disabled_checks"]):
self.verify_template_prettier_incompatible(template, manifest_data)
if (
self.is_message_enabled("xml-id-position-first", manifest_data["disabled_checks"])
and (first_attr := template.keys()[0]) != "id"
and (template_id_short := template.get("id"))
):
self.register_error(
code="xml-id-position-first",
message=f'The "id" attribute must be first `<{template.tag} id="{template_id_short}" {first_attr}=...`',
info=f'Use `<{template.tag} id="{template_id_short}" {first_attr}=...` instead',
filepath=manifest_data["filename_short"],
line=template.sourceline,
)
if self.autofix:
self.autofix_id_position_first(template, first_attr, manifest_data)
for xmlid_key, records in template_ids.items():
if len(records) < 2:
continue
self.register_error(
code="xml-duplicate-template-id",
message=f"Duplicate xml template id `{xmlid_key}`",
filepath=records[0].filename,
line=records[0].element.sourceline,
extra_positions=[(record.filename, record.element.sourceline) for record in records[1:]],
)
@utils.only_required_for_checks("xml-deprecated-data-node")
def check_xml_deprecated_data_node(self):
"""* Check xml-deprecated-data-node
Deprecated <data> node inside <odoo> xml node"""
for manifest_data in self.manifest_datas:
if not self.is_message_enabled("xml-deprecated-data-node", manifest_data["disabled_checks"]):
continue
for data_node in self.xpath_deprecated_data(manifest_data["node"]):
self.register_error(
code="xml-deprecated-data-node",
message="Deprecated `<data>` node",
info='Use `<odoo>` instead of `<odoo><data>` or `<odoo noupdate="1">` instead of `<odoo><data noupdate="1">`',
filepath=manifest_data["filename_short"],
line=data_node.sourceline,
)
@utils.only_required_for_checks("xml-deprecated-openerp-node")
def check_xml_deprecated_openerp_node(self):
"""* Check xml-deprecated-openerp-node
deprecated <openerp> xml node"""
for manifest_data in self.manifest_datas:
if not self.is_message_enabled("xml-deprecated-openerp-node", manifest_data["disabled_checks"]):
continue
for openerp_node in self.xpath_openerp(manifest_data["node"]):
self.register_error(
code="xml-deprecated-openerp-node",
message="Deprecated `<openerp>` xml node",
info="Use `<odoo>` instead",
filepath=manifest_data["filename_short"],
line=openerp_node.sourceline,
)
@utils.only_required_for_checks("xml-deprecated-qweb-directive")
def check_xml_deprecated_qweb_directive(self):
"""* Check xml-deprecated-qweb-directive
for use of deprecated QWeb directives t-*-options"""
for manifest_data in self.manifest_datas:
if not self.is_message_enabled("xml-deprecated-qweb-directive", manifest_data["disabled_checks"]):
continue
for node in self.xpath_qweb_deprecated(manifest_data["node"]):
directive_str = ", ".join(set(node.attrib) & self.qweb_deprecated_directives)
self.register_error(
code="xml-deprecated-qweb-directive",
message=f"Deprecated QWeb directive `{directive_str}`. Use `t-options` instead",
filepath=manifest_data["filename_short"],
line=node.sourceline,
)
@utils.only_required_for_checks("xml-deprecated-qweb-directive-15")
def check_xml_deprecated_qweb_directives_15(self):
"""* Check xml-deprecated-qweb-directive-15
t-esc and t-raw directives are deprecated in Odoo v15.0, use t-out instead.
For more details https://github.com/odoo/odoo/commit/01875541b1a8131cb and https://github.com/odoo/odoo/pull/70004
"""
if not self.module_version or (self.module_version and self.module_version < Version("15")):
return
for manifest_data in self.manifest_datas:
if not self.is_message_enabled("xml-deprecated-qweb-directive-15", manifest_data["disabled_checks"]):
continue
for node in self.xpath_qweb_deprecated15(manifest_data["node"]):
node_attrs = set(node.attrib)
node_attrs_deprecated = node_attrs & self.qweb_deprecated_directives15
self.register_error(
code="xml-deprecated-qweb-directive-15",
message=f"Deprecated QWeb directive `{', '.join(node_attrs_deprecated)}`. Use `t-out` instead",
filepath=manifest_data["filename_short"],
line=node.sourceline,
)
if self.autofix and "t-out" not in node_attrs:
# TODO: add autofix test
# if t-out already exists, skip autofix
attr_deprecated = next(iter(node_attrs_deprecated))
value_deprecated = node.attrib.get(attr_deprecated)
node_content = node_xml.NodeContent(manifest_data["filename"], node)
pattern = rb"(?P<prefix>\b)" + re.escape(attr_deprecated).encode() + rb'(?P<suffix>\s*=\s*["\'])'
content_node2 = re.sub(pattern, rb"\g<prefix>t-out\g<suffix>", node_content.content_node, count=1)
if content_node2 != node_content.content_node:
# Modify the record attrib to propagate the change to other checks
node_content.content_node = content_node2
node.attrib.pop(attr_deprecated)
node.attrib["t-out"] = value_deprecated
utils.perform_fix(manifest_data["filename"], bytes(node_content))
@utils.only_required_for_checks("xml-xpath-translatable-item")
def check_xml_xpath(self):
"""* Check xml-xpath-translatable-item check `xpath` nodes using `contains(text(), 'Text translatable')`
Since that the text could be translated so it is a mutable value.
It could raise `ValueError` exception if the language is changed.
"""
for manifest_data in self.manifest_datas:
for xpath_node in self.xpath_xpath(manifest_data["node"]):
node_expr = (xpath_node.get("expr") or "").replace(" ", "")
if "[contains(text()" in node_expr or "[text()=" in node_expr:
self.register_error(
code="xml-xpath-translatable-item",
message="Use of translatable xpath `text()`",
filepath=manifest_data["filename_short"],
line=xpath_node.sourceline,
)
@utils.only_required_for_checks("xml-oe-structure-missing-id")
def check_xml_oe_structure(self):
"""* Check xml-oe-structure-missing-id
Ensure all tags with class 'oe_structure' have an ID. For more information on the rationale, see:
https://github.com/OCA/odoo-pre-commit-hooks/issues/27
"""
for manifest_data in self.manifest_datas:
for xpath_node in self.xpath_oe_structure_woid(manifest_data["node"]):
self.register_error(
code="xml-oe-structure-missing-id",
message=(
"Consider removing the class `oe_structure` or adding a proper "
"id to the tag. The id must contain `oe_structure`"
),
filepath=manifest_data["filename_short"],
line=xpath_node.sourceline,
)
@utils.only_required_for_checks("xml-deprecated-oe-chatter")
def check_xml_deprecated_oe_chatter(self):
"""* Check xml-deprecated-oe-chatter
Odoo 18 introduced a new XML tag `<chatter/>` which replaces the old way to declare
chatters on form views. For more information, see:
https://github.com/odoo/odoo/pull/156463
"""
if not self.module_version or (self.module_version and self.module_version < Version("18")):
return
for manifest_data in self.manifest_datas:
for xpath_node in self.xpath_oe_chatter(manifest_data["node"]):
self.register_error(
code="xml-deprecated-oe-chatter",
message=("Please replace old style chatters with the new tag <chatter/>."),
filepath=manifest_data["filename_short"],
line=xpath_node.sourceline,