Skip to content
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

upload_datafile: handling of the content (mime) type #142

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bb4f1f1
Merge branch 'master' into develop
skasberger Jan 27, 2021
5ff653c
Merge branch 'master' into develop
skasberger Feb 15, 2021
3514716
move tox requirement to development.txt
atrisovic Feb 17, 2021
4edaac3
Fixing API endpoint to get user details
ecowan Feb 19, 2021
d14be31
Merge branch 'atrisovic-tox' into develop
skasberger Feb 24, 2021
857f879
Merge branch 'master' into develop
skasberger Feb 24, 2021
899faae
Merge branch 'user_endpoint' of https://github.com/opendp/pyDataverse…
skasberger Feb 25, 2021
9c8c071
Merge branch 'opendp-user_endpoint' into develop
skasberger Feb 25, 2021
038f36a
Merge branch 'master' into develop
skasberger Apr 6, 2021
abf42e0
add codecov instead of coveralls
skasberger Apr 7, 2021
9f7cda2
fix issues for codecov
skasberger Apr 7, 2021
e55d750
fix issues for codecov
skasberger Apr 7, 2021
bd10a93
fix issue: codecov
skasberger Apr 7, 2021
0b9de59
fix issue: codecov #5
skasberger Apr 7, 2021
7cf1982
fix issue: codecov #6
skasberger Apr 7, 2021
65e3231
fix issue: codecov #7
skasberger Apr 7, 2021
38f675b
fix issue: codecov #8
skasberger Apr 7, 2021
6ecb1af
fix issue: codecov #9
skasberger Apr 7, 2021
112d9ed
fix issue: codecov #10
skasberger Apr 7, 2021
d067e03
fix issue: codecov #11
skasberger Apr 7, 2021
9bd2f6a
add codecov and update tox
skasberger Apr 7, 2021
d38461a
Update pre-commit
skasberger Apr 7, 2021
6478ede
fix minor docs issues
skasberger Apr 7, 2021
6dddd4f
fix issue: flake8 exlude filenames without dir
skasberger Apr 12, 2021
cc06022
small fixes in requirements
skasberger Apr 12, 2021
628dbb4
Added a way to supply the mime type to upload_datafile(); and made th…
landreev Jan 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .coveragerc

This file was deleted.

9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ repos:
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
- id: fix-encoding-pragma
args: [--remove]
- id: pretty-format-json
args: [--autofix,--no-sort-keys]
- id: requirements-txt-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
Expand All @@ -27,3 +31,8 @@ repos:
hooks:
- id: blacken-docs
additional_dependencies: [black==19.10b0]
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.4.0
hooks:
- id: reorder-python-imports
args: ['--application-directories=.:src', --py36-plus]
4 changes: 3 additions & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ sphinx:
configuration: src/pyDataverse/docs/source/conf.py

# Optionally build your docs in additional formats such as PDF and ePub
formats: all
formats:
- epub
- pdf

# Optionally set the version of Python and requirements required to build your docs
python:
Expand Down
10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ matrix:
env: TOXENV=py38
- python: 3.6
env: TOXENV=docs
- python: 3.6
env: TOXENV=coverage
- python: 3.6
env: TOXENV=coveralls

branches:
only:
Expand All @@ -26,9 +22,9 @@ before_install:
- echo $TRAVIS_PYTHON_VERSION

install:
- pip install --upgrade pip
- pip install tox-travis
- pip install coverage
- pip install coveralls
- pip install codecov
- virtualenv --version
- easy_install --version
- pip --version
Expand All @@ -38,7 +34,7 @@ script:
- tox

after_success:
- coveralls
- codecov

notifications:
email:
Expand Down
7 changes: 2 additions & 5 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ you created the branch, check the section on
:ref:`updating a PR <contributing_changes_update-pull-request>`.


From here, you now can move forward to
From here, you now can move forward to

- contribute to the documentation (see below)
- contribute to the :ref:`code base <contributing_code>`
Expand Down Expand Up @@ -267,7 +267,7 @@ Some other important things to know about the docs:
- The pyDataverse documentation consists of two parts:

- the docstrings in the code itself and
- the docs in the folder ``src/pyDataverse/doc/``
- the docs in the folder ``src/pyDataverse/docs/``

- The docstrings provide a clear explanation of the usage of the individual functions, while the documentation consists of tutorial-like overviews per topic together with some other information (what’s new, installation, this page you're viewing right now, etc).
- The docstrings follow the `Numpy Docstring Standard <https://numpydoc.readthedocs.io/en/latest/format.html>`_.
Expand Down Expand Up @@ -871,6 +871,3 @@ You can find the full release history at :ref:`community_history` and on
**Versioning**

For pyDataverse, `Semantic versioning <https://semver.org/>`_ is used for releases.


.. _contributing_resources:
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
pyDataverse is a Python module for [Dataverse](http://dataverse.org).
It helps to access the Dataverse [API's](http://guides.dataverse.org/en/latest/api/index.html) and manipulate, validate, import and export all Dataverse data-types (Dataverse, Dataset, Datafile).

**Find out more: [Read the Docs](https://pydataverse.readthedocs.io/en/latest/)**
**Find out more: [pyDataverse Documentation](https://pydataverse.readthedocs.io/en/latest/)**
2 changes: 1 addition & 1 deletion requirements/common.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
requests>=2.12.0
jsonschema>=3.2.0
requests>=2.12.0
7 changes: 3 additions & 4 deletions requirements/development.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Requirements for development.
-r common.txt
-r lint.txt
-r docs.txt
-r lint.txt
-r packaging.txt
-r tests.txt
wheel
pre-commit
twine
wheel
8 changes: 3 additions & 5 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# 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
git+https://github.com/python/python-docs-theme.git#egg=python-docs-theme
pydocstyle
restructuredtext_lint
pygments
restructuredtext_lint
rstcheck
sphinx==1.7.*
17 changes: 8 additions & 9 deletions requirements/lint.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Requirements for linting tests (tox)
autopep8
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-blind-except==v0.1.1
flake8-breakpoint==v1.1.0
flake8-bugbear==20.1.4
flake8-builtins==1.5.3
flake8-comprehensions==3.2.3
flake8-requirements==v1.3.2
flake8-return==0.3.0
flake8-docstrings==1.5.0
flake8-pytest-style==v1.2.1
flake8-requirements==v1.3.2
flake8-rst-docstrings==v0.0.13
mypy==0.790
Pygments
pylint
radon
mypy==0.790
autopep8
1 change: 0 additions & 1 deletion requirements/packaging.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# Requirements for packaging test (tox)
twine
5 changes: 2 additions & 3 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Requirements for tests (pytest)
-r common.txt
codecov
pytest
pytest-cov
tox
selenium==3.141.0
tox
38 changes: 21 additions & 17 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,36 @@ license_file = LICENSE.txt

[tool:pytest]
testpaths = tests

[coverage:run]
source = wheel
omit = .tox/*

[coverage:report]
show_missing = true

[radon]
cc_min = B
addopts = -v

[flake8]
max-line-length = 88
ignore = E129,E203,E501,W503,S101
exclude =
.tox
.egg
src/pyDataverse/docs
src/pyDataverse/schemas
src/pyDataverse/templates
conf.py
conftest.py
__pycache__
.mypy_cache
.tox
.egg

[pylint]
max-line-length = 88

[pylama:pycodestyle]
max_line_length = 88
[coverage:run]
source=pyDataverse

[coverage:report]
show_missing = true

[coverage:html]
directory = docs/coverage/html

[coverage:xml]
output = coverage.xml

[pylama:pylint]
max_line_length = 88
disable = R
[radon]
cc_min = B
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import codecs
import os
import re
from setuptools.command.test import test as TestCommand
import sys

from setuptools import find_packages
from setuptools import setup
import sys
from setuptools.command.test import test as TestCommand

ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
Expand Down
2 changes: 0 additions & 2 deletions src/pyDataverse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

Licensed under the MIT License.
"""
from __future__ import absolute_import

from requests.packages import urllib3

urllib3.disable_warnings() # noqa
Expand Down
30 changes: 18 additions & 12 deletions src/pyDataverse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
import json
import subprocess as sp

from requests import ConnectionError, Response, delete, get, post, put
from requests import ConnectionError
from requests import delete
from requests import get
from requests import post
from requests import put
from requests import Response

from pyDataverse.exceptions import (
ApiAuthorizationError,
ApiUrlError,
DatasetNotFoundError,
DataverseNotEmptyError,
DataverseNotFoundError,
OperationFailedError,
)
from pyDataverse.exceptions import ApiAuthorizationError
from pyDataverse.exceptions import ApiUrlError
from pyDataverse.exceptions import DatasetNotFoundError
from pyDataverse.exceptions import DataverseNotEmptyError
from pyDataverse.exceptions import DataverseNotFoundError
from pyDataverse.exceptions import OperationFailedError


class Api:
Expand Down Expand Up @@ -1639,7 +1642,7 @@ def get_datafile_metadata(
# CHECK: Its not really clear, if the version query can also be done via ID.
return self.get_request(url, auth=auth)

def upload_datafile(self, identifier, filename, json_str=None, is_pid=True):
def upload_datafile(self, identifier, filename, json_str=None, is_pid=True, content_type='application/octet-stream'):
"""Add file to a dataset.

Add a file to an existing Dataset. Description and tags are optional:
Expand All @@ -1666,6 +1669,8 @@ def upload_datafile(self, identifier, filename, json_str=None, is_pid=True):
Metadata as JSON string.
is_pid : bool
``True`` to use persistent identifier. ``False``, if not.
content_type : str
MIME type. Defaults to ``application/octet-stream``; this will prompt Dataverse to attempt to identify the MIME type of the file more accurately.

Returns
-------
Expand All @@ -1680,7 +1685,7 @@ def upload_datafile(self, identifier, filename, json_str=None, is_pid=True):
else:
url += "/datasets/{0}/add".format(identifier)

files = {"file": open(filename, "rb")}
files = {"file": (filename, open(filename, "rb"), content_type)}
return self.post_request(
url, data={"jsonData": json_str}, files=files, auth=True
)
Expand Down Expand Up @@ -2083,6 +2088,7 @@ def get_children(
- Unify tree and models

"""
# print(self.base_url_api)
children = []

if children_types is None:
Expand Down Expand Up @@ -2181,7 +2187,7 @@ def get_user(self):

https://guides.dataverse.org/en/latest/api/native-api.html#get-user-information-in-json-format
"""
url = f"{self.base_url}/users/:me"
url = f"{self.base_url_api_native}/users/:me"
return self.get_request(url, auth=True)

def redetect_file_type(
Expand Down
9 changes: 3 additions & 6 deletions src/pyDataverse/docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# sys.path.insert(0, os.path.abspath('../../'))

# -- Project information -----------------------------------------------------

import pyDataverse
from datetime import date
import os
import sys
from datetime import date

import pyDataverse

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

Expand Down
2 changes: 1 addition & 1 deletion src/pyDataverse/docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Release v\ |version|.

.. _homepage_description:

**pyDataverse** is a Python module for `Dataverse <http://dataverse.org>`_ you can use for:
**pyDataverse** is a Python module for `Dataverse <http://dataverse.org>`_ you can use for:

- accessing the Dataverse `API's <http://guides.dataverse.org/en/latest/api/index.html>`_
- manipulating and using the Dataverse (meta)data - Dataverses, Datasets, Datafiles
Expand Down
8 changes: 8 additions & 0 deletions src/pyDataverse/docs/source/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Where pyDataverse depends on external libraries, we document the most
important right here and provide links to the canonical documentation outside of scope.


.. _reference_api:

API Interface
-----------------------------

Expand All @@ -19,6 +21,8 @@ Access all of Dataverse APIs.
:members:


.. _reference_models:

Models Interface
-----------------------------

Expand All @@ -29,6 +33,8 @@ and `Datafile`). This includes import, export and manipulation.
:inherited-members:


.. _reference_utils:

Utils Interface
-----------------------------

Expand All @@ -38,6 +44,8 @@ Helper functions.
:members:


.. _reference_exceptions:

Exceptions
-----------------------------

Expand Down
4 changes: 2 additions & 2 deletions src/pyDataverse/docs/source/user/advanced-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ converts boolean values, and loads JSON cells properly.
Once we have the data in Python, we can easily import the data into
pyDataverse.

For this, loop over each Dataset :class:`dict`, to:
For this, loop over each Dataset :class:`dict`, to:

#. Instantiate an empty :class:`Dataset <pyDataverse.models.Dataset>`
#. add the data with :meth:`set() <pyDataverse.models.Dataset.set>` and
#. add the data with :meth:`set() <pyDataverse.models.Dataset.set>` and
#. append the instance to a :class:`list`.

::
Expand Down
Loading