-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
73 lines (58 loc) · 2.03 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
63
64
65
66
67
68
69
70
71
72
73
from arcgis.features import FeatureLayer
from datetime import datetime, timezone
def connect(portal, url):
layer = FeatureLayer(url, portal)
# You can look at properties with
# layer.properties
# or get the list of fields with layer.properties.fields
return layer
def s2i(s):
""" Convert a string to an integer even if it has + and , in it. """
# Turns out sometimes there can be text in the string, for example "pending",
# and that made this function crash.
if type(s)==type(0):
# Input is already a number
return s
try:
# Sometimes input has a number with commas in it, remove those.
if s:
return int(float(s.replace(',', '')))
except ValueError:
pass
return None
def local2utc(t):
""" Change a datetime object from local to UTC """
# I'm not sure but maybe I should just set tzinfo here too??
# tzinfo = timezone.utc
return t.astimezone(timezone.utc).replace(microsecond=0, second=0)
# UNIT TESTS
if __name__ == "__main__":
from arcgis.gis import GIS
from config import Config
assert s2i(None) == None
assert s2i("") == None
assert s2i("pending") == None
assert s2i(123) == 123
assert s2i("1,100") == 1100
assert s2i("123456.123456") == 123456
print(local2utc(datetime.now()))
try:
portal = GIS(profile=os.environ.get('USERNAME'))
#print("Logged in as " + str(portal.properties.user.username))
except Exception as e:
print(f"Could not connect to portal. \"{e}\"")
exit(-1)
layers = [
Config.COVID_CASES_URL,
Config.PUBLIC_WEEKLY_URL,
Config.HOSCAP_URL,
Config.PPE_URL,
]
for url in layers:
try:
layer = connect(portal, url)
print("name = '%s'" % layer.properties.name)#, layer.properties.fields)
except Exception as e:
print("Open failed for '%s' : %s" % (url, e))
exit(-1)
# That's all!