forked from qgis/QGIS-Django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handler.py
53 lines (43 loc) · 1.32 KB
/
file_handler.py
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
"""
Validator for QLR file.
"""
import re
import xml.etree.ElementTree as ET
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
def parse_qlr(xmlfile):
xmlfile.seek(0)
try:
tree = ET.parse(xmlfile)
except ET.ParseError:
raise ValidationError(_('Cannot parse the qlr file. '
'Please ensure your file is correct.'))
return tree
def validator(xmlfile):
tree = parse_qlr(xmlfile)
root = tree.getroot()
if not root or not root.tag == 'qlr':
raise ValidationError(_('Invalid root tag of qlr file. '
'Please ensure your file is correct.'))
return True
def get_url_datasource(xmlfile):
tree = parse_qlr(xmlfile)
root = tree.getroot()
datasource = root.find('./maplayers/maplayer/datasource')
rgx = r'(?<=url=)[\'"]?(.*?)[\'"&\s]*?$'
try:
url = re.findall(rgx, datasource.text)
except TypeError:
return None
except AttributeError:
return None
result = url[0] if url else None
return result
def get_provider(xmlfile):
tree = parse_qlr(xmlfile)
root = tree.getroot()
provider = root.find('./maplayers/maplayer/provider')
try:
return provider.text
except AttributeError:
return None