nc-py-api 0.12.1__py3-none-any.whl → 0.15.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,197 @@
1
+ """Nextcloud API for declaring TaskProcessing provider."""
2
+
3
+ import contextlib
4
+ import dataclasses
5
+ import typing
6
+
7
+ from ..._exceptions import NextcloudException, NextcloudExceptionNotFound
8
+ from ..._misc import clear_from_params_empty, require_capabilities
9
+ from ..._session import AsyncNcSessionApp, NcSessionApp
10
+
11
+ _EP_SUFFIX: str = "ai_provider/task_processing"
12
+
13
+
14
+ @dataclasses.dataclass
15
+ class TaskProcessingProvider:
16
+ """TaskProcessing provider description."""
17
+
18
+ def __init__(self, raw_data: dict):
19
+ self._raw_data = raw_data
20
+
21
+ @property
22
+ def name(self) -> str:
23
+ """Unique ID for the provider."""
24
+ return self._raw_data["name"]
25
+
26
+ @property
27
+ def display_name(self) -> str:
28
+ """Providers display name."""
29
+ return self._raw_data["display_name"]
30
+
31
+ @property
32
+ def task_type(self) -> str:
33
+ """The TaskType provided by this provider."""
34
+ return self._raw_data["task_type"]
35
+
36
+ def __repr__(self):
37
+ return f"<{self.__class__.__name__} name={self.name}, type={self.task_type}>"
38
+
39
+
40
+ class _TaskProcessingProviderAPI:
41
+ """API for TaskProcessing providers, available as **nc.providers.task_processing.<method>**."""
42
+
43
+ def __init__(self, session: NcSessionApp):
44
+ self._session = session
45
+
46
+ def register(
47
+ self, name: str, display_name: str, task_type: str, custom_task_type: dict[str, typing.Any] | None = None
48
+ ) -> None:
49
+ """Registers or edit the TaskProcessing provider."""
50
+ require_capabilities("app_api", self._session.capabilities)
51
+ params = {
52
+ "name": name,
53
+ "displayName": display_name,
54
+ "taskType": task_type,
55
+ "customTaskType": custom_task_type,
56
+ }
57
+ clear_from_params_empty(["customTaskType"], params)
58
+ self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)
59
+
60
+ def unregister(self, name: str, not_fail=True) -> None:
61
+ """Removes TaskProcessing provider."""
62
+ require_capabilities("app_api", self._session.capabilities)
63
+ try:
64
+ self._session.ocs("DELETE", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
65
+ except NextcloudExceptionNotFound as e:
66
+ if not not_fail:
67
+ raise e from None
68
+
69
+ def next_task(self, provider_ids: list[str], task_types: list[str]) -> dict[str, typing.Any]:
70
+ """Get the next task processing task from Nextcloud."""
71
+ with contextlib.suppress(NextcloudException):
72
+ if r := self._session.ocs(
73
+ "GET",
74
+ "/ocs/v2.php/taskprocessing/tasks_provider/next",
75
+ json={"providerIds": provider_ids, "taskTypeIds": task_types},
76
+ ):
77
+ return r
78
+ return {}
79
+
80
+ def set_progress(self, task_id: int, progress: float) -> dict[str, typing.Any]:
81
+ """Report new progress value of the task to Nextcloud. Progress should be in range from 0.0 to 100.0."""
82
+ with contextlib.suppress(NextcloudException):
83
+ if r := self._session.ocs(
84
+ "POST",
85
+ f"/ocs/v2.php/taskprocessing/tasks_provider/{task_id}/progress",
86
+ json={"taskId": task_id, "progress": progress / 100.0},
87
+ ):
88
+ return r
89
+ return {}
90
+
91
+ def upload_result_file(self, task_id: int, file: bytes | str | typing.Any) -> int:
92
+ """Uploads file and returns fileID that should be used in the ``report_result`` function.
93
+
94
+ .. note:: ``file`` can be any file-like object.
95
+ """
96
+ return self._session.ocs(
97
+ "POST",
98
+ f"/ocs/v2.php/taskprocessing/tasks_provider/{task_id}/file",
99
+ files={"file": file},
100
+ )["fileId"]
101
+
102
+ def report_result(
103
+ self,
104
+ task_id: int,
105
+ output: dict[str, typing.Any] | None = None,
106
+ error_message: str | None = None,
107
+ ) -> dict[str, typing.Any]:
108
+ """Report result of the task processing to Nextcloud."""
109
+ with contextlib.suppress(NextcloudException):
110
+ if r := self._session.ocs(
111
+ "POST",
112
+ f"/ocs/v2.php/taskprocessing/tasks_provider/{task_id}/result",
113
+ json={"taskId": task_id, "output": output, "errorMessage": error_message},
114
+ ):
115
+ return r
116
+ return {}
117
+
118
+
119
+ class _AsyncTaskProcessingProviderAPI:
120
+ """Async API for TaskProcessing providers."""
121
+
122
+ def __init__(self, session: AsyncNcSessionApp):
123
+ self._session = session
124
+
125
+ async def register(
126
+ self, name: str, display_name: str, task_type: str, custom_task_type: dict[str, typing.Any] | None = None
127
+ ) -> None:
128
+ """Registers or edit the TaskProcessing provider."""
129
+ require_capabilities("app_api", await self._session.capabilities)
130
+ params = {
131
+ "name": name,
132
+ "displayName": display_name,
133
+ "taskType": task_type,
134
+ "customTaskType": custom_task_type,
135
+ }
136
+ clear_from_params_empty(["customTaskType"], params)
137
+ await self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)
138
+
139
+ async def unregister(self, name: str, not_fail=True) -> None:
140
+ """Removes TaskProcessing provider."""
141
+ require_capabilities("app_api", await self._session.capabilities)
142
+ try:
143
+ await self._session.ocs("DELETE", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
144
+ except NextcloudExceptionNotFound as e:
145
+ if not not_fail:
146
+ raise e from None
147
+
148
+ async def next_task(self, provider_ids: list[str], task_types: list[str]) -> dict[str, typing.Any]:
149
+ """Get the next task processing task from Nextcloud."""
150
+ with contextlib.suppress(NextcloudException):
151
+ if r := await self._session.ocs(
152
+ "GET",
153
+ "/ocs/v2.php/taskprocessing/tasks_provider/next",
154
+ json={"providerIds": provider_ids, "taskTypeIds": task_types},
155
+ ):
156
+ return r
157
+ return {}
158
+
159
+ async def set_progress(self, task_id: int, progress: float) -> dict[str, typing.Any]:
160
+ """Report new progress value of the task to Nextcloud. Progress should be in range from 0.0 to 100.0."""
161
+ with contextlib.suppress(NextcloudException):
162
+ if r := await self._session.ocs(
163
+ "POST",
164
+ f"/ocs/v2.php/taskprocessing/tasks_provider/{task_id}/progress",
165
+ json={"taskId": task_id, "progress": progress / 100.0},
166
+ ):
167
+ return r
168
+ return {}
169
+
170
+ async def upload_result_file(self, task_id: int, file: bytes | str | typing.Any) -> int:
171
+ """Uploads file and returns fileID that should be used in the ``report_result`` function.
172
+
173
+ .. note:: ``file`` can be any file-like object.
174
+ """
175
+ return (
176
+ await self._session.ocs(
177
+ "POST",
178
+ f"/ocs/v2.php/taskprocessing/tasks_provider/{task_id}/file",
179
+ files={"file": file},
180
+ )
181
+ )["fileId"]
182
+
183
+ async def report_result(
184
+ self,
185
+ task_id: int,
186
+ output: dict[str, typing.Any] | None = None,
187
+ error_message: str | None = None,
188
+ ) -> dict[str, typing.Any]:
189
+ """Report result of the task processing to Nextcloud."""
190
+ with contextlib.suppress(NextcloudException):
191
+ if r := await self._session.ocs(
192
+ "POST",
193
+ f"/ocs/v2.php/taskprocessing/tasks_provider/{task_id}/result",
194
+ json={"taskId": task_id, "output": output, "errorMessage": error_message},
195
+ ):
196
+ return r
197
+ return {}
@@ -1,6 +1,7 @@
1
1
  """Nextcloud API for working with drop-down file's menu."""
2
2
 
3
3
  import dataclasses
4
+ import warnings
4
5
 
5
6
  from ..._exceptions import NextcloudExceptionNotFound
6
7
  from ..._misc import require_capabilities
@@ -54,6 +55,11 @@ class UiFileActionEntry:
54
55
  """Relative ExApp url which will be called if user click on the entry."""
55
56
  return self._raw_data["action_handler"]
56
57
 
58
+ @property
59
+ def version(self) -> str:
60
+ """AppAPI `2.6.0` supports new version of UiActions(https://github.com/cloud-py-api/app_api/pull/284)."""
61
+ return self._raw_data.get("version", "1.0")
62
+
57
63
  def __repr__(self):
58
64
  return f"<{self.__class__.__name__} name={self.name}, mime={self.mime}, handler={self.action_handler}>"
59
65
 
@@ -67,7 +73,12 @@ class _UiFilesActionsAPI:
67
73
  self._session = session
68
74
 
69
75
  def register(self, name: str, display_name: str, callback_url: str, **kwargs) -> None:
70
- """Registers the files a dropdown menu element."""
76
+ """Registers the files dropdown menu element."""
77
+ warnings.warn(
78
+ "register() is deprecated and will be removed in a future version. Use register_ex() instead.",
79
+ DeprecationWarning,
80
+ stacklevel=2,
81
+ )
71
82
  require_capabilities("app_api", self._session.capabilities)
72
83
  params = {
73
84
  "name": name,
@@ -80,6 +91,20 @@ class _UiFilesActionsAPI:
80
91
  }
81
92
  self._session.ocs("POST", f"{self._session.ae_url}/{self._ep_suffix}", json=params)
82
93
 
94
+ def register_ex(self, name: str, display_name: str, callback_url: str, **kwargs) -> None:
95
+ """Registers the files dropdown menu element(extended version that receives ``ActionFileInfoEx``)."""
96
+ require_capabilities("app_api", self._session.capabilities)
97
+ params = {
98
+ "name": name,
99
+ "displayName": display_name,
100
+ "actionHandler": callback_url,
101
+ "icon": kwargs.get("icon", ""),
102
+ "mime": kwargs.get("mime", "file"),
103
+ "permissions": kwargs.get("permissions", 31),
104
+ "order": kwargs.get("order", 0),
105
+ }
106
+ self._session.ocs("POST", f"{self._session.ae_url_v2}/{self._ep_suffix}", json=params)
107
+
83
108
  def unregister(self, name: str, not_fail=True) -> None:
84
109
  """Removes files dropdown menu element."""
85
110
  require_capabilities("app_api", self._session.capabilities)
@@ -110,6 +135,11 @@ class _AsyncUiFilesActionsAPI:
110
135
 
111
136
  async def register(self, name: str, display_name: str, callback_url: str, **kwargs) -> None:
112
137
  """Registers the files a dropdown menu element."""
138
+ warnings.warn(
139
+ "register() is deprecated and will be removed in a future version. Use register_ex() instead.",
140
+ DeprecationWarning,
141
+ stacklevel=2,
142
+ )
113
143
  require_capabilities("app_api", await self._session.capabilities)
114
144
  params = {
115
145
  "name": name,
@@ -122,6 +152,20 @@ class _AsyncUiFilesActionsAPI:
122
152
  }
123
153
  await self._session.ocs("POST", f"{self._session.ae_url}/{self._ep_suffix}", json=params)
124
154
 
155
+ async def register_ex(self, name: str, display_name: str, callback_url: str, **kwargs) -> None:
156
+ """Registers the files dropdown menu element(extended version that receives ``ActionFileInfoEx``)."""
157
+ require_capabilities("app_api", await self._session.capabilities)
158
+ params = {
159
+ "name": name,
160
+ "displayName": display_name,
161
+ "actionHandler": callback_url,
162
+ "icon": kwargs.get("icon", ""),
163
+ "mime": kwargs.get("mime", "file"),
164
+ "permissions": kwargs.get("permissions", 31),
165
+ "order": kwargs.get("order", 0),
166
+ }
167
+ await self._session.ocs("POST", f"{self._session.ae_url_v2}/{self._ep_suffix}", json=params)
168
+
125
169
  async def unregister(self, name: str, not_fail=True) -> None:
126
170
  """Removes files dropdown menu element."""
127
171
  require_capabilities("app_api", await self._session.capabilities)
@@ -203,9 +203,7 @@ class FsNode:
203
203
  )
204
204
 
205
205
  def __eq__(self, other):
206
- if self.file_id and self.file_id == other.file_id:
207
- return True
208
- return False
206
+ return bool(self.file_id and self.file_id == other.file_id)
209
207
 
210
208
  @property
211
209
  def has_extra(self) -> bool:
@@ -282,12 +280,13 @@ class FilePermissions(enum.IntFlag):
282
280
  """Access to re-share object(s)"""
283
281
 
284
282
 
285
- def permissions_to_str(permissions: int, is_dir: bool = False) -> str:
283
+ def permissions_to_str(permissions: int | str, is_dir: bool = False) -> str:
286
284
  """Converts integer permissions to string permissions.
287
285
 
288
286
  :param permissions: concatenation of ``FilePermissions`` integer flags.
289
287
  :param is_dir: Flag indicating is permissions related to the directory object or not.
290
288
  """
289
+ permissions = int(permissions) if not isinstance(permissions, int) else permissions
291
290
  r = ""
292
291
  if permissions & FilePermissions.PERMISSION_SHARE:
293
292
  r += "R"
@@ -519,3 +518,10 @@ class ActionFileInfo(BaseModel):
519
518
  last_modified=datetime.datetime.utcfromtimestamp(self.mtime).replace(tzinfo=datetime.timezone.utc),
520
519
  mimetype=self.mime,
521
520
  )
521
+
522
+
523
+ class ActionFileInfoEx(BaseModel):
524
+ """New ``register_ex`` uses new data format which allowing receiving multiple NC Nodes in one request."""
525
+
526
+ files: list[ActionFileInfo]
527
+ """Always list of ``ActionFileInfo`` with one element minimum."""
nc_py_api/files/_files.py CHANGED
@@ -168,6 +168,18 @@ def build_list_tags_response(response: Response) -> list[SystemTag]:
168
168
  return result
169
169
 
170
170
 
171
+ def build_tags_ids_for_object(url_to_fetch: str, response: Response) -> list[int]:
172
+ result = []
173
+ records = _webdav_response_to_records(response, "list_tags_ids")
174
+ for record in records:
175
+ prop_stat = record["d:propstat"]
176
+ if str(prop_stat.get("d:status", "")).find("200 OK") != -1:
177
+ href_suffix = str(record["d:href"]).removeprefix(url_to_fetch).strip("/")
178
+ if href_suffix:
179
+ result.append(int(href_suffix))
180
+ return result
181
+
182
+
171
183
  def build_update_tag_req(
172
184
  name: str | None, user_visible: bool | None, user_assignable: bool | None
173
185
  ) -> ElementTree.Element: