Skip to content

Commit

Permalink
r.in.wms BaseCapabilitiesTree: fix init ElementTree class (cap file/c…
Browse files Browse the repository at this point in the history
…ap file str arg) (#651)
  • Loading branch information
tmszi committed Jun 13, 2020
1 parent 0bc5ac3 commit 56a497c
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions scripts/r.in.wms/wms_cap_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
@author Stepan Turek <stepan.turek seznam.cz> (Mentor: Martin Landa)
"""
import pathlib

try:
from xml.etree.ElementTree import ParseError
except ImportError: # < Python 2.7
Expand All @@ -30,12 +32,28 @@ class BaseCapabilitiesTree(etree.ElementTree):
def __init__(self, cap_file):
"""!Initialize xml.etree.ElementTree
"""
is_file = False
try:
etree.ElementTree.__init__(self, file=cap_file)
except ParseError:
raise ParseError(_("Unable to parse XML file"))
except IOError as error:
raise ParseError(_("Unable to open XML file '%s'.\n%s\n" % (cap_file, error)))
xml = pathlib.Path(cap_file)
if xml.exists():
is_file = True
except OSError as exc:
if exc.errno == 36: # file name too long
pass
else:
raise
if is_file:
try:
etree.ElementTree.__init__(self, file=cap_file)
except ParseError:
raise ParseError(_("Unable to parse XML file"))
except IOError as error:
raise ParseError(_("Unable to open XML file '%s'.\n%s\n" % (cap_file, error)))
else:
try:
etree.ElementTree.__init__(self, element=etree.fromstring(cap_file))
except ParseError:
raise ParseError(_("Unable to parse XML file"))

if self.getroot() is None:
raise ParseError(_("Root node was not found."))
Expand Down

0 comments on commit 56a497c

Please sign in to comment.