Commit 4a14b9bf authored by Vegard Vesterheim's avatar Vegard Vesterheim
Browse files

Run black formatting

parent d7d0ab43
......@@ -14,7 +14,9 @@ import copy
import urllib.parse
# FIXME: why do I need this
sys.path.append(os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), ".."))
)
from kindnavsync.navapi import NAVAPI, ObjectNotFound
# FIXME
......@@ -25,7 +27,7 @@ NAV_API_TIMEOUT = 10.0
# FIXME
# KIND_SERVICE_URL = 'http://localhost:7080/api/nettinstallasjoner.json'
KIND_SERVICE_URL = 'https://kind.uninett.no/api/nettinstallasjoner.json'
KIND_SERVICE_URL = "https://kind.uninett.no/api/nettinstallasjoner.json"
DEFAULT_ORGANIZATION = "uninett"
DEFAULT_LOCATION = "norge"
......@@ -40,28 +42,27 @@ def main():
api_url = args.api_url
api_token = args.api_token
nav_api = NAVAPI(url=api_url, auth_token=api_token,
timeout=NAV_API_TIMEOUT)
nav_api = NAVAPI(url=api_url, auth_token=api_token, timeout=NAV_API_TIMEOUT)
LOG.setLevel(logging.DEBUG if args.debug else logging.INFO)
LOG.addHandler(logging.StreamHandler())
for ni in nettinst:
try:
room_response = nav_api.get_room(ni['navn'])
room_response = nav_api.get_room(ni["navn"])
room = room_response.body
LOG.debug("Fant rom: %s", room)
except ObjectNotFound:
LOG.debug("Creating room for: %s", ni['navn'])
room = dict(id=ni['navn'], location=DEFAULT_LOCATION)
LOG.debug("Creating room for: %s", ni["navn"])
room = dict(id=ni["navn"], location=DEFAULT_LOCATION)
try:
nav_api.post_room(room)
except Exception as e:
LOG.error("Error in creating room for %s: ", ni['navn'])
LOG.error("Error in creating room for %s: ", ni["navn"])
LOG.error("Error is : %s", e)
continue
except Exception as e:
LOG.error("Error in fetching room for %s: ", ni['navn'])
LOG.error("Error in fetching room for %s: ", ni["navn"])
LOG.error("Error: %s", e)
continue
......@@ -70,32 +71,34 @@ def main():
org = {}
# Update room attributes
org['location'] = DEFAULT_LOCATION
org["location"] = DEFAULT_LOCATION
# FIXME, maybe add more into description?
desc_fields = ['beskrivelse', 'termineringsadresse']
description = '. '.join([ni[p] for p in desc_fields if p in ni and ni[p]])
desc_fields = ["beskrivelse", "termineringsadresse"]
description = ". ".join(
[ni[p] for p in desc_fields if p in ni and ni[p]]
)
if not description:
description = 'No description found'
org['description'] = description
if 'org' in ni:
org['data'] = {
'site_owner': ni['org'],
'site_owner_url': 'https://kind.uninett.no/' +
str(int(ni['org_id']))
description = "No description found"
org["description"] = description
if "org" in ni:
org["data"] = {
"site_owner": ni["org"],
"site_owner_url": "https://kind.uninett.no/"
+ str(int(ni["org_id"])),
}
if 'lat' in ni and 'lon' in ni:
org['position'] = [
'%.12f' % round(ni['lat'], 12),
'%.12f' % round(ni['lon'], 12)
if "lat" in ni and "lon" in ni:
org["position"] = [
"%.12f" % round(ni["lat"], 12),
"%.12f" % round(ni["lon"], 12),
]
# Merge data from KIND into existing entry
merge_dict(patch, org)
if DeepDiff(patch, room, ignore_order=True):
LOG.debug("Update needed")
LOG.debug(patch)
nav_api.patch_room(room_id=ni['navn'], patch=patch)
nav_api.patch_room(room_id=ni["navn"], patch=patch)
else:
LOG.debug("No update needed for room: %s", ni['navn'])
LOG.debug("No update needed for room: %s", ni["navn"])
# https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
......@@ -105,7 +108,7 @@ def merge_dict(d, updates):
if isinstance(v, collections.abc.Mapping):
d[k] = merge_dict(d.get(k, {}), v)
else:
if (k not in d):
if k not in d:
LOG.debug("Setting %s to %s", k, v)
d[k] = v
elif d[k] != v:
......@@ -117,7 +120,7 @@ def merge_dict(d, updates):
def get_kind_data(room):
url = KIND_SERVICE_URL
if room:
url = KIND_SERVICE_URL+'?navn='+urllib.parse.quote(room)
url = KIND_SERVICE_URL + "?navn=" + urllib.parse.quote(room)
LOG.debug("Fetching data from: %s", url)
return requests.get(url).json()
......@@ -154,7 +157,6 @@ def parse_args():
"NAV_API_TOKEN environment variable, or the --api-token "
"option"
)
return args
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment