Skip to content

Commit

Permalink
Merge updates
Browse files Browse the repository at this point in the history
* add black
* update tox.ini
* re-structure requirements
* add pre-commit config
* change to_json() to json()
* update tests to the changes
  • Loading branch information
skasberger committed Dec 7, 2020
1 parent 4aef93b commit fc1a711
Show file tree
Hide file tree
Showing 25 changed files with 632 additions and 558 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ setup.sh
.pypirc
data/
dev/
internal/
internal
*.code-workspace
.python-version
devel.py

# Distribution / packaging
build/
Expand Down
36 changes: 1 addition & 35 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
default_language_version:
python: python3
exclude: ^migrations/
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
Expand All @@ -26,38 +27,3 @@ repos:
hooks:
- id: blacken-docs
additional_dependencies: [black==19.10b0]
# - repo: local
# hooks:
# - id: rst
# name: rst
# entry: rst-lint --encoding utf-8
# language: python
# types: [rst]
# additional_dependencies: [pygments, restructuredtext_lint]
# - repo: https://github.com/pycqa/flake8
# rev: 3.8.3
# hooks:
# - id: flake8
# alias: flake8
# language_version: python3
# args: [--exclude=tests/test_*.py,src/pyDataverse/docs/.py]
# additional_dependencies: [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]
# - id: flake8
# alias: flake8_docs
# language_version: python3
# args: [--docstring-convention numpy]
# additional_dependencies: [flake8-docstrings==1.5.0,flake8-rst-docstrings==v0.0.13]
# - id: flake8
# alias: flake8_tests
# language_version: python3
# args: [--exclude=src/pyDataverse/*.py,src/pyDataverse/docs/.py]
# additional_dependencies: [flake8-pytest-style==v1.2.1,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]
# - repo: https://github.com/PyCQA/pylint
# rev: pylint-2.5.3
# hooks:
# - id: pylint
# exclude: conf.py
# - repo: https://github.com/gvanderest/pylama-pre-commit
# rev: 0.1.2
# hooks:
# - id: pylama
2 changes: 2 additions & 0 deletions requirements/common.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests>=2.12.0
jsonschema>=3.2.0
6 changes: 5 additions & 1 deletion requirements/development.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Requirements for development.
tox
-r common.txt
-r lint.txt
-r docs.txt
-r tests.txt
wheel
pre-commit
twine
1 change: 1 addition & 0 deletions requirements/docker.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-r common.txt
2 changes: 2 additions & 0 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Requirements for documentation test (tox)
-r common.txt
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
rstcheck
2 changes: 2 additions & 0 deletions requirements/lint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ flake8-return==0.3.0
flake8-pytest-style==v1.2.1
pylint
radon
mypy==0.790
autopep8
1 change: 1 addition & 0 deletions requirements/production.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-r common.txt
23 changes: 6 additions & 17 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
# Requirements for tests (tox)
coverage==4.5.3
atomicwrites==1.3.0
attrs==19.1.0
certifi==2019.3.9
chardet==3.0.4
idna==2.8
importlib-metadata==0.15
more-itertools==5.0.0
pluggy==0.12.0
py==1.8.0
pytest==4.5.0
pytest-cov==2.7.1
six==1.12.0
urllib3==1.25.3
wcwidth==0.1.7
zipp==0.5.1
# Requirements for tests (pytest)
-r common.txt
pytest
pytest-cov
tox
selenium==3.141.0
19 changes: 0 additions & 19 deletions src/pyDataverse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,6 @@ def __init__(self, base_url, api_token=None, api_version="latest"):
)
else:
self.base_url_api = None

# try:
# resp = self.get_info_version()
# if 'data' in resp.json().keys():
# if 'version' in resp.json()['data'].keys():
# self.dataverse_version = resp.json()['data']['version']
# else:
# # TODO: raise exception
# self.dataverse_version = None
# print('Key not in response.')
# else:
# self.dataverse_version = None
# # TODO: raise exception
# print('Key not in response.')
# except:
# self.dataverse_version = None
# # TODO: raise exception
# print('Dataverse build version can not be retrieved.')

self.timeout = 500

def __str__(self):
Expand Down
1 change: 0 additions & 1 deletion src/pyDataverse/docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

sys.path.insert(0, os.path.abspath("../../.."))


