Replaced deprecated json_output arg in NextCloud client with response.json_data

This commit is contained in:
Fabio Manganiello 2022-04-26 19:30:26 +02:00
parent a80adc996f
commit da73a5f1b9
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -50,8 +50,13 @@ class NextcloudPlugin(Plugin):
""" """
def __init__(self, url: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, def __init__(
**kwargs): self,
url: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
**kwargs
):
""" """
:param url: URL to the index of your default NextCloud instance. :param url: URL to the index of your default NextCloud instance.
:param username: Default NextCloud username. :param username: Default NextCloud username.
@ -61,8 +66,13 @@ class NextcloudPlugin(Plugin):
self.conf = ClientConfig(url=url, username=username, password=password) self.conf = ClientConfig(url=url, username=username, password=password)
self._client = self._get_client(**self.conf.to_dict()) self._client = self._get_client(**self.conf.to_dict())
def _get_client(self, url: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, def _get_client(
raise_on_empty: bool = False): self,
url: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
raise_on_empty: bool = False,
):
from nextcloud import NextCloud from nextcloud import NextCloud
if not url: if not url:
@ -71,19 +81,25 @@ class NextcloudPlugin(Plugin):
raise AssertionError('No url/username/password provided') raise AssertionError('No url/username/password provided')
return None return None
return NextCloud(endpoint=self.conf.url, user=self.conf.username, password=self.conf.password, return NextCloud(
json_output=True) endpoint=self.conf.url,
user=self.conf.username,
password=self.conf.password,
)
return NextCloud(endpoint=url, user=username, password=password, json_output=True) return NextCloud(endpoint=url, user=username, password=password)
@staticmethod @staticmethod
def _get_permissions(permissions: Optional[List[str]]) -> int: def _get_permissions(permissions: Optional[List[str]]) -> int:
int_perm = 0 int_perm = 0
for perm in (permissions or []): for perm in permissions or []:
perm = perm.upper() perm = perm.upper()
assert hasattr(Permission, perm), 'Unknown permissions type: {}. Supported permissions: {}'.format( assert hasattr(
perm, [p.name.lower() for p in Permission]) Permission, perm
), 'Unknown permissions type: {}. Supported permissions: {}'.format(
perm, [p.name.lower() for p in Permission]
)
if perm == 'ALL': if perm == 'ALL':
int_perm = Permission.ALL.value int_perm = Permission.ALL.value
@ -96,8 +112,11 @@ class NextcloudPlugin(Plugin):
@staticmethod @staticmethod
def _get_share_type(share_type: str) -> int: def _get_share_type(share_type: str) -> int:
share_type = share_type.upper() share_type = share_type.upper()
assert hasattr(ShareType, share_type), 'Unknown share type: {}. Supported share types: {}'.format( assert hasattr(
share_type, [s.name.lower() for s in ShareType]) ShareType, share_type
), 'Unknown share type: {}. Supported share types: {}'.format(
share_type, [s.name.lower() for s in ShareType]
)
return getattr(ShareType, share_type).value return getattr(ShareType, share_type).value
@ -114,13 +133,23 @@ class NextcloudPlugin(Plugin):
args=', '.join(args), args=', '.join(args),
sep=', ' if args and kwargs else '', sep=', ' if args and kwargs else '',
kwargs=', '.join(['{}={}'.format(k, v) for k, v in kwargs.items()]), kwargs=', '.join(['{}={}'.format(k, v) for k, v in kwargs.items()]),
error=response.meta.get('message', '[No message]') if hasattr(response, 'meta') else response.raw.reason) error=response.meta.get('message', '[No message]')
if hasattr(response, 'meta')
else response.raw.reason,
)
return response.data return response.json_data
@action @action
def get_activities(self, since: Optional[id] = None, limit: Optional[int] = None, object_type: Optional[str] = None, def get_activities(
object_id: Optional[int] = None, sort: str = 'desc', **server_args) -> List[str]: self,
since: Optional[id] = None,
limit: Optional[int] = None,
object_type: Optional[str] = None,
object_id: Optional[int] = None,
sort: str = 'desc',
**server_args
) -> List[str]:
""" """
Get the list of recent activities on an instance. Get the list of recent activities on an instance.
@ -132,9 +161,15 @@ class NextcloudPlugin(Plugin):
:param server_args: Override the default server settings (see :meth:`._get_client` arguments). :param server_args: Override the default server settings (see :meth:`._get_client` arguments).
:return: The list of selected activities. :return: The list of selected activities.
""" """
return self._execute(server_args, 'get_activities', since=since, limit=limit, object_type=object_type, return self._execute(
object_id=object_id, server_args,
sort=sort) 'get_activities',
since=since,
limit=limit,
object_type=object_type,
object_id=object_id,
sort=sort,
)
@action @action
def get_apps(self, **server_args) -> List[str]: def get_apps(self, **server_args) -> List[str]:
@ -216,8 +251,13 @@ class NextcloudPlugin(Plugin):
return self._execute(server_args, 'get_group', group_id) return self._execute(server_args, 'get_group', group_id)
@action @action
def get_groups(self, search: Optional[str] = None, limit: Optional[int] = None, offset: Optional[int] = None, def get_groups(
**server_args) -> List[str]: self,
search: Optional[str] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
**server_args
) -> List[str]:
""" """
Search for groups. Search for groups.
@ -226,7 +266,9 @@ class NextcloudPlugin(Plugin):
:param offset: Start offset. :param offset: Start offset.
:param server_args: Override the default server settings (see :meth:`._get_client` arguments). :param server_args: Override the default server settings (see :meth:`._get_client` arguments).
""" """
return self._execute(server_args, 'get_groups', search=search, limit=limit, offset=offset).get('groups', []) return self._execute(
server_args, 'get_groups', search=search, limit=limit, offset=offset
).get('groups', [])
@action @action
def create_group_folder(self, name: str, **server_args): def create_group_folder(self, name: str, **server_args):
@ -268,7 +310,9 @@ class NextcloudPlugin(Plugin):
return self._execute(server_args, 'get_group_folders') return self._execute(server_args, 'get_group_folders')
@action @action
def rename_group_folder(self, folder_id: Union[int, str], new_name: str, **server_args): def rename_group_folder(
self, folder_id: Union[int, str], new_name: str, **server_args
):
""" """
Rename a group folder. Rename a group folder.
@ -279,7 +323,9 @@ class NextcloudPlugin(Plugin):
self._execute(server_args, 'rename_group_folder', folder_id, new_name) self._execute(server_args, 'rename_group_folder', folder_id, new_name)
@action @action
def grant_access_to_group_folder(self, folder_id: Union[int, str], group_id: str, **server_args): def grant_access_to_group_folder(
self, folder_id: Union[int, str], group_id: str, **server_args
):
""" """
Grant access to a group folder to a given group. Grant access to a group folder to a given group.
@ -290,7 +336,9 @@ class NextcloudPlugin(Plugin):
self._execute(server_args, 'grant_access_to_group_folder', folder_id, group_id) self._execute(server_args, 'grant_access_to_group_folder', folder_id, group_id)
@action @action
def revoke_access_to_group_folder(self, folder_id: Union[int, str], group_id: str, **server_args): def revoke_access_to_group_folder(
self, folder_id: Union[int, str], group_id: str, **server_args
):
""" """
Revoke access to a group folder to a given group. Revoke access to a group folder to a given group.
@ -301,7 +349,9 @@ class NextcloudPlugin(Plugin):
self._execute(server_args, 'revoke_access_to_group_folder', folder_id, group_id) self._execute(server_args, 'revoke_access_to_group_folder', folder_id, group_id)
@action @action
def set_group_folder_quota(self, folder_id: Union[int, str], quota: Optional[int], **server_args): def set_group_folder_quota(
self, folder_id: Union[int, str], quota: Optional[int], **server_args
):
""" """
Set the quota of a group folder. Set the quota of a group folder.
@ -309,11 +359,21 @@ class NextcloudPlugin(Plugin):
:param quota: Quota in bytes - set None for unlimited. :param quota: Quota in bytes - set None for unlimited.
:param server_args: Override the default server settings (see :meth:`._get_client` arguments). :param server_args: Override the default server settings (see :meth:`._get_client` arguments).
""" """
self._execute(server_args, 'set_quota_of_group_folder', folder_id, quota if quota is not None else -3) self._execute(
server_args,
'set_quota_of_group_folder',
folder_id,
quota if quota is not None else -3,
)
@action @action
def set_group_folder_permissions(self, folder_id: Union[int, str], group_id: str, permissions: List[str], def set_group_folder_permissions(
**server_args): self,
folder_id: Union[int, str],
group_id: str,
permissions: List[str],
**server_args
):
""" """
Set the permissions on a folder for a group. Set the permissions on a folder for a group.
@ -330,8 +390,13 @@ class NextcloudPlugin(Plugin):
:param server_args: Override the default server settings (see :meth:`._get_client` arguments). :param server_args: Override the default server settings (see :meth:`._get_client` arguments).
""" """
self._execute(server_args, 'set_permissions_to_group_folder', folder_id, group_id, self._execute(
self._get_permissions(permissions)) server_args,
'set_permissions_to_group_folder',
folder_id,
group_id,
self._get_permissions(permissions),
)
@action @action
def get_notifications(self, **server_args) -> list: def get_notifications(self, **server_args) -> list:
@ -372,8 +437,16 @@ class NextcloudPlugin(Plugin):
self._execute(server_args, 'delete_notification', notification_id) self._execute(server_args, 'delete_notification', notification_id)
@action @action
def create_share(self, path: str, share_type: str, share_with: Optional[str] = None, public_upload: bool = False, def create_share(
password: Optional[str] = None, permissions: Optional[List[str]] = None, **server_args) -> dict: self,
path: str,
share_type: str,
share_with: Optional[str] = None,
public_upload: bool = False,
password: Optional[str] = None,
permissions: Optional[List[str]] = None,
**server_args
) -> dict:
""" """
Share a file/folder with a user/group or a public link. Share a file/folder with a user/group or a public link.
@ -442,9 +515,16 @@ class NextcloudPlugin(Plugin):
""" """
share_type = self._get_share_type(share_type) share_type = self._get_share_type(share_type)
permissions = self._get_permissions(permissions or ['read']) permissions = self._get_permissions(permissions or ['read'])
return self._execute(server_args, 'create_share', path, share_type=share_type, share_with=share_with, return self._execute(
public_upload=public_upload, server_args,
password=password, permissions=permissions) 'create_share',
path,
share_type=share_type,
share_with=share_with,
public_upload=public_upload,
password=password,
permissions=permissions,
)
@action @action
def get_shares(self, **server_args) -> List[dict]: def get_shares(self, **server_args) -> List[dict]:
@ -516,8 +596,15 @@ class NextcloudPlugin(Plugin):
return self._execute(server_args, 'get_share_info', str(share_id)) return self._execute(server_args, 'get_share_info', str(share_id))
@action @action
def update_share(self, share_id: int, public_upload: Optional[bool] = None, password: Optional[str] = None, def update_share(
permissions: Optional[List[str]] = None, expire_date: Optional[str] = None, **server_args): self,
share_id: int,
public_upload: Optional[bool] = None,
password: Optional[str] = None,
permissions: Optional[List[str]] = None,
expire_date: Optional[str] = None,
**server_args
):
""" """
Update the permissions of a shared resource. Update the permissions of a shared resource.
@ -539,8 +626,15 @@ class NextcloudPlugin(Plugin):
if permissions: if permissions:
permissions = self._get_permissions(permissions) permissions = self._get_permissions(permissions)
self._execute(server_args, 'update_share', share_id, public_upload=public_upload, password=password, self._execute(
permissions=permissions, expire_date=expire_date) server_args,
'update_share',
share_id,
public_upload=public_upload,
password=password,
permissions=permissions,
expire_date=expire_date,
)
@action @action
def create_user(self, user_id: str, password: str, **server_args): def create_user(self, user_id: str, password: str, **server_args):
@ -611,8 +705,13 @@ class NextcloudPlugin(Plugin):
return self._execute(server_args, 'get_user', user_id) return self._execute(server_args, 'get_user', user_id)
@action @action
def get_users(self, search: Optional[str] = None, limit: Optional[int] = None, offset: Optional[int] = None, def get_users(
**server_args) -> List[str]: self,
search: Optional[str] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
**server_args
) -> List[str]:
""" """
Get the list of users matching some search criteria. Get the list of users matching some search criteria.
@ -621,7 +720,9 @@ class NextcloudPlugin(Plugin):
:param offset: Search results offset (default: None). :param offset: Search results offset (default: None).
:return: List of the matched user IDs. :return: List of the matched user IDs.
""" """
return self._execute(server_args, 'get_users', search=search, limit=limit, offset=offset) return self._execute(
server_args, 'get_users', search=search, limit=limit, offset=offset
)
@action @action
def delete_user(self, user_id: str, **server_args): def delete_user(self, user_id: str, **server_args):
@ -733,8 +834,15 @@ class NextcloudPlugin(Plugin):
self._execute(server_args, 'delete_path', user_id, path) self._execute(server_args, 'delete_path', user_id, path)
@action @action
def upload_file(self, remote_path: str, local_path: Optional[str] = None, content: Optional[str] = None, def upload_file(
user_id: Optional[str] = None, timestamp: Optional[Union[datetime, int, str]] = None, **server_args): self,
remote_path: str,
local_path: Optional[str] = None,
content: Optional[str] = None,
user_id: Optional[str] = None,
timestamp: Optional[Union[datetime, int, str]] = None,
**server_args
):
""" """
Upload a file. Upload a file.
@ -753,17 +861,32 @@ class NextcloudPlugin(Plugin):
if isinstance(timestamp, datetime): if isinstance(timestamp, datetime):
timestamp = int(timestamp.timestamp()) timestamp = int(timestamp.timestamp())
assert (local_path or content) and not (local_path and content), 'Please specify either local_path or content' assert (local_path or content) and not (
local_path and content
), 'Please specify either local_path or content'
if local_path: if local_path:
method = 'upload_file' method = 'upload_file'
local_path = os.path.abspath(os.path.expanduser(local_path)) local_path = os.path.abspath(os.path.expanduser(local_path))
else: else:
method = 'upload_file_contents' method = 'upload_file_contents'
return self._execute(server_args, method, user_id, local_path or content, remote_path, timestamp=timestamp) return self._execute(
server_args,
method,
user_id,
local_path or content,
remote_path,
timestamp=timestamp,
)
@action @action
def download_file(self, remote_path: str, local_path: str, user_id: Optional[str] = None, **server_args): def download_file(
self,
remote_path: str,
local_path: str,
user_id: Optional[str] = None,
**server_args
):
""" """
Download a file. Download a file.
@ -783,8 +906,14 @@ class NextcloudPlugin(Plugin):
os.chdir(cur_dir) os.chdir(cur_dir)
@action @action
def list(self, path: str, user_id: Optional[str] = None, depth: int = 1, all_properties: bool = False, def list(
**server_args) -> List[dict]: self,
path: str,
user_id: Optional[str] = None,
depth: int = 1,
all_properties: bool = False,
**server_args
) -> List[dict]:
""" """
List the content of a folder on the NextCloud instance. List the content of a folder on the NextCloud instance.
@ -795,10 +924,19 @@ class NextcloudPlugin(Plugin):
:param server_args: Override the default server settings (see :meth:`._get_client` arguments). :param server_args: Override the default server settings (see :meth:`._get_client` arguments).
""" """
user_id = user_id or server_args.get('username', self.conf.username) user_id = user_id or server_args.get('username', self.conf.username)
return self._execute(server_args, 'list_folders', user_id, path, depth=depth, all_properties=all_properties) return self._execute(
server_args,
'list_folders',
user_id,
path,
depth=depth,
all_properties=all_properties,
)
@action @action
def list_favorites(self, path: Optional[str] = None, user_id: Optional[str] = None, **server_args) -> List[dict]: def list_favorites(
self, path: Optional[str] = None, user_id: Optional[str] = None, **server_args
) -> List[dict]:
""" """
List the favorite items for a user. List the favorite items for a user.
@ -810,7 +948,9 @@ class NextcloudPlugin(Plugin):
return self._execute(server_args, 'list_folders', user_id, path) return self._execute(server_args, 'list_folders', user_id, path)
@action @action
def mark_favorite(self, path: Optional[str] = None, user_id: Optional[str] = None, **server_args): def mark_favorite(
self, path: Optional[str] = None, user_id: Optional[str] = None, **server_args
):
""" """
Add a path to a user's favorites. Add a path to a user's favorites.
@ -822,7 +962,14 @@ class NextcloudPlugin(Plugin):
self._execute(server_args, 'set_favorites', user_id, path) self._execute(server_args, 'set_favorites', user_id, path)
@action @action
def copy(self, path: str, destination: str, user_id: Optional[str] = None, overwrite: bool = False, **server_args): def copy(
self,
path: str,
destination: str,
user_id: Optional[str] = None,
overwrite: bool = False,
**server_args
):
""" """
Copy a resource to another path. Copy a resource to another path.
@ -833,10 +980,19 @@ class NextcloudPlugin(Plugin):
:param server_args: Override the default server settings (see :meth:`._get_client` arguments). :param server_args: Override the default server settings (see :meth:`._get_client` arguments).
""" """
user_id = user_id or server_args.get('username', self.conf.username) user_id = user_id or server_args.get('username', self.conf.username)
self._execute(server_args, 'copy_path', user_id, path, destination, overwrite=overwrite) self._execute(
server_args, 'copy_path', user_id, path, destination, overwrite=overwrite
)
@action @action
def move(self, path: str, destination: str, user_id: Optional[str] = None, overwrite: bool = False, **server_args): def move(
self,
path: str,
destination: str,
user_id: Optional[str] = None,
overwrite: bool = False,
**server_args
):
""" """
Move a resource to another path. Move a resource to another path.
@ -847,7 +1003,9 @@ class NextcloudPlugin(Plugin):
:param server_args: Override the default server settings (see :meth:`._get_client` arguments). :param server_args: Override the default server settings (see :meth:`._get_client` arguments).
""" """
user_id = user_id or server_args.get('username', self.conf.username) user_id = user_id or server_args.get('username', self.conf.username)
self._execute(server_args, 'move_path', user_id, path, destination, overwrite=overwrite) self._execute(
server_args, 'move_path', user_id, path, destination, overwrite=overwrite
)
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: