Skip to content

Commit

Permalink
Merge pull request #49 from SubstraFoundation/add_permissions_field
Browse files Browse the repository at this point in the history
Adding asset without permissions field - Issue #19
  • Loading branch information
maeldebon authored Dec 5, 2019
2 parents 9044c30 + 6dd5925 commit 8385ae4
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions substra/sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ def _flatten_permissions(data, field_name):
return data


def _update_permissions_field(data, permissions_field='permissions'):
if permissions_field not in data:
default_permissions = {
'public': False,
'authorized_ids': [],
}
data[permissions_field] = default_permissions
# XXX workaround because backend accepts only Form Data body. This is due to
# the fact that backend expects both file objects and payload in the
# same request
data = _flatten_permissions(data, field_name=permissions_field)
return data


class Client(object):

def __init__(self, config_path=None, profile_name=None, user_path=None):
Expand Down Expand Up @@ -121,24 +135,10 @@ def add_profile(self, profile_name, username, password, url, version='0.0', inse
keyring.set_password(profile_name, username, password)
return self._set_current_profile(profile_name, profile)

def _add(self, asset, data, files=None, timeout=False, exist_ok=False,
json_encoding=False, permissions_field='permissions'):
def _add(self, asset, data, files=None, timeout=False, exist_ok=False, json_encoding=False):
"""Add asset."""
data = deepcopy(data) # make a deep copy for avoiding modification by reference
files = files or {}

if permissions_field:
if permissions_field not in data:
default_permissions = {
'public': False,
'authorized_ids': [],
}
data[permissions_field] = default_permissions
# XXX workaround because backend accepts only Form Data body. This is due to
# the fact that backend expects both file objects and payload in the
# same request
data = _flatten_permissions(data, field_name=permissions_field)

requests_kwargs = {}
if files:
requests_kwargs['files'] = files
Expand Down Expand Up @@ -192,6 +192,7 @@ def add_data_sample(self, data, local=True, timeout=False, exist_ok=False):
existing asset will be returned.
"""
data = _update_permissions_field(data)
if 'paths' in data:
raise ValueError("data: invalid 'paths' field")
if 'path' not in data:
Expand Down Expand Up @@ -234,6 +235,7 @@ def add_data_samples(self, data, local=True, timeout=False):
large amount of data it is recommended to add them one by one. It allows a
better control in case of failures.
"""
data = _update_permissions_field(data)
if 'path' in data:
raise ValueError("data: invalid 'path' field")
if 'paths' not in data:
Expand Down Expand Up @@ -263,6 +265,7 @@ def add_dataset(self, data, timeout=False, exist_ok=False):
If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
existing asset will be returned.
"""
data = _update_permissions_field(data)
attributes = ['data_opener', 'description']
with utils.extract_files(data, attributes) as (data, files):
res = self._add(
Expand Down Expand Up @@ -296,6 +299,7 @@ def add_objective(self, data, timeout=False, exist_ok=False):
If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
existing asset will be returned.
"""
data = _update_permissions_field(data)
attributes = ['metrics', 'description']
with utils.extract_files(data, attributes) as (data, files):
res = self._add(
Expand Down Expand Up @@ -326,6 +330,7 @@ def add_algo(self, data, timeout=False, exist_ok=False):
If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
existing asset will be returned.
"""
data = _update_permissions_field(data)
attributes = ['file', 'description']
with utils.extract_files(data, attributes) as (data, files):
res = self._add(
Expand Down Expand Up @@ -353,6 +358,7 @@ def add_aggregate_algo(self, data, timeout=False, exist_ok=False):
If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
existing asset will be returned.
"""
data = _update_permissions_field(data)
attributes = ['file', 'description']
with utils.extract_files(data, attributes) as (data, files):
res = self._add(
Expand Down Expand Up @@ -380,6 +386,7 @@ def add_composite_algo(self, data, timeout=False, exist_ok=False):
If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
existing asset will be returned.
"""
data = _update_permissions_field(data)
attributes = ['file', 'description']
with utils.extract_files(data, attributes) as (data, files):
res = self._add(
Expand Down Expand Up @@ -410,8 +417,7 @@ def add_traintuple(self, data, timeout=False, exist_ok=False):
If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
existing asset will be returned.
"""
res = self._add(assets.TRAINTUPLE, data, timeout=timeout,
exist_ok=exist_ok, permissions_field=None)
res = self._add(assets.TRAINTUPLE, data, timeout=timeout, exist_ok=exist_ok)

# The backend has inconsistent API responses when getting or adding an asset (with much
# less data when responding to adds). A second GET request hides the discrepancies.
Expand Down Expand Up @@ -461,9 +467,8 @@ def add_composite_traintuple(self, data, timeout=False, exist_ok=False):
If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
existing asset will be returned.
"""
res = self._add(assets.COMPOSITE_TRAINTUPLE, data, timeout=timeout,
exist_ok=exist_ok,
permissions_field='out_model_trunk_permissions')
data = _update_permissions_field(data, permissions_field='out_model_trunk_permissions')
res = self._add(assets.COMPOSITE_TRAINTUPLE, data, timeout=timeout, exist_ok=exist_ok)

# The backend has inconsistent API responses when getting or adding an asset (with much
# less data when responding to adds). A second GET request hides the discrepancies.
Expand All @@ -487,8 +492,7 @@ def add_testtuple(self, data, timeout=False, exist_ok=False):
If `exist_ok` is true, `AlreadyExists` exceptions will be ignored and the
existing asset will be returned.
"""
res = self._add(assets.TESTTUPLE, data, timeout=timeout,
exist_ok=exist_ok)
res = self._add(assets.TESTTUPLE, data, timeout=timeout, exist_ok=exist_ok)

# The backend has inconsistent API responses when getting or adding an asset (with much
# less data when responding to adds). A second GET request hides the discrepancies.
Expand Down

0 comments on commit 8385ae4

Please sign in to comment.