@@ -293,7 +293,48 @@ def check_values_selection_field(cr, table_name, field_name, allowed_values):
293293 return res
294294
295295
296- def load_data (env_or_cr , module_name , filename , idref = None , mode = "init" ):
296+ def _file_open (module_name , filename ):
297+ """
298+ Search addons paths and upgrade paths for filename and return the opened file,
299+ raise if not found
300+ """
301+ pathname = os .path .join (module_name , filename )
302+ try :
303+ if version_info [0 ] >= 11 :
304+ fp = tools .file_open (pathname , "rb" )
305+ else :
306+ fp = tools .file_open (pathname )
307+ except OSError :
308+ if tools .config .get ("upgrade_path" ):
309+ if version_info [0 ] >= 19 :
310+ upgrade_paths = tools .config ["upgrade_path" ]
311+ else :
312+ upgrade_paths = tools .config ["upgrade_path" ].split ("," )
313+ for path in upgrade_paths :
314+ pathname = os .path .join (path , module_name , filename )
315+ try :
316+ fp = open (pathname )
317+ break
318+ except OSError : # pylint: disable=W7938
319+ pass
320+ else :
321+ raise OSError (
322+ "Couldn't find file %s in the upgrade paths (%s)"
323+ % (filename , tools .config ["upgrade_path" ])
324+ )
325+ else :
326+ raise
327+ return fp
328+
329+
330+ def load_data (
331+ env_or_cr ,
332+ module_name ,
333+ filename ,
334+ idref = None ,
335+ mode = "init" ,
336+ xml_transformation_filename = None ,
337+ ):
297338 """
298339 Load an xml, csv or yml data file from your post script. The usual case for
299340 this is the
@@ -327,6 +368,18 @@ def load_data(env_or_cr, module_name, filename, idref=None, mode="init"):
327368 update, standard Odoo would recreate the record if it was deleted,
328369 but this will fail in cases where there are required fields to be
329370 filled which are not contained in the data file.
371+ :param xml_transformation_filename:
372+ if filename is an xml file (like the noupdate_changes.xml file generated
373+ by upgrade_analysis), you can pass the name of a transformation file with
374+ view inheritance instructions to transform the file being loaded in-place.
375+
376+ Ie if you pass a file containing::
377+
378+ <odoo>
379+ <xpath expr="//field[@name='uom_id']" position="replace" />
380+ </odoo>
381+
382+ here, you deactivate the first update on a field called `uom_id`.
330383 """
331384
332385 cr = env_or_cr
@@ -342,33 +395,8 @@ def load_data(env_or_cr, module_name, filename, idref=None, mode="init"):
342395 idref = {}
343396 logger .info ("%s: loading %s" % (module_name , filename ))
344397 _ , ext = os .path .splitext (filename )
345- pathname = os .path .join (module_name , filename )
346398
347- try :
348- if version_info [0 ] >= 11 :
349- fp = tools .file_open (pathname , "rb" )
350- else :
351- fp = tools .file_open (pathname )
352- except OSError :
353- if tools .config .get ("upgrade_path" ):
354- if version_info [0 ] >= 19 :
355- upgrade_paths = tools .config ["upgrade_path" ]
356- else :
357- upgrade_paths = tools .config ["upgrade_path" ].split ("," )
358- for path in upgrade_paths :
359- pathname = os .path .join (path , module_name , filename )
360- try :
361- fp = open (pathname )
362- break
363- except OSError : # pylint: disable=W7938
364- pass
365- else :
366- raise OSError (
367- "Couldn't find file %s in the upgrade paths (%s)"
368- % (filename , tools .config ["upgrade_path" ])
369- )
370- else :
371- raise
399+ fp = _file_open (module_name , filename )
372400
373401 try :
374402 if ext == ".csv" :
@@ -388,7 +416,17 @@ def load_data(env_or_cr, module_name, filename, idref=None, mode="init"):
388416 mode = "init" ,
389417 )
390418 else :
391- tools .convert_xml_import (env_or_cr , module_name , fp , idref , mode = mode )
419+ tools .convert_xml_import (
420+ env_or_cr ,
421+ module_name ,
422+ fp
423+ if not xml_transformation_filename
424+ else _load_data_xml_transformation (
425+ fp , _file_open (module_name , xml_transformation_filename )
426+ ),
427+ idref ,
428+ mode = mode ,
429+ )
392430 finally :
393431 fp .close ()
394432
@@ -428,6 +466,24 @@ def yield_element(node, path=None):
428466 return yield_element (etree .parse (fp ).getroot ())
429467
430468
469+ def _load_data_xml_transformation (fp , transformation_fp ):
470+ """
471+ Apply xml_transformation (string of view inheritance style snippets) to xml file
472+ fp, return file-like object that can be loaded with etree.parse
473+ """
474+ from odoo .tools .template_inheritance import apply_inheritance_specs
475+
476+ transformation_arch = etree .parse (transformation_fp )
477+
478+ modified = apply_inheritance_specs (
479+ etree .parse (fp ).getroot (),
480+ transformation_arch .getroot ().getchildren (),
481+ )
482+ result = StringIO (etree .tostring (modified , encoding = "unicode" ))
483+ result .name = None
484+ return result
485+
486+
431487# for backwards compatibility
432488load_xml = load_data
433489table_exists = openupgrade_tools .table_exists
0 commit comments