Skip to content

Commit

Permalink
Add pre-commit to workflow; Fix endline SVG issue; Add pydocstyle, re…
Browse files Browse the repository at this point in the history
…structuredtext_lint and pygments to docs requirements; add flake8 plugins, black and pylint to linter requirements; update setup.cfg with linter settings, Fix formatting issues in api.py
  • Loading branch information
skasberger committed Jul 9, 2020
1 parent f302562 commit 06e7d20
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 56 deletions.
2 changes: 1 addition & 1 deletion assets/function_schematic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion assets/models_export.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion assets/models_import.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions deps/dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Requirements for development.
tox
wheel
pre-commit
3 changes: 3 additions & 0 deletions deps/docs-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
sphinx==1.7.*
git+https://github.com/python/python-docs-theme.git#egg=python-docs-theme
git+https://github.com/pypa/pypa-docs-theme.git#egg=pypa-docs-theme
pydocstyle
restructuredtext_lint
pygments
20 changes: 15 additions & 5 deletions deps/lint-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Requirements for linting test (tox)
flake8 == 3.7.4
isort == 4.3.4
autopep8
pydocstyle
# Requirements for linting tests (tox)
black==19.10b0
flake8==3.7.4
flake8-docstrings==1.5.0
flake8-rst-docstrings==v0.0.13
flake8-blind-except==v0.1.1
flake8-builtins==1.5.3
flake8-bandit==v2.1.0
flake8-breakpoint==v1.1.0
flake8-bugbear==20.1.4
flake8-comprehensions==3.2.3
flake8-requirements==v1.3.2
flake8-return==0.3.0
flake8-pytest-style==v1.2.1
pylint
radon
19 changes: 19 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,22 @@ show_missing = true

[radon]
cc_min = B

[flake8]
max-line-length = 88
ignore = E129,E203,E501,W503,S101
exclude =
.tox
.egg
conf.py
conftest.py

[pylint]
max-line-length = 88

[pylama:pycodestyle]
max_line_length = 88

[pylama:pylint]
max_line_length = 88
disable = R
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def run_tests(self):
author_email="stefan.kasberger@univie.ac.at",
name="pyDataverse",
version=find_version("src", "pyDataverse", "__init__.py"),
description="A wrapper for the Dataverse API",
description="A Python module for Dataverses API's and its data types",
long_description=read_file("README.md"),
long_description_content_type="text/markdown",
license="MIT",
Expand Down
13 changes: 6 additions & 7 deletions src/pyDataverse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""Find out more at https://github.com/AUSSDA/pyDataverse."""
from __future__ import absolute_import

from requests.packages import urllib3

urllib3.disable_warnings() # noqa
"""Find out more at https://github.com/AUSSDA/pyDataverse.
"""pyDataverse
Copyright 2019 Stefan Kasberger
Licensed under the MIT License.
"""
from __future__ import absolute_import

from requests.packages import urllib3

urllib3.disable_warnings() # noqa

__author__ = "Stefan Kasberger"
__email__ = "stefan.kasberger@univie.ac.at"
Expand Down
89 changes: 49 additions & 40 deletions src/pyDataverse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def __init__(self, base_url, api_token=None, api_version="latest"):
'OK'
"""

if not isinstance(base_url, ("".__class__, u"".__class__)):
raise ApiUrlError("base_url {0} is not a string.".format(base_url))
self.base_url = base_url
Expand Down Expand Up @@ -141,17 +140,19 @@ def get_request(self, url, params=None, auth=False):
params["key"] = str(self.api_token)
else:
raise ApiAuthorizationError(
"ERROR: GET - Api token not passed to "
"`get_request` {}.".format(url)
"ERROR: GET - Api token not passed to `get_request` {0}.".format(
url
)
)

try:
resp = get(url, params=params)
if resp.status_code == 401:
error_msg = resp.json()["message"]
raise ApiAuthorizationError(
"ERROR: GET - Authorization invalid {0}. MSG: {1}."
"".format(url, error_msg)
"ERROR: GET - Authorization invalid {0}. MSG: {1}.".format(
url, error_msg
)
)
elif resp.status_code >= 300:
if resp.text:
Expand All @@ -164,7 +165,7 @@ def get_request(self, url, params=None, auth=False):
return resp
except ConnectionError:
raise ConnectionError(
"ERROR: GET - Could not establish connection to api {}." "".format(url)
"ERROR: GET - Could not establish connection to api {0}.".format(url)
)

def post_request(self, url, data=None, auth=False, params=None, files=None):
Expand Down Expand Up @@ -205,13 +206,14 @@ def post_request(self, url, data=None, auth=False, params=None, files=None):
if resp.status_code == 401:
error_msg = resp.json()["message"]
raise ApiAuthorizationError(
"ERROR: POST HTTP 401 - Authorization error {0}. MSG: {1}"
"".format(url, error_msg)
"ERROR: POST HTTP 401 - Authorization error {0}. MSG: {1}".format(
url, error_msg
)
)
return resp
except ConnectionError:
raise ConnectionError(
"ERROR: POST - Could not establish connection to API: {}" "".format(url)
"ERROR: POST - Could not establish connection to API: {0}".format(url)
)

def put_request(self, url, data=None, auth=False, params=None):
Expand Down Expand Up @@ -242,22 +244,24 @@ def put_request(self, url, data=None, auth=False, params=None):
params["key"] = self.api_token
else:
raise ApiAuthorizationError(
"ERROR: PUT - Api token not passed to "
"`put_request` {}.".format(url)
"ERROR: PUT - Api token not passed to `put_request` {0}.".format(
url
)
)

try:
resp = put(url, data=data, params=params)
if resp.status_code == 401:
error_msg = resp.json()["message"]
raise ApiAuthorizationError(
"ERROR: PUT HTTP 401 - Authorization error {0}. MSG: {1}"
"".format(url, error_msg)
"ERROR: PUT HTTP 401 - Authorization error {0}. MSG: {1}".format(
url, error_msg
)
)
return resp
except ConnectionError:
raise ConnectionError(
"ERROR: PUT - Could not establish connection to api {}." "".format(url)
"ERROR: PUT - Could not establish connection to api {0}.".format(url)
)

def delete_request(self, url, auth=False, params=None):
Expand Down Expand Up @@ -286,16 +290,17 @@ def delete_request(self, url, auth=False, params=None):
params["key"] = self.api_token
else:
raise ApiAuthorizationError(
"ERROR: DELETE - Api token not passed to "
"`delete_request` {}.".format(url)
"ERROR: DELETE - Api token not passed to `delete_request` {}.".format(
url
)
)

try:
resp = delete(url, params=params)
return resp
except ConnectionError:
raise ConnectionError(
"ERROR: DELETE could not establish connection to api {}." "".format(url)
"ERROR: DELETE could not establish connection to api {}.".format(url)
)


Expand All @@ -318,8 +323,7 @@ class DataAccessApi(Api):
"""

def __init__(self, base_url, api_token=None):
"""Init an DataAccessApi() class.
"""
"""Init an DataAccessApi() class."""
super().__init__(base_url, api_token)
if base_url:
self.base_url_api_data_access = "{0}/access".format(self.base_url_api)
Expand Down Expand Up @@ -545,8 +549,7 @@ class MetricsApi(Api):
"""

def __init__(self, base_url, api_token=None, api_version="latest"):
"""Init an MetricsApi() class.
"""
"""Init an MetricsApi() class."""
super().__init__(base_url, api_token, api_version)
if base_url:
self.base_url_api_metrics = "{0}/api/info/metrics".format(self.base_url)
Expand Down Expand Up @@ -751,9 +754,8 @@ def create_dataverse(self, identifier, metadata, auth=True, parent=":root"):
"""
if not parent:
raise DataverseNotFoundError(
"Dataverse {} not found. No parent dataverse passed to `create_dataverse()`.".format(
identifier
)
"Dataverse {} not found. No parent dataverse passed to"
" `create_dataverse()`.".format(identifier)
)

url = "{0}/dataverses/{1}".format(self.base_url_api_native, parent)
Expand Down Expand Up @@ -866,14 +868,16 @@ def delete_dataverse(self, identifier, auth=True):
if resp.status_code == 401:
error_msg = resp.json()["message"]
raise ApiAuthorizationError(
"ERROR: HTTP 401 - Delete Dataverse {0} unauthorized. "
"MSG: {1}".format(identifier, error_msg)
"ERROR: HTTP 401 - Delete Dataverse {0} unauthorized. MSG: {1}".format(
identifier, error_msg
)
)
elif resp.status_code == 404:
error_msg = resp.json()["message"]
raise DataverseNotFoundError(
"ERROR: HTTP 404 - Dataverse {0} was not found. MSG: {1}"
"".format(identifier, error_msg)
"ERROR: HTTP 404 - Dataverse {0} was not found. MSG: {1}".format(
identifier, error_msg
)
)
elif resp.status_code == 403:
error_msg = resp.json()["message"]
Expand All @@ -885,8 +889,9 @@ def delete_dataverse(self, identifier, auth=True):
elif resp.status_code != 200:
error_msg = resp.json()["message"]
raise OperationFailedError(
"ERROR: HTTP {0} - Dataverse {1} could not be deleted. MSG: "
"{2}".format(resp.status_code, identifier, error_msg)
"ERROR: HTTP {0} - Dataverse {1} could not be deleted. MSG: {2}".format(
resp.status_code, identifier, error_msg
)
)
elif resp.status_code == 200:
print("Dataverse {} deleted.".format(identifier))
Expand Down Expand Up @@ -1234,8 +1239,9 @@ def create_dataset(self, dataverse, metadata, auth=True):
if resp.status_code == 404:
error_msg = resp.json()["message"]
raise DataverseNotFoundError(
"ERROR: HTTP 404 - Dataverse {0} was not found. MSG: {1}"
"".format(dataverse, error_msg)
"ERROR: HTTP 404 - Dataverse {0} was not found. MSG: {1}".format(
dataverse, error_msg
)
)
elif resp.status_code == 401:
error_msg = resp.json()["message"]
Expand Down Expand Up @@ -1335,7 +1341,8 @@ def edit_dataset_metadata(
print("Wrong passed data format.")
else:
print(
"You may not add data to a field that already has data and does not allow multiples. Use is_replace=true to replace existing data."
"You may not add data to a field that already has data and does not"
" allow multiples. Use is_replace=true to replace existing data."
)
elif resp.status_code == 200:
print("Dataset {0} updated".format(identifier))
Expand Down Expand Up @@ -1473,8 +1480,9 @@ def publish_dataset(self, pid, type="minor", auth=True):
if resp.status_code == 404:
error_msg = resp.json()["message"]
raise DatasetNotFoundError(
"ERROR: HTTP 404 - Dataset {0} was not found. MSG: {1}"
"".format(pid, error_msg)
"ERROR: HTTP 404 - Dataset {0} was not found. MSG: {1}".format(
pid, error_msg
)
)
elif resp.status_code == 401:
error_msg = resp.json()["message"]
Expand Down Expand Up @@ -1567,8 +1575,9 @@ def delete_dataset(self, identifier, is_pid=True, auth=True):
if resp.status_code == 404:
error_msg = resp.json()["message"]
raise DatasetNotFoundError(
"ERROR: HTTP 404 - Dataset '{0}' was not found. MSG: {1}"
"".format(identifier, error_msg)
"ERROR: HTTP 404 - Dataset '{0}' was not found. MSG: {1}".format(
identifier, error_msg
)
)
elif resp.status_code == 405:
error_msg = resp.json()["message"]
Expand Down Expand Up @@ -2145,7 +2154,8 @@ def get_children(self, parent=":root", parent_type="dataverse", children_types=[
and "datasets" not in children_types
):
print(
"ERROR: Wrong children_types passed: 'dataverses' and 'datafiles' passed, 'datasets' missing."
"ERROR: Wrong children_types passed: 'dataverses' and 'datafiles'"
" passed, 'datasets' missing."
)
return False

Expand Down Expand Up @@ -2232,8 +2242,7 @@ class SearchApi(Api):
"""

def __init__(self, base_url, api_token=None, api_version="latest"):
"""Init an SearchApi() class.
"""
"""Init an SearchApi() class."""
super().__init__(base_url, api_token, api_version)
if base_url:
self.base_url_api_search = "{0}/search?q=".format(self.base_url_api)
Expand Down

0 comments on commit 06e7d20

Please sign in to comment.