Skip to content

Commit

Permalink
Support for new upload params in MWDB Core v2.6.0 (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 committed Dec 30, 2021
1 parent 9cb38fe commit 590e3e6
Showing 1 changed file with 91 additions and 14 deletions.
105 changes: 91 additions & 14 deletions mwdblib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ def _upload_params(
self,
parent: Optional[Union[MWDBObject, str]],
metakeys: Optional[Dict[str, Union[str, List[str]]]],
attributes: Optional[Dict[str, Union[Any, List[Any]]]],
tags: Optional[List[str]],
share_with: Optional[str],
private: bool,
public: bool,
Expand All @@ -709,13 +711,23 @@ def _upload_params(
"""
if isinstance(parent, MWDBObject):
parent = parent.id
if metakeys and attributes:
raise ValueError("'attributes' and 'metakeys' must be used exclusively")

metakeys_list = [
# Use attributes if set or metakeys otherwise
_attributes = attributes or metakeys or {}
attributes_list = [
{"key": key, "value": value}
for key, value_list in (metakeys or {}).items()
for key, value_list in _attributes.items()
for value in (value_list if isinstance(value_list, list) else [value_list])
]

_metakeys_param = {"metakeys": attributes_list} if metakeys else {}

_attributes_param = {"attributes": attributes_list} if attributes else {}

_tags_param = {"tags": [{"tag": tag} for tag in tags]} if tags else {}

if len([arg for arg in (share_with, private, public) if arg]) > 1:
raise ValidationError(
"'share_with', 'private' and 'public' arguments are exclusive"
Expand All @@ -728,14 +740,22 @@ def _upload_params(
elif not share_with:
share_with = "*"

return {"parent": parent, "metakeys": metakeys_list, "upload_as": share_with}
return {
"parent": parent,
"upload_as": share_with,
**_tags_param,
**_metakeys_param,
**_attributes_param,
}

def upload_file(
self,
name: str,
content: bytes,
parent: Optional[Union[MWDBObject, str]] = None,
metakeys: Optional[Dict[str, Union[str, List[str]]]] = None,
attributes: Optional[Dict[str, Union[Any, List[Any]]]] = None,
tags: Optional[List[str]] = None,
share_with: Optional[str] = None,
private: bool = False,
public: bool = False,
Expand All @@ -749,9 +769,15 @@ def upload_file(
:type content: bytes
:param parent: Parent object or parent identifier
:type parent: :class:`MWDBObject` or str, optional
:param metakeys: Dictionary with metakeys.
If you want to set many values with the same key: use list as value
:param metakeys: Dictionary with string attributes
(to be used for MWDB Core older than 2.6.0)
:type metakeys: dict, optional
:param attributes: Dictionary with attributes to be set after upload.
If you want to set many values with the same key: use list as value.
Attributes support object values that are JSON-serializable.
:type attributes: dict, optional
:param tags: Dictionary with tags to be set after upload.
:type tags: list, optional
:param share_with: Group name you want to share object with
:type share_with: str, optional
:param private: True if sample should be uploaded as private
Expand All @@ -760,6 +786,11 @@ def upload_file(
:type public: bool, optional
:rtype: :class:`MWDBFile`
.. versionadded:: 4.0.0
Added ``attributes`` and ``tags`` arguments.
They are supported by MWDB Core >= 2.6.0, use ``metakeys``
if your MWDB Core version is older.
Usage example:
.. code-block:: python
Expand All @@ -778,7 +809,13 @@ def upload_file(
None,
json.dumps(
self._upload_params(
parent, metakeys, share_with, private, public
parent=parent,
metakeys=metakeys,
attributes=attributes,
tags=tags,
share_with=share_with,
private=private,
public=public,
)
),
),
Expand All @@ -793,6 +830,8 @@ def upload_config(
config_type: str = "static",
parent: Optional[Union[MWDBObject, str]] = None,
metakeys: Optional[Dict[str, Union[str, List[str]]]] = None,
attributes: Optional[Dict[str, Union[Any, List[Any]]]] = None,
tags: Optional[List[str]] = None,
share_with: Optional[str] = None,
private: bool = False,
public: bool = False,
Expand All @@ -809,17 +848,27 @@ def upload_config(
:type config_type: str, optional
:param parent: Parent object or parent identifier
:type parent: :class:`MWDBObject` or str, optional
:param metakeys: Dictionary with metakeys.
If you want to set many values with the same key: use list as value
:param metakeys: Dictionary with string attributes
(to be used for MWDB Core older than 2.6.0)
:type metakeys: dict, optional
:param attributes: Dictionary with attributes to be set after upload.
If you want to set many values with the same key: use list as value.
Attributes support object values that are JSON-serializable.
:type attributes: dict, optional
:param tags: Dictionary with tags to be set after upload.
:type tags: list, optional
:param share_with: Group name you want to share object with
:type share_with: str, optional
:param private: True if sample should be uploaded as private
:type private: bool, optional
:param public: True if sample should be visible for everyone
:type public: bool, optional
:rtype: :class:`MWDBConfig`
:raises: :class:`requests.exceptions.HTTPError`, :class:`ValueError`
.. versionadded:: 4.0.0
Added ``attributes`` and ``tags`` arguments.
They are supported by MWDB Core >= 2.6.0, use ``metakeys``
if your MWDB Core version is older.
.. code-block:: python
Expand All @@ -838,7 +887,15 @@ def upload_config(
"""
params = {"family": family, "cfg": cfg, "config_type": config_type}
params.update(
self._upload_params(parent, metakeys, share_with, private, public)
self._upload_params(
parent=parent,
metakeys=metakeys,
attributes=attributes,
tags=tags,
share_with=share_with,
private=private,
public=public,
)
)
result = self.api.post("config", json=params)
return MWDBConfig(self.api, result)
Expand All @@ -850,6 +907,8 @@ def upload_blob(
content: str,
parent: Optional[Union[MWDBObject, str]] = None,
metakeys: Optional[Dict[str, Union[str, List[str]]]] = None,
attributes: Optional[Dict[str, Union[Any, List[Any]]]] = None,
tags: Optional[List[str]] = None,
share_with: Optional[str] = None,
private: bool = False,
public: bool = False,
Expand All @@ -865,21 +924,39 @@ def upload_blob(
:type content: str
:param parent: Parent object or parent identifier
:type parent: :class:`MWDBObject` or str, optional
:param metakeys: Dictionary with metakeys.
If you want to set many values with the same key: use list as value
:param metakeys: Dictionary with string attributes
(to be used for MWDB Core older than 2.6.0)
:type metakeys: dict, optional
:param attributes: Dictionary with attributes to be set after upload.
If you want to set many values with the same key: use list as value.
Attributes support object values that are JSON-serializable.
:type attributes: dict, optional
:param tags: Dictionary with tags to be set after upload.
:type tags: list, optional
:param share_with: Group name you want to share object with
:type share_with: str, optional
:param private: True if sample should be uploaded as private
:type private: bool, optional
:param public: True if sample should be visible for everyone
:type public: bool, optional
:rtype: :class:`MWDBBlob`
:raises: :class:`requests.exceptions.HTTPError`, :class:`ValueError`
.. versionadded:: 4.0.0
Added ``attributes`` and ``tags`` arguments.
They are supported by MWDB Core >= 2.6.0, use ``metakeys``
if your MWDB Core version is older.
"""
params = {"blob_name": name, "blob_type": type, "content": content}
params.update(
self._upload_params(parent, metakeys, share_with, private, public)
self._upload_params(
parent=parent,
metakeys=metakeys,
attributes=attributes,
tags=tags,
share_with=share_with,
private=private,
public=public,
)
)
result = self.api.post("blob", json=params)
return MWDBBlob(self.api, result)

0 comments on commit 590e3e6

Please sign in to comment.