Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
netdb-client-lib
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
scc-net
netvs
netdb-client-lib
Commits
98904594
Commit
98904594
authored
Nov 09, 2020
by
Benedikt Neuffer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Separate base_url and endpoint
This should fix #1.
parent
27141766
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
22 deletions
+48
-22
example_config.ini
example_config.ini
+7
-4
netdb_client/util.py
netdb_client/util.py
+41
-18
No files found.
example_config.ini
View file @
98904594
[DEFAULT]
endpoint
=
www-net.scc.kit.edu
endpoint
=
prod
[www-net-devel.scc.kit.edu]
[dev]
base_url
=
www-net-devel.scc.kit.edu
version
=
3.0
token
=
<token>
[www-net-test.scc.kit.edu]
[test]
base_url
=
www-net-test.scc.kit.edu
version
=
3.0
token
=
<token>
[www-net.scc.kit.edu]
[prod]
base_url
=
www-net.scc.kit.edu
version
=
3.0
token
=
<token>
netdb_client/util.py
View file @
98904594
...
...
@@ -25,10 +25,14 @@ class ArgumentParser(argparse.ArgumentParser):
self
.
add_argument
(
'--auth-config'
,
default
=
os
.
path
.
expanduser
(
'~/.config/netdb_client.ini'
),
help
=
'config file path (default: %(default)s)'
)
self
.
add_argument
(
'--endpoint'
,
'-e'
,
help
=
'endpoint to use.
\n
'
'Environment: "NETDB_ENDPOINT"
\n
'
'Config: [DEFAULT]: endpoint'
)
self
.
add_argument
(
'--base-url'
,
'-b'
,
help
=
'webapi server.
\n
'
'Environment: "NETDB_BASE_URL"
\n
'
'Config: [
DEFAULT]: endpoint
'
)
'Config: [
$endpoint]: base_url
'
)
self
.
add_argument
(
'--version'
,
help
=
'webapi version.
\n
'
'Environment: "NETDB_VERSION"
\n
'
...
...
@@ -47,23 +51,42 @@ class ArgumentParser(argparse.ArgumentParser):
self
.
error
(
f
"Config file is readable by others. Please set it at least to 660."
)
config
.
read
(
configpath
)
def
load_configoption
(
option
):
if
getattr
(
args
,
option
)
is
None
:
setattr
(
args
,
option
,
os
.
getenv
(
f
'NETDB_
{
option
.
upper
()
}
'
,
None
))
if
getattr
(
args
,
option
)
is
None
:
if
option
==
'base_url'
:
setattr
(
args
,
option
,
config
.
get
(
'DEFAULT'
,
'endpoint'
,
fallback
=
None
))
else
:
setattr
(
args
,
option
,
config
.
get
(
args
.
base_url
,
option
,
fallback
=
None
))
if
getattr
(
args
,
option
)
is
None
:
self
.
error
(
f
'No
{
option
}
specified (looked in args, environment variable '
f
'"NETDB_
{
option
.
upper
()
}
", config-file in "
{
args
.
auth_config
}
")'
)
# load base_url first!
for
configoption
in
[
'base_url'
,
'token'
,
'version'
]:
load_configoption
(
configoption
)
def
load_config
(
option
):
"""load config for an option"""
# if value is already set, we can stop here
if
getattr
(
args
,
option
)
is
not
None
:
return
# try to load option from environment
value
=
os
.
getenv
(
f
'NETDB_
{
option
.
upper
()
}
'
,
None
)
if
value
is
None
:
# endpoint should be loaded from DEFAULT section
if
option
==
'endpoint'
:
section
=
'DEFAULT'
# everything else loads from the $endpoint section
else
:
section
=
getattr
(
args
,
'endpoint'
)
# load value from config section
value
=
config
.
get
(
section
,
option
,
fallback
=
None
)
# For backwards compatibility:
# if base_url is still unknown use endpoint instead
if
option
==
'base_url'
and
value
is
None
:
value
=
section
if
value
is
None
:
self
.
error
(
f
'No
{
option
}
specified (looked in args, environment variable '
f
'"NETDB_
{
option
.
upper
()
}
", config-file in "
{
args
.
auth_config
}
section
{
section
}
")'
)
# we have a value now and can use it
setattr
(
args
,
option
,
value
)
# start with endpoint to get the right config section
for
option
in
[
'endpoint'
,
'base_url'
,
'token'
,
'version'
]:
load_config
(
option
)
return
args
...
...
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