Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist/
*.xml
htmlcov/
.pylint.d/
.cache/
5 changes: 5 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ tasks:
- rm -rf build
- rm -f dist/*
- python3 setup.py bdist_wheel

format:
desc: Format code using black
cmds:
- ./scripts/builder.sh black archivist examples unittests
106 changes: 53 additions & 53 deletions archivist/access_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,119 +10,119 @@
ASSETS_LABEL,
)

DEFAULT_PAGE_SIZE=500
DEFAULT_PAGE_SIZE = 500


class _AccessPoliciesClient:
"""docstring
"""
"""docstring"""

def __init__(self, archivist):
"""docstring
"""
"""docstring"""
self._archivist = archivist

def create(self, request):
"""docstring
"""
"""docstring"""

return AccessPolicy(**self._archivist.post(
f"{ACCESS_POLICIES_SUBPATH}/{ACCESS_POLICIES_LABEL}",
request,
))
return AccessPolicy(
**self._archivist.post(
f"{ACCESS_POLICIES_SUBPATH}/{ACCESS_POLICIES_LABEL}",
request,
)
)

def read(self, identity):
"""docstring
"""
return AccessPolicy(**self._archivist.get(
ACCESS_POLICIES_SUBPATH,
identity,
))
"""docstring"""
return AccessPolicy(
**self._archivist.get(
ACCESS_POLICIES_SUBPATH,
identity,
)
)

def update(self, identity, request):
"""docstring
"""
return AccessPolicy(**self._archivist.patch(
ACCESS_POLICIES_SUBPATH,
identity,
request,
))
"""docstring"""
return AccessPolicy(
**self._archivist.patch(
ACCESS_POLICIES_SUBPATH,
identity,
request,
)
)

def delete(self, identity):
"""docstring
"""
"""docstring"""
return self._archivist.delete(ACCESS_POLICIES_SUBPATH, identity)

@staticmethod
def __query(props):
"""docstring
"""
"""docstring"""
query = props or {}
return query

def count(self, *, query=None):
"""docstring
"""
"""docstring"""
return self._archivist.count(
f"{ACCESS_POLICIES_SUBPATH}/{ACCESS_POLICIES_LABEL}",
query=self.__query(query)
query=self.__query(query),
)

def list(self, *, page_size=DEFAULT_PAGE_SIZE, query=None):
"""docstring
"""
"""docstring"""
return (
AccessPolicy(**a) for a in self._archivist.list(
AccessPolicy(**a)
for a in self._archivist.list(
f"{ACCESS_POLICIES_SUBPATH}/{ACCESS_POLICIES_LABEL}",
ACCESS_POLICIES_LABEL,
page_size=page_size,
query=self.__query(query)
query=self.__query(query),
)
)

# additional queries on different endpoints
def count_matching_assets(self, access_policy_id, *, query=None):
"""docstring
"""
"""docstring"""
return self._archivist.count(
SEP.join((ACCESS_POLICIES_SUBPATH, access_policy_id, ASSETS_LABEL)),
ASSETS_LABEL,
query=self.__query(query)
query=self.__query(query),
)

def list_matching_assets(self, access_policy_id, *, page_size=DEFAULT_PAGE_SIZE, query=None):
"""docstring
"""
def list_matching_assets(
self, access_policy_id, *, page_size=DEFAULT_PAGE_SIZE, query=None
):
"""docstring"""
return (
AccessPolicy(**a) for a in self._archivist.list(
AccessPolicy(**a)
for a in self._archivist.list(
SEP.join((ACCESS_POLICIES_SUBPATH, access_policy_id, ASSETS_LABEL)),
ASSETS_LABEL,
page_size=page_size,
query=self.__query(query)
query=self.__query(query),
)
)

def count_matching_access_policies(self, asset_id, *, query=None):
"""docstring
"""
"""docstring"""
return self._archivist.count(
SEP.join((ACCESS_POLICIES_SUBPATH, asset_id, ACCESS_POLICIES_LABEL)),
ACCESS_POLICIES_LABEL,
query=self.__query(query)
query=self.__query(query),
)

def list_matching_access_policies(self, asset_id, *, page_size=DEFAULT_PAGE_SIZE, query=None):
"""docstring
"""
def list_matching_access_policies(
self, asset_id, *, page_size=DEFAULT_PAGE_SIZE, query=None
):
"""docstring"""
return (
AccessPolicy(**a) for a in self._archivist.list(
AccessPolicy(**a)
for a in self._archivist.list(
SEP.join((ACCESS_POLICIES_SUBPATH, asset_id, ASSETS_LABEL)),
ACCESS_POLICIES_LABEL,
page_size=page_size,
query=self.__query(query)
query=self.__query(query),
)
)


class AccessPolicy(dict):
"""AccessPolicy object
"""
"""AccessPolicy object"""
57 changes: 20 additions & 37 deletions archivist/archivist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,17 @@ class Archivist: # pylint: disable=too-many-instance-attributes

Either auth or cert must be specified
"""

def __init__(self, url, *, auth=None, cert=None, verify=True):
"""docstring
"""
self._headers = {'content-type': 'application/json'}
"""docstring"""
self._headers = {"content-type": "application/json"}
if auth is not None:
self._headers['authorization'] = 'Bearer ' + auth.strip()
self._headers["authorization"] = "Bearer " + auth.strip()

self._url = url
self._verify = verify
if not cert and not auth:
raise ArchivistIllegalArgumentError(
"Either auth or cert must be specified"
)
raise ArchivistIllegalArgumentError("Either auth or cert must be specified")

if cert and auth:
raise ArchivistIllegalArgumentError(
Expand All @@ -65,9 +63,7 @@ def __init__(self, url, *, auth=None, cert=None, verify=True):

if cert:
if not os_path_isfile(cert):
raise ArchivistNotFoundError(
f"Cert file {cert} does not exist"
)
raise ArchivistNotFoundError(f"Cert file {cert} does not exist")

self._cert = cert

Expand All @@ -78,31 +74,26 @@ def __init__(self, url, *, auth=None, cert=None, verify=True):

@property
def headers(self):
"""docstring
"""
"""docstring"""
return self._headers

@property
def url(self):
"""docstring
"""
"""docstring"""
return self._url

@property
def verify(self):
"""docstring
"""
"""docstring"""
return self._verify

@property
def cert(self):
"""docstring
"""
"""docstring"""
return self._cert

def __add_headers(self, headers):
"""docstring
"""
"""docstring"""
if headers is not None:
newheaders = {**self.headers, **headers}
else:
Expand Down Expand Up @@ -182,11 +173,11 @@ def post_file(self, path, fd, mtype):

multipart = MultipartEncoder(
fields={
'file': ('filename', fd, mtype),
"file": ("filename", fd, mtype),
}
)
headers = {
'content-type': multipart.content_type,
"content-type": multipart.content_type,
}
response = requests.post(
SEP.join((self.url, ROOT, path)),
Expand Down Expand Up @@ -242,7 +233,7 @@ def patch(self, subpath, identity, request, *, headers=None):

def __list(self, path, args, *, headers=None):
if args:
path = '?'.join((path, args))
path = "?".join((path, args))

response = requests.get(
SEP.join((self.url, ROOT, path)),
Expand All @@ -258,10 +249,8 @@ def __list(self, path, args, *, headers=None):

@staticmethod
def __query(query):
return query and '&'.join(
sorted(
f"{k}={v}" for k, v in flatten(query, reducer='dot').items()
)
return query and "&".join(
sorted(f"{k}={v}" for k, v in flatten(query, reducer="dot").items())
)

def get_by_signature(self, path, field, query, *, headers=None):
Expand All @@ -281,9 +270,7 @@ def get_by_signature(self, path, field, query, *, headers=None):

response = self.__list(
path,
'&'.join(
(a for a in (paging, qry) if a)
),
"&".join((a for a in (paging, qry) if a)),
headers=headers,
)

Expand Down Expand Up @@ -311,15 +298,13 @@ def count(self, path, *, query=None):

paging = "page_size=1"
qry = self.__query(query)
headers = {HEADERS_REQUEST_TOTAL_COUNT: 'true'}
headers = {HEADERS_REQUEST_TOTAL_COUNT: "true"}

# v2/assets?page_size=10&something=something...

response = self.__list(
path,
'&'.join(
(a for a in (paging, qry) if a)
),
"&".join((a for a in (paging, qry) if a)),
headers=headers,
)

Expand Down Expand Up @@ -348,9 +333,7 @@ def list(self, path, field, *, page_size=None, query=None, headers=None):
while True:
response = self.__list(
path,
'&'.join(
(a for a in (paging, qry) if a)
),
"&".join((a for a in (paging, qry) if a)),
headers=headers,
)
data = response.json()
Expand Down
Loading