project = "pyDataverse"
author = "Stefan Kasberger"
author_affiliation = "AUSSDA - The Austrian Social Science Data Archive"
Expand Down
46 changes: 31 additions & 15 deletions src/pyDataverse/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ def validate_json(self, filename_schema=None):
"""
if filename_schema is None:
filename_schema = os.path.join(os.path.dirname(os.path.realpath(__file__)), self._default_json_schema_filename)
filename_schema = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
self._default_json_schema_filename,
)
assert isinstance(filename_schema, str)

return validate_data(
json.loads(self.to_json(validate=False)),
filename_schema,
file_format="json",
json.loads(self.json(validate=False)), filename_schema, file_format="json",
)

def from_json(
Expand Down Expand Up @@ -142,7 +143,10 @@ def from_json(
assert isinstance(data_format, str)
assert data_format in self._allowed_json_formats
if filename_schema is None:
filename_schema = os.path.join(os.path.dirname(os.path.realpath(__file__)), self._default_json_schema_filename)
filename_schema = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
self._default_json_schema_filename,
)
assert isinstance(filename_schema, str)

data = {}
Expand Down Expand Up @@ -172,7 +176,7 @@ def from_json(

self.set(data)

def to_json(self, data_format=None, validate=True, filename_schema=None):
def json(self, data_format=None, validate=True, filename_schema=None):
r"""Create JSON from :class:`DVObject` attributes.
Parameters
Expand All @@ -197,7 +201,10 @@ def to_json(self, data_format=None, validate=True, filename_schema=None):
assert isinstance(data_format, str)
assert data_format in self._allowed_json_formats
if filename_schema is None:
filename_schema = os.path.join(os.path.dirname(os.path.realpath(__file__)), self._default_json_schema_filename)
filename_schema = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
self._default_json_schema_filename,
)
assert isinstance(filename_schema, str)

data = {}
Expand Down Expand Up @@ -233,7 +240,7 @@ class Dataverse(DVObject):
_allowed_json_formats : list
List of all possible JSON data formats.
_json_dataverse_upload_attr : list
List of all attributes to be exported in :func:`to_json`.
List of all attributes to be exported in :func:`json`.
"""

def __init__(self, data=None):
Expand Down Expand Up @@ -288,7 +295,7 @@ class Dataset(DVObject):
_allowed_json_formats : list
List of all possible JSON data formats.
_json_dataverse_upload_attr : list
List with all attributes to be exported in :func:`to_json`.
List with all attributes to be exported in :func:`json`.
__attr_import_dv_up_datasetVersion_values : list
Dataverse API Upload Dataset JSON attributes inside ds[\'datasetVersion\'].
__attr_import_dv_up_citation_fields_values : list
Expand Down Expand Up @@ -665,12 +672,15 @@ def validate_json(self, filename_schema=None):
"""
if filename_schema is None:
filename_schema = os.path.join(os.path.dirname(os.path.realpath(__file__)), self._default_json_schema_filename)
filename_schema = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
self._default_json_schema_filename,
)
assert isinstance(filename_schema, str)

is_valid = True

data_json = self.to_json(validate=False)
data_json = self.json(validate=False)
if data_json:
is_valid = validate_data(
json.loads(data_json), filename_schema, file_format="json"
Expand Down Expand Up @@ -830,7 +840,10 @@ def from_json(
assert isinstance(data_format, str)
assert data_format in self._allowed_json_formats
if filename_schema is None:
filename_schema = os.path.join(os.path.dirname(os.path.realpath(__file__)), self._default_json_schema_filename)
filename_schema = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
self._default_json_schema_filename,
)
assert isinstance(filename_schema, str)

data = {}
Expand Down Expand Up @@ -1109,7 +1122,7 @@ def __generate_field_arrays(self, key, sub_keys):
assert isinstance(tmp_list, list)
return tmp_list

def to_json(self, data_format=None, validate=True, filename_schema=None):
def json(self, data_format=None, validate=True, filename_schema=None):
"""Create Dataset JSON from attributes.
Parameters
Expand All @@ -1134,7 +1147,10 @@ def to_json(self, data_format=None, validate=True, filename_schema=None):
assert isinstance(data_format, str)
assert data_format in self._allowed_json_formats
if filename_schema is None:
filename_schema = os.path.join(os.path.dirname(os.path.realpath(__file__)), self._default_json_schema_filename)
filename_schema = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
self._default_json_schema_filename,
)
assert isinstance(filename_schema, str)

data = {}
Expand Down Expand Up @@ -1484,7 +1500,7 @@ class Datafile(DVObject):
_allowed_json_formats : list
List of all possible JSON data formats.
_json_dataverse_upload_attr : list
List of all attributes to be exported in :func:`to_json`.
List of all attributes to be exported in :func:`json`.
"""

def __init__(self, data=None):
Expand Down
10 changes: 3 additions & 7 deletions src/pyDataverse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def write_file(filename, data, mode="w", encoding="utf-8"):
f.write(data)


def read_json(filename, mode="r", encoding="utf-8"):
def read_json(filename: str, mode: str = "r", encoding: str = "utf-8") -> dict:
"""Read in a json file.
See more about the json module at
Expand All @@ -85,14 +85,10 @@ def read_json(filename, mode="r", encoding="utf-8"):
Data as a json-formatted string.
"""
assert isinstance(filename, str)
assert isinstance(mode, str)
assert isinstance(encoding, str)

# TODO: add kwargs
with open(filename, mode, encoding=encoding) as f:
data = json.load(f)

assert isinstance(data, dict)
return data


Expand All @@ -113,7 +109,7 @@ def write_json(filename, data, mode="w", encoding="utf-8"):
"""
assert isinstance(filename, str)
assert isinstance(data, str)
assert isinstance(data, dict)
assert isinstance(mode, str)
assert isinstance(encoding, str)

Expand Down
Empty file added tests/api/__init__.py
Empty file.
Loading

0 comments on commit fc1a711

Please sign in to comment.