Skip to content

Enhancement/20 allow reading and writing shared config file #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/gridscale_api_client_python.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ rope
black
pytest
pytest-mock
pyyaml
Empty file added examples/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
projects:
- name: default
userId: user123
token: pass123
url: https://api.gridscale.io
- name: something-else
userId: user456
token: pass456
url: https://api.gridscale.io
62 changes: 62 additions & 0 deletions examples/configloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import shutil
import sys
import os.path
import pathlib
import yaml


def default_config_path():
"""

this checks the operation system of the user.
this is used to determine the standard save location for the global gridscale config file.

"""
#check if os is linux
if(sys.platform in ("linux", "linux2")):
path = "~/.config/gridscale"
path = os.path.expanduser(path)
if not os.path.exists(path):
os.makedirs(path)
#check if os is windows
elif(sys.platform in ("win32", "cygwin", "msys")):
path = "%APPDATA%\gridscale"
path = os.path.expanduser(path)
if not os.path.exists(path):
os.makedirs(path)
#check if os is mac os
elif(sys.platform in ("darwin", "os2", "os2emx")):
path = "~/Library/Application Support/gridscale"
path = os.path.expanduser(path)
if not os.path.exists(path):
os.makedirs(path)
else:
raise RuntimeError("Operating system not supported")

return path


def create_config(path):
"""
this will copy the currently used config file in the standard folder
"""
syspath = default_config_path() + "/config.yaml"
shutil.copyfile(path, syspath)


def load_config(path):
"""
First checking "path" to match minimum length and other requirements.

Then it opens the specified config file and returns all keys which include token and UserId.
"""
# opens specified file to retrieve config tokens
if isinstance(path, (pathlib.Path, str)):
assert path
with open(f"{path}", 'r') as stream:
data = yaml.safe_load(stream)
# return list of dictionaries for all projects
for value in data.values():
return (value)
else:
raise AssertionError
13 changes: 8 additions & 5 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from pprint import pprint
from uuid import uuid4
import os
from index_by.key import index_by_key
from configloader import load_config

from gs_api_client import SyncGridscaleApiClient, GridscaleApiClient, models
from gs_api_client import Configuration
Expand All @@ -16,11 +18,12 @@
# api_config.debug = True
api_config.host = 'https://api.gridscale.io'

#TODO: Insert your API token and User ID
api_config.api_key['X-Auth-Token'] = "AUTH_TOKEN"
api_config.api_key['X-Auth-UserId'] = "USER_UUID"
# api_config.debug = True

#TODO: Change filename

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the filename?

configfile = load_config("config.yaml")
api_config.api_key['X-Auth-Token'] = configfile[0].get("token")
api_config.api_key['X-Auth-UserId'] = configfile[0].get("userId")
api_config.debug = True

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shouldn't be debug per default


print('-' * 80)
client = SyncGridscaleApiClient(configuration=api_config, http_info=False)

Expand Down
10 changes: 10 additions & 0 deletions tests/example-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
projects:
- name: default
userId: user123
token: pass123
url: https://api.gridscale.io

- name: something-else
userId: user456
token: pass456
url: https://api.gridscale.io
65 changes: 65 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import os.path
import shutil

import pytest

from gs_api_client import Configuration
from examples.configloader import load_config


CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


def test_debug_is_disabled_by_default():
Expand All @@ -9,3 +18,59 @@ def test_debug_is_disabled_by_default():
def test_tls_certs_are_verified_by_default():
config = Configuration()
assert config.verify_ssl


def test_load_config_from_yaml():
"""Make sure we can load a config from a given YAML file."""

example_config = os.path.join(CURRENT_DIR, "example-config.yaml")
res = load_config(example_config)
assert isinstance(res, list)
assert len(res) == 2
assert isinstance(res[0], dict)
assert res[0]["name"] == "default"
assert res[1]["name"] == "something-else"


def test_load_config_works_without_fileext(tmp_path):
"""Ensure load_config does not interpret file path or file name."""

example_config = os.path.join(CURRENT_DIR, "example-config.yaml")
dest = tmp_path / "a"
shutil.copyfile(example_config, dest)
res = load_config(dest)
assert isinstance(res, list)
assert len(res) == 2


def test_load_config_handles_non_existing_file():
""" "Ensure load_config raises FileNotFoundError."""

with pytest.raises(FileNotFoundError):
load_config("fufu.yaml")


def test_load_config_checks_for_bogus_input():
""" "Ensure load_config checks it's input."""

with pytest.raises(AssertionError):
load_config(42)

with pytest.raises(AssertionError):
load_config("")


def test_load_config_propagates_parsing_errors():
""" "Ensure load_config raises any error during parsing."""

import yaml

not_a_yaml_file = os.path.join(CURRENT_DIR, "test_config.py")
with pytest.raises(yaml.YAMLError):
load_config(not_a_yaml_file)


def test_load_config_has_doc_string():
""" "Make sure load_config is documented."""

assert load_config.__doc__