Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
verktoy
kindnavsync
Commits
d7d0ab43
Commit
d7d0ab43
authored
Jan 15, 2021
by
Vegard Vesterheim
Browse files
Flake8 cleanups
parent
5ca2a4ad
Changes
1
Hide whitespace changes
Inline
Side-by-side
bin/nettinst2room
View file @
d7d0ab43
...
...
@@ -15,9 +15,7 @@ import urllib.parse
# FIXME: why do I need this
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
"__file__"
),
'..'
)))
from
kindnavsync.errors
import
retry_on_timeout
from
kindnavsync.navapi
import
NAVAPI
,
ObjectNotFound
,
ClientError
from
kindnavsync.navapi
import
NAVAPI
,
ObjectNotFound
# FIXME
# NAV_API_URL = "http://localhost/api/1"
...
...
@@ -34,14 +32,16 @@ DEFAULT_LOCATION = "norge"
LOG
=
logging
.
getLogger
(
"nettinst2room"
)
def
main
():
args
=
parse_args
()
room
=
args
.
room
nettinst
=
get_kind_data
(
args
.
room
)
api_url
=
args
.
api_url
api_token
=
args
.
api_token
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
:
...
...
@@ -51,34 +51,18 @@ def main():
LOG
.
debug
(
"Fant rom: %s"
,
room
)
except
ObjectNotFound
:
## {
## "id": "vvwashere",
## "position": [
## "59.913054035463",
## "10.720285183010"
## ],
## "description": "Observatoriegata 1b, 0254 Oslo",
## "data": {
## "tlf": "23118900",
## "site_owner": "Norsk kulturråd",
## "site_owner_url": "https://kind.uninett.no/263"
## },
## "location": "norge"
## }
LOG
.
debug
(
"Creating room for: %s"
,
ni
[
'navn'
])
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 is : %s"
,
e
)
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: %s"
,
e
)
LOG
.
error
(
"Error in fetching room for %s: "
,
ni
[
'navn'
])
LOG
.
error
(
"Error: %s"
,
e
)
continue
# Creating a deep copy can make the patch bigger than needed...
...
...
@@ -88,7 +72,7 @@ def main():
# Update room attributes
org
[
'location'
]
=
DEFAULT_LOCATION
# FIXME, maybe add more into description?
desc_fields
=
[
'beskrivelse'
,
'termineringsadresse'
]
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'
...
...
@@ -96,7 +80,8 @@ def main():
if
'org'
in
ni
:
org
[
'data'
]
=
{
'site_owner'
:
ni
[
'org'
],
'site_owner_url'
:
'https://kind.uninett.no/'
+
str
(
int
(
ni
[
'org_id'
]))
'site_owner_url'
:
'https://kind.uninett.no/'
+
str
(
int
(
ni
[
'org_id'
]))
}
if
'lat'
in
ni
and
'lon'
in
ni
:
org
[
'position'
]
=
[
...
...
@@ -104,13 +89,14 @@ def main():
'%.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
)
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
)
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
def
merge_dict
(
d
,
updates
):
...
...
@@ -119,7 +105,7 @@ def merge_dict(d, updates):
if
isinstance
(
v
,
collections
.
abc
.
Mapping
):
d
[
k
]
=
merge_dict
(
d
.
get
(
k
,
{}),
v
)
else
:
if
(
not
k
in
d
):
if
(
k
not
in
d
):
LOG
.
debug
(
"Setting %s to %s"
,
k
,
v
)
d
[
k
]
=
v
elif
d
[
k
]
!=
v
:
...
...
@@ -127,6 +113,7 @@ def merge_dict(d, updates):
d
[
k
]
=
v
return
d
def
get_kind_data
(
room
):
url
=
KIND_SERVICE_URL
if
room
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment