-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
62 lines (47 loc) · 1.84 KB
/
utils.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
54
55
56
57
58
59
60
61
62
import sys
import unicodedata
from dwca.darwincore.utils import qualname as qn
def valid_dwca(dwca):
return (dwca.core_rowtype == qn('Occurrence') and
dwca.core_contains_term(qn('decimalLatitude')) and
dwca.core_contains_term(qn('decimalLongitude')))
class CannotConvertException(Exception):
pass
def dwcaline_to_epsg4326(line):
""" Returns a {'lat': X, 'lon': Y} dict for the given DwCALine. """
try:
lat = float(line.data[qn('decimalLatitude')])
lon = float(line.data[qn('decimalLongitude')])
except ValueError:
raise CannotConvertException()
return {'lat': lat, 'lon': lon}
def unicode_to_ascii(string):
return unicodedata.normalize('NFKD', string).encode('ascii', 'ignore')
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is one of "yes" or "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "\
"(or 'y' or 'n').\n")