fw4spl
check_xml.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 
4 """
5 
6 hooks to check XML syntax
7 
8 [hooks]
9 pretxncommit.check_xml = python:/path-to/hooks:check_xml
10 
11 """
12 
13 from xml.etree import ElementTree as ET
14 
15 import common
16 
17 SEPARATOR = '%s\n' % ('-' * 79)
18 
19 
20 def xml_parser(content):
21  try:
22  tree = ET.fromstring(content)
23  except ET.ParseError as err:
24  line_number, column = err.position
25  line = content.splitlines()[line_number - 2]
26  caret = '{:=>{}}'.format('^', column)
27  err.msg = '{}\n{}\n{}'.format(err, line, caret)
28  raise
29  return tree
30 
31 
32 def check_xml(files):
33  abort = False
34 
35  for f in files:
36  if f.path.lower().endswith(('.xml', '.xsd')):
37  content = f.contents
38  common.trace('Checking ' + str(f.path) + ' syntax...')
39  try:
40  xml_parser(content)
41  except ET.ParseError as err:
42 
43  common.error('XML parsing error in ' + f.path + ' :\n ' + err.msg + '\n.')
44  abort = True
45 
46  return abort
47 
48 
49 hooks = {
50  'check_xml': check_xml,
51 }
def error(msg)
Definition: common.py:52
def trace(msg)
Definition: common.py:47