Skip to content

Instantly share code, notes, and snippets.

@arbaes
Last active August 13, 2019 23:35
Show Gist options
  • Select an option

  • Save arbaes/825b12c117c4f7bfb7a7b06dc1a39dc3 to your computer and use it in GitHub Desktop.

Select an option

Save arbaes/825b12c117c4f7bfb7a7b06dc1a39dc3 to your computer and use it in GitHub Desktop.
from lxml import etree
from collections import defaultdict
import glob
TARGET_PATH = ['Odoo/odoo/addons/', 'Odoo/enterprise/']
EXCLUDED_TAGS= ['xpath', 'field']
EXCLUDED_IDENTIFIERS = ['t-if', 't']
unsupported_tags = defaultdict(int)
print('Initialize...')
files = []
for path in TARGET_PATH:
files += [f for f in glob.glob(path + "**/*view*.xml", recursive=True)]
print("Found " + str(len(files)) + ' files.')
total_file_replaced_count = 0
total_element_replaced_count = 0
unsupported_count = 0
excluded_count = 0
for f in files:
view = etree.parse(f)
view_root = view.getroot()
replaced_current_file = 0
replaced_element_count = 0
for element in view_root.iter():
if element.tag not in EXCLUDED_TAGS and 'position' in element.attrib and element.attrib['position'] != 'attributes':
if len(element.attrib) != 2:
unsupported_count += 1
print('UNSUPPORTED: '+ str(element.tag) + ' - ' + str(element.attrib) + ' - ' + f)
else:
xpath_position = element.attrib['position']
xpath_tag = element.tag
xpath_identifier = False;
xpath_value = False
for attrib_name, attrib_value in element.attrib.items():
if attrib_name != 'position':
xpath_identifier = attrib_name
xpath_value = attrib_value
break;
if xpath_identifier in EXCLUDED_IDENTIFIERS or '(' in xpath_value:
excluded_count += 1
print('EXCLUDED: '+ str(element.tag) + ' - ' + str(element.attrib) + ' - ' + f)
break;
# BUILD XPATH
if xpath_identifier != 'class':
xpath_expr = '//' + xpath_tag + '[@' + xpath_identifier + "='" + xpath_value +"']"
else:
xpath_expr = '//' + xpath_tag + "[hasclass('"+ xpath_value +"')]"
element.tag = 'xpath'
element.attrib.pop(xpath_identifier)
element.set('position', xpath_position)
element.set('expr', xpath_expr)
replaced_element_count += 1
elif 'position' in element.attrib and element.tag not in EXCLUDED_TAGS:
excluded_count += 1
print('EXCLUDED: '+ str(element.tag) + ' - ' + str(element.attrib) + ' - ' + f)
unsupported_tags[element.tag] += 1
total_element_replaced_count += replaced_element_count
if (replaced_element_count > 0):
view.write(f, encoding="utf-8", xml_declaration=True,)
with open(f, 'a') as fresh_file:
fresh_file.write('\n')
total_file_replaced_count += 1
print('====================================================')
print(str(total_element_replaced_count) + " element(s) xpath'ed")
print(str(total_file_replaced_count) + ' file(s) modified')
print('----------------------------------------------------')
print(str(excluded_count) + ' element(s) excluded, ' + str(unsupported_count) + ' unsupported')
print('Excluded tags:')
print(unsupported_tags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment