sapiopycommons 2024.11.11a364__py3-none-any.whl → 2024.11.18a366__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.
Potentially problematic release.
This version of sapiopycommons might be problematic. Click here for more details.
- sapiopycommons/callbacks/callback_util.py +532 -83
- sapiopycommons/callbacks/field_builder.py +537 -0
- sapiopycommons/chem/IndigoMolecules.py +2 -0
- sapiopycommons/chem/Molecules.py +77 -18
- sapiopycommons/customreport/__init__.py +0 -0
- sapiopycommons/customreport/column_builder.py +60 -0
- sapiopycommons/customreport/custom_report_builder.py +130 -0
- sapiopycommons/customreport/term_builder.py +299 -0
- sapiopycommons/datatype/attachment_util.py +11 -10
- sapiopycommons/datatype/data_fields.py +61 -0
- sapiopycommons/datatype/pseudo_data_types.py +440 -0
- sapiopycommons/eln/experiment_handler.py +272 -70
- sapiopycommons/eln/experiment_report_util.py +653 -0
- sapiopycommons/files/complex_data_loader.py +5 -4
- sapiopycommons/files/file_bridge.py +31 -24
- sapiopycommons/files/file_bridge_handler.py +340 -0
- sapiopycommons/files/file_data_handler.py +2 -5
- sapiopycommons/files/file_util.py +59 -9
- sapiopycommons/files/file_validator.py +92 -6
- sapiopycommons/files/file_writer.py +44 -15
- sapiopycommons/flowcyto/flow_cyto.py +77 -0
- sapiopycommons/flowcyto/flowcyto_data.py +75 -0
- sapiopycommons/general/accession_service.py +375 -0
- sapiopycommons/general/aliases.py +207 -6
- sapiopycommons/general/audit_log.py +189 -0
- sapiopycommons/general/custom_report_util.py +212 -37
- sapiopycommons/general/exceptions.py +21 -8
- sapiopycommons/general/popup_util.py +21 -0
- sapiopycommons/general/sapio_links.py +50 -0
- sapiopycommons/general/time_util.py +8 -2
- sapiopycommons/multimodal/multimodal.py +146 -0
- sapiopycommons/multimodal/multimodal_data.py +490 -0
- sapiopycommons/processtracking/custom_workflow_handler.py +406 -0
- sapiopycommons/processtracking/endpoints.py +22 -22
- sapiopycommons/recordmodel/record_handler.py +481 -97
- sapiopycommons/rules/eln_rule_handler.py +34 -25
- sapiopycommons/rules/on_save_rule_handler.py +34 -31
- sapiopycommons/sftpconnect/__init__.py +0 -0
- sapiopycommons/sftpconnect/sftp_builder.py +69 -0
- sapiopycommons/webhook/webhook_context.py +39 -0
- sapiopycommons/webhook/webhook_handlers.py +201 -42
- sapiopycommons/webhook/webservice_handlers.py +67 -0
- {sapiopycommons-2024.11.11a364.dist-info → sapiopycommons-2024.11.18a366.dist-info}/METADATA +5 -2
- sapiopycommons-2024.11.18a366.dist-info/RECORD +59 -0
- {sapiopycommons-2024.11.11a364.dist-info → sapiopycommons-2024.11.18a366.dist-info}/WHEEL +1 -1
- sapiopycommons-2024.11.11a364.dist-info/RECORD +0 -38
- {sapiopycommons-2024.11.11a364.dist-info → sapiopycommons-2024.11.18a366.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import io
|
|
2
2
|
|
|
3
3
|
from sapiopylib.rest.User import SapioUser
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
from sapiopycommons.general.aliases import UserIdentifier, AliasUtil
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
class CDL:
|
|
8
9
|
@staticmethod
|
|
9
|
-
def load_cdl(context:
|
|
10
|
+
def load_cdl(context: UserIdentifier, config_name: str, file_name: str, file_data: bytes | str) \
|
|
10
11
|
-> list[int]:
|
|
11
12
|
"""
|
|
12
13
|
Create data records from a file using one of the complex data loader (CDL) configurations in the system.
|
|
@@ -22,8 +23,8 @@ class CDL:
|
|
|
22
23
|
"configName": config_name,
|
|
23
24
|
"fileName": file_name
|
|
24
25
|
}
|
|
25
|
-
user: SapioUser =
|
|
26
|
-
with io.
|
|
26
|
+
user: SapioUser = AliasUtil.to_sapio_user(context)
|
|
27
|
+
with io.BytesIO(file_data.encode() if isinstance(file_data, str) else file_data) as data_stream:
|
|
27
28
|
response = user.post_data_stream(sub_path, params=params, data_stream=data_stream)
|
|
28
29
|
user.raise_for_status(response)
|
|
29
30
|
# The response content is returned as bytes for a comma separated string of record IDs.
|
|
@@ -4,19 +4,21 @@ import urllib.parse
|
|
|
4
4
|
|
|
5
5
|
from requests import Response
|
|
6
6
|
from sapiopylib.rest.User import SapioUser
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
from sapiopycommons.general.aliases import UserIdentifier, AliasUtil
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
# FR-46064 - Initial port of PyWebhookUtils to sapiopycommons.
|
|
11
12
|
class FileBridge:
|
|
12
13
|
@staticmethod
|
|
13
|
-
def read_file(context:
|
|
14
|
+
def read_file(context: UserIdentifier, bridge_name: str, file_path: str,
|
|
14
15
|
base64_decode: bool = True) -> bytes:
|
|
15
16
|
"""
|
|
16
17
|
Read a file from FileBridge.
|
|
17
18
|
|
|
18
19
|
:param context: The current webhook context or a user object to send requests from.
|
|
19
|
-
:param bridge_name: The name of the bridge to use.
|
|
20
|
+
:param bridge_name: The name of the bridge to use. This is the "connection name" in the
|
|
21
|
+
file bridge configurations.
|
|
20
22
|
:param file_path: The path to read the file from.
|
|
21
23
|
:param base64_decode: If true, base64 decode the file. Files are by default base64 encoded when retrieved from
|
|
22
24
|
FileBridge.
|
|
@@ -26,7 +28,7 @@ class FileBridge:
|
|
|
26
28
|
params = {
|
|
27
29
|
'Filepath': f"bridge://{bridge_name}/{file_path}"
|
|
28
30
|
}
|
|
29
|
-
user: SapioUser =
|
|
31
|
+
user: SapioUser = AliasUtil.to_sapio_user(context)
|
|
30
32
|
response = user.get(sub_path, params)
|
|
31
33
|
user.raise_for_status(response)
|
|
32
34
|
|
|
@@ -36,13 +38,14 @@ class FileBridge:
|
|
|
36
38
|
return ret_val
|
|
37
39
|
|
|
38
40
|
@staticmethod
|
|
39
|
-
def write_file(context:
|
|
41
|
+
def write_file(context: UserIdentifier, bridge_name: str, file_path: str,
|
|
40
42
|
file_data: bytes | str) -> None:
|
|
41
43
|
"""
|
|
42
44
|
Write a file to FileBridge.
|
|
43
45
|
|
|
44
46
|
:param context: The current webhook context or a user object to send requests from.
|
|
45
|
-
:param bridge_name: The name of the bridge to use.
|
|
47
|
+
:param bridge_name: The name of the bridge to use. This is the "connection name" in the
|
|
48
|
+
file bridge configurations.
|
|
46
49
|
:param file_path: The path to write the file to. If a file already exists at the given path then the file is
|
|
47
50
|
overwritten.
|
|
48
51
|
:param file_data: A string or bytes of the file to be written.
|
|
@@ -51,41 +54,43 @@ class FileBridge:
|
|
|
51
54
|
params = {
|
|
52
55
|
'Filepath': f"bridge://{bridge_name}/{file_path}"
|
|
53
56
|
}
|
|
54
|
-
user: SapioUser =
|
|
55
|
-
with io.
|
|
57
|
+
user: SapioUser = AliasUtil.to_sapio_user(context)
|
|
58
|
+
with io.BytesIO(file_data.encode() if isinstance(file_data, str) else file_data) as data_stream:
|
|
56
59
|
response = user.post_data_stream(sub_path, params=params, data_stream=data_stream)
|
|
57
60
|
user.raise_for_status(response)
|
|
58
61
|
|
|
59
62
|
@staticmethod
|
|
60
|
-
def list_directory(context:
|
|
63
|
+
def list_directory(context: UserIdentifier, bridge_name: str,
|
|
61
64
|
file_path: str | None = "") -> list[str]:
|
|
62
65
|
"""
|
|
63
66
|
List the contents of a FileBridge directory.
|
|
64
67
|
|
|
65
68
|
:param context: The current webhook context or a user object to send requests from.
|
|
66
|
-
:param bridge_name: The name of the bridge to use.
|
|
69
|
+
:param bridge_name: The name of the bridge to use. This is the "connection name" in the
|
|
70
|
+
file bridge configurations.
|
|
67
71
|
:param file_path: The path to read the directory from.
|
|
68
|
-
:return: A list of
|
|
72
|
+
:return: A list of names of files and folders in the directory.
|
|
69
73
|
"""
|
|
70
74
|
sub_path = '/ext/filebridge/listDirectory'
|
|
71
75
|
params = {
|
|
72
76
|
'Filepath': f"bridge://{bridge_name}/{file_path}"
|
|
73
77
|
}
|
|
74
|
-
user: SapioUser =
|
|
78
|
+
user: SapioUser = AliasUtil.to_sapio_user(context)
|
|
75
79
|
response: Response = user.get(sub_path, params=params)
|
|
76
80
|
user.raise_for_status(response)
|
|
77
81
|
|
|
78
82
|
response_body: list[str] = response.json()
|
|
79
83
|
path_length = len(f"bridge://{bridge_name}/")
|
|
80
|
-
return [urllib.parse.unquote(value[path_length:]
|
|
84
|
+
return [urllib.parse.unquote(value)[path_length:] for value in response_body]
|
|
81
85
|
|
|
82
86
|
@staticmethod
|
|
83
|
-
def create_directory(context:
|
|
87
|
+
def create_directory(context: UserIdentifier, bridge_name: str, file_path: str) -> None:
|
|
84
88
|
"""
|
|
85
89
|
Create a new directory in FileBridge.
|
|
86
90
|
|
|
87
91
|
:param context: The current webhook context or a user object to send requests from.
|
|
88
|
-
:param bridge_name: The name of the bridge to use.
|
|
92
|
+
:param bridge_name: The name of the bridge to use. This is the "connection name" in the
|
|
93
|
+
file bridge configurations.
|
|
89
94
|
:param file_path: The path to create the directory at. If a directory already exists at the given path then an
|
|
90
95
|
exception is raised.
|
|
91
96
|
"""
|
|
@@ -93,40 +98,42 @@ class FileBridge:
|
|
|
93
98
|
params = {
|
|
94
99
|
'Filepath': f"bridge://{bridge_name}/{file_path}"
|
|
95
100
|
}
|
|
96
|
-
user: SapioUser =
|
|
101
|
+
user: SapioUser = AliasUtil.to_sapio_user(context)
|
|
97
102
|
response = user.post(sub_path, params=params)
|
|
98
103
|
user.raise_for_status(response)
|
|
99
104
|
|
|
100
105
|
@staticmethod
|
|
101
|
-
def delete_file(context:
|
|
106
|
+
def delete_file(context: UserIdentifier, bridge_name: str, file_path: str) -> None:
|
|
102
107
|
"""
|
|
103
108
|
Delete an existing file in FileBridge.
|
|
104
109
|
|
|
105
110
|
:param context: The current webhook context or a user object to send requests from.
|
|
106
|
-
:param bridge_name: The name of the bridge to use.
|
|
111
|
+
:param bridge_name: The name of the bridge to use. This is the "connection name" in the
|
|
112
|
+
file bridge configurations.
|
|
107
113
|
:param file_path: The path to the file to delete.
|
|
108
114
|
"""
|
|
109
115
|
sub_path = '/ext/filebridge/deleteFile'
|
|
110
116
|
params = {
|
|
111
117
|
'Filepath': f"bridge://{bridge_name}/{file_path}"
|
|
112
118
|
}
|
|
113
|
-
user: SapioUser =
|
|
114
|
-
response = user.
|
|
119
|
+
user: SapioUser = AliasUtil.to_sapio_user(context)
|
|
120
|
+
response = user.delete(sub_path, params=params)
|
|
115
121
|
user.raise_for_status(response)
|
|
116
122
|
|
|
117
123
|
@staticmethod
|
|
118
|
-
def delete_directory(context:
|
|
124
|
+
def delete_directory(context: UserIdentifier, bridge_name: str, file_path: str) -> None:
|
|
119
125
|
"""
|
|
120
126
|
Delete an existing directory in FileBridge.
|
|
121
127
|
|
|
122
128
|
:param context: The current webhook context or a user object to send requests from.
|
|
123
|
-
:param bridge_name: The name of the bridge to use.
|
|
129
|
+
:param bridge_name: The name of the bridge to use. This is the "connection name" in the
|
|
130
|
+
file bridge configurations.
|
|
124
131
|
:param file_path: The path to the directory to delete.
|
|
125
132
|
"""
|
|
126
133
|
sub_path = '/ext/filebridge/deleteDirectory'
|
|
127
134
|
params = {
|
|
128
135
|
'Filepath': f"bridge://{bridge_name}/{file_path}"
|
|
129
136
|
}
|
|
130
|
-
user: SapioUser =
|
|
131
|
-
response = user.
|
|
137
|
+
user: SapioUser = AliasUtil.to_sapio_user(context)
|
|
138
|
+
response = user.delete(sub_path, params=params)
|
|
132
139
|
user.raise_for_status(response)
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import abstractmethod, ABC
|
|
4
|
+
from weakref import WeakValueDictionary
|
|
5
|
+
|
|
6
|
+
from sapiopylib.rest.User import SapioUser
|
|
7
|
+
|
|
8
|
+
from sapiopycommons.files.file_bridge import FileBridge
|
|
9
|
+
from sapiopycommons.general.aliases import AliasUtil, UserIdentifier
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class FileBridgeHandler:
|
|
13
|
+
"""
|
|
14
|
+
The FileBridgeHandler provides caching of the results of file bridge endpoint calls while also containing quality
|
|
15
|
+
of life functions for common file bridge actions.
|
|
16
|
+
"""
|
|
17
|
+
user: SapioUser
|
|
18
|
+
__bridge: str
|
|
19
|
+
__file_cache: dict[str, bytes]
|
|
20
|
+
"""A cache of file paths to file bytes."""
|
|
21
|
+
__files: dict[str, File]
|
|
22
|
+
"""A cache of file paths to File objects."""
|
|
23
|
+
__dir_cache: dict[str, list[str]]
|
|
24
|
+
"""A cache of directory file paths to the names of the files or nested directories within it."""
|
|
25
|
+
__directories: dict[str, Directory]
|
|
26
|
+
"""A cache of directory file paths to Directory objects."""
|
|
27
|
+
|
|
28
|
+
__instances: WeakValueDictionary[str, FileBridgeHandler] = WeakValueDictionary()
|
|
29
|
+
__initialized: bool
|
|
30
|
+
|
|
31
|
+
def __new__(cls, context: UserIdentifier, bridge_name: str):
|
|
32
|
+
"""
|
|
33
|
+
:param context: The current webhook context or a user object to send requests from.
|
|
34
|
+
"""
|
|
35
|
+
user = AliasUtil.to_sapio_user(context)
|
|
36
|
+
key = f"{user.__hash__()}:{bridge_name}"
|
|
37
|
+
obj = cls.__instances.get(key)
|
|
38
|
+
if not obj:
|
|
39
|
+
obj = object.__new__(cls)
|
|
40
|
+
obj.__initialized = False
|
|
41
|
+
cls.__instances[key] = obj
|
|
42
|
+
return obj
|
|
43
|
+
|
|
44
|
+
def __init__(self, context: UserIdentifier, bridge_name: str):
|
|
45
|
+
"""
|
|
46
|
+
:param context: The current webhook context or a user object to send requests from.
|
|
47
|
+
:param bridge_name: The name of the bridge to communicate with. This is the "connection name" in the
|
|
48
|
+
file bridge configurations.
|
|
49
|
+
"""
|
|
50
|
+
if self.__initialized:
|
|
51
|
+
return
|
|
52
|
+
self.__initialized = True
|
|
53
|
+
|
|
54
|
+
self.user = AliasUtil.to_sapio_user(context)
|
|
55
|
+
self.__bridge = bridge_name
|
|
56
|
+
self.__file_cache = {}
|
|
57
|
+
self.__files = {}
|
|
58
|
+
self.__dir_cache = {}
|
|
59
|
+
self.__directories = {}
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def connection_name(self) -> str:
|
|
63
|
+
return self.__bridge
|
|
64
|
+
|
|
65
|
+
def clear_caches(self) -> None:
|
|
66
|
+
"""
|
|
67
|
+
Clear the file and directory caches of this handler.
|
|
68
|
+
"""
|
|
69
|
+
self.__file_cache.clear()
|
|
70
|
+
self.__files.clear()
|
|
71
|
+
self.__dir_cache.clear()
|
|
72
|
+
self.__directories.clear()
|
|
73
|
+
|
|
74
|
+
def read_file(self, file_path: str, base64_decode: bool = True) -> bytes:
|
|
75
|
+
"""
|
|
76
|
+
Read a file from FileBridge. The bytes of the given file will be cached so that any subsequent reads of this
|
|
77
|
+
file will not make an additional webservice call.
|
|
78
|
+
|
|
79
|
+
:param file_path: The path to read the file from.
|
|
80
|
+
:param base64_decode: If true, base64 decode the file. Files are by default base64 encoded when retrieved from
|
|
81
|
+
FileBridge.
|
|
82
|
+
:return: The bytes of the file.
|
|
83
|
+
"""
|
|
84
|
+
if file_path in self.__file_cache:
|
|
85
|
+
return self.__file_cache[file_path]
|
|
86
|
+
file_bytes: bytes = FileBridge.read_file(self.user, self.__bridge, file_path, base64_decode)
|
|
87
|
+
self.__file_cache[file_path] = file_bytes
|
|
88
|
+
return file_bytes
|
|
89
|
+
|
|
90
|
+
def write_file(self, file_path: str, file_data: bytes | str) -> None:
|
|
91
|
+
"""
|
|
92
|
+
Write a file to FileBridge. The bytes of the given file will be cached so that any subsequent reads of this
|
|
93
|
+
file will not make an additional webservice call.
|
|
94
|
+
|
|
95
|
+
:param file_path: The path to write the file to. If a file already exists at the given path then the file is
|
|
96
|
+
overwritten.
|
|
97
|
+
:param file_data: A string or bytes of the file to be written.
|
|
98
|
+
"""
|
|
99
|
+
FileBridge.write_file(self.user, self.__bridge, file_path, file_data)
|
|
100
|
+
self.__file_cache[file_path] = file_data if isinstance(file_data, bytes) else file_data.encode()
|
|
101
|
+
|
|
102
|
+
# Find the directory path to this file and the name of the file. Add the file name to the cached list of
|
|
103
|
+
# files for the directory, assuming we have this directory cached and the file isn't already in it.
|
|
104
|
+
last_slash: int = file_path.rfind("/")
|
|
105
|
+
dir_path: str = file_path[:last_slash]
|
|
106
|
+
file_name: str = file_path[last_slash + 1:]
|
|
107
|
+
if dir_path in self.__dir_cache and file_path not in self.__dir_cache[dir_path]:
|
|
108
|
+
self.__dir_cache[dir_path].append(file_name)
|
|
109
|
+
|
|
110
|
+
def delete_file(self, file_path: str) -> None:
|
|
111
|
+
"""
|
|
112
|
+
Delete an existing file in FileBridge. If this file is in the cache, it will also be deleted from the cache.
|
|
113
|
+
|
|
114
|
+
:param file_path: The path to the file to delete.
|
|
115
|
+
"""
|
|
116
|
+
FileBridge.delete_file(self.user, self.__bridge, file_path)
|
|
117
|
+
if file_path in self.__file_cache:
|
|
118
|
+
self.__file_cache.pop(file_path)
|
|
119
|
+
if file_path in self.__files:
|
|
120
|
+
self.__files.pop(file_path)
|
|
121
|
+
|
|
122
|
+
def list_directory(self, file_path: str) -> list[str]:
|
|
123
|
+
"""
|
|
124
|
+
List the contents of a FileBridge directory. The contents of this directory will be cached so that any
|
|
125
|
+
subsequent lists of this directory will not make an additional webservice call.
|
|
126
|
+
|
|
127
|
+
:param file_path: The path to read the directory from.
|
|
128
|
+
:return: A list of names of files and folders in the directory.
|
|
129
|
+
"""
|
|
130
|
+
if file_path in self.__dir_cache:
|
|
131
|
+
return self.__dir_cache[file_path]
|
|
132
|
+
files: list[str] = FileBridge.list_directory(self.user, self.__bridge, file_path)
|
|
133
|
+
self.__dir_cache[file_path] = files
|
|
134
|
+
return files
|
|
135
|
+
|
|
136
|
+
def create_directory(self, file_path: str) -> None:
|
|
137
|
+
"""
|
|
138
|
+
Create a new directory in FileBridge. This new directory will be added to the cache as empty so that listing
|
|
139
|
+
the same directory does not make an additional webservice call.
|
|
140
|
+
|
|
141
|
+
:param file_path: The path to create the directory at. If a directory already exists at the given path then an
|
|
142
|
+
exception is raised.
|
|
143
|
+
"""
|
|
144
|
+
FileBridge.create_directory(self.user, self.__bridge, file_path)
|
|
145
|
+
# This directory was just created, so we know it's empty.
|
|
146
|
+
self.__dir_cache[file_path] = []
|
|
147
|
+
|
|
148
|
+
def delete_directory(self, file_path: str) -> None:
|
|
149
|
+
"""
|
|
150
|
+
Delete an existing directory in FileBridge. If this directory is in the cache, it will also be deleted
|
|
151
|
+
from the cache.
|
|
152
|
+
|
|
153
|
+
:param file_path: The path to the directory to delete.
|
|
154
|
+
"""
|
|
155
|
+
FileBridge.delete_directory(self.user, self.__bridge, file_path)
|
|
156
|
+
if file_path in self.__dir_cache:
|
|
157
|
+
self.__dir_cache.pop(file_path)
|
|
158
|
+
if file_path in self.__directories:
|
|
159
|
+
self.__directories.pop(file_path)
|
|
160
|
+
|
|
161
|
+
def is_file(self, file_path: str) -> bool:
|
|
162
|
+
"""
|
|
163
|
+
Determine if the given file path points to a file or a directory. This is achieved by trying to call
|
|
164
|
+
list_directory on the given file path. If an exception is thrown, that's because the function was called
|
|
165
|
+
on a file. If no exception is thrown, then we know that this is a directory, and we have now also cached
|
|
166
|
+
the contents of that directory if it wasn't cached already.
|
|
167
|
+
|
|
168
|
+
:param file_path: A file path.
|
|
169
|
+
:return: True if the file path points to a file. False if it points to a directory.
|
|
170
|
+
"""
|
|
171
|
+
try:
|
|
172
|
+
self.list_directory(file_path)
|
|
173
|
+
return False
|
|
174
|
+
except Exception:
|
|
175
|
+
return True
|
|
176
|
+
|
|
177
|
+
def move_file(self, move_from: str, move_to: str, old_name: str, new_name: str | None = None) -> None:
|
|
178
|
+
"""
|
|
179
|
+
Move a file from one location to another within File Bridge. This is done be reading the file into memory,
|
|
180
|
+
writing a copy of the file in the new location, then deleting the original file.
|
|
181
|
+
|
|
182
|
+
:param move_from: The path to the current location of the file.
|
|
183
|
+
:param move_to: The path to move the file to.
|
|
184
|
+
:param old_name: The current name of the file.
|
|
185
|
+
:param new_name: The name that the file should have after it is moved. if this is not provided, then the new
|
|
186
|
+
name will be the same as the old name.
|
|
187
|
+
"""
|
|
188
|
+
if not new_name:
|
|
189
|
+
new_name = old_name
|
|
190
|
+
|
|
191
|
+
# Read the file into memory.
|
|
192
|
+
file_bytes: bytes = self.read_file(move_from + "/" + old_name)
|
|
193
|
+
# Write the file into the new location.
|
|
194
|
+
self.write_file(move_to + "/" + new_name, file_bytes)
|
|
195
|
+
# Delete the file from the old location. We do this last in case the write call fails.
|
|
196
|
+
self.delete_file(move_from + "/" + old_name)
|
|
197
|
+
|
|
198
|
+
def get_file_object(self, file_path: str) -> File:
|
|
199
|
+
"""
|
|
200
|
+
Get a File object from a file path. This object can be used to get the contents of the file at this path
|
|
201
|
+
and traverse up the file hierarchy to the directory that the file is contained within.
|
|
202
|
+
|
|
203
|
+
There is no guarantee that this file actually exists within the current file bridge connection when it is
|
|
204
|
+
constructed. If the file doesn't exist, then retrieving its contents will fail.
|
|
205
|
+
|
|
206
|
+
:param file_path: A file path.
|
|
207
|
+
:return: A File object constructed form the given file path.
|
|
208
|
+
"""
|
|
209
|
+
if file_path in self.__files:
|
|
210
|
+
return self.__files[file_path]
|
|
211
|
+
file = File(self, file_path)
|
|
212
|
+
self.__files[file_path] = file
|
|
213
|
+
return file
|
|
214
|
+
|
|
215
|
+
def get_directory_object(self, file_path: str) -> Directory | None:
|
|
216
|
+
"""
|
|
217
|
+
Get a Directory object from a file path. This object can be used to traverse up and down the file hierarchy
|
|
218
|
+
by going up to the parent directory that this directory is contained within or going down to the contents of
|
|
219
|
+
this directory.
|
|
220
|
+
|
|
221
|
+
There is no guarantee that this directory actually exists within the current file bridge connection when it is
|
|
222
|
+
constructed. If the directory doesn't exist, then retrieving its contents will fail.
|
|
223
|
+
|
|
224
|
+
:param file_path: A file path.
|
|
225
|
+
:return: A Directory object constructed form the given file path.
|
|
226
|
+
"""
|
|
227
|
+
if file_path is None:
|
|
228
|
+
return None
|
|
229
|
+
if file_path in self.__directories:
|
|
230
|
+
return self.__directories[file_path]
|
|
231
|
+
directory = Directory(self, file_path)
|
|
232
|
+
self.__directories[file_path] = directory
|
|
233
|
+
return directory
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
class FileBridgeObject(ABC):
|
|
237
|
+
"""
|
|
238
|
+
A FileBridgeObject is either a file or a directory that is contained within file bridge. Every object has a
|
|
239
|
+
name and a parent directory that it is contained within (unless the object is located in the bridge root, in
|
|
240
|
+
which case the parent is None). From the name and the parent, a path can be constructed to that object.
|
|
241
|
+
"""
|
|
242
|
+
_handler: FileBridgeHandler
|
|
243
|
+
name: str
|
|
244
|
+
parent: Directory | None
|
|
245
|
+
|
|
246
|
+
def __init__(self, handler: FileBridgeHandler, file_path: str):
|
|
247
|
+
self._handler = handler
|
|
248
|
+
|
|
249
|
+
name, root = split_path(file_path)
|
|
250
|
+
self.name = name
|
|
251
|
+
self.parent = handler.get_directory_object(root)
|
|
252
|
+
|
|
253
|
+
@abstractmethod
|
|
254
|
+
def is_file(self) -> bool:
|
|
255
|
+
"""
|
|
256
|
+
:return: True if this object is a file. False if it is a directory.
|
|
257
|
+
"""
|
|
258
|
+
pass
|
|
259
|
+
|
|
260
|
+
def get_path(self) -> str:
|
|
261
|
+
"""
|
|
262
|
+
:return: The file path that leads to this object.
|
|
263
|
+
"""
|
|
264
|
+
if self.parent is None:
|
|
265
|
+
return self.name
|
|
266
|
+
return self.parent.get_path() + "/" + self.name
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
class File(FileBridgeObject):
|
|
270
|
+
def __init__(self, handler: FileBridgeHandler, file_path: str):
|
|
271
|
+
"""
|
|
272
|
+
:param handler: A FileBridgeHandler for the connection that this file came from.
|
|
273
|
+
:param file_path: The path to this file.
|
|
274
|
+
"""
|
|
275
|
+
super().__init__(handler, file_path)
|
|
276
|
+
|
|
277
|
+
@property
|
|
278
|
+
def contents(self) -> bytes:
|
|
279
|
+
"""
|
|
280
|
+
:return: The bytes of this file.
|
|
281
|
+
This pulls from the cache of this object's related FileBridgeHandler.
|
|
282
|
+
"""
|
|
283
|
+
return self._handler.read_file(self.get_path())
|
|
284
|
+
|
|
285
|
+
def is_file(self) -> bool:
|
|
286
|
+
return True
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
class Directory(FileBridgeObject):
|
|
290
|
+
def __init__(self, handler: FileBridgeHandler, file_path: str):
|
|
291
|
+
"""
|
|
292
|
+
:param handler: A FileBridgeHandler for the connection that this directory came from.
|
|
293
|
+
:param file_path: The path to this directory.
|
|
294
|
+
"""
|
|
295
|
+
super().__init__(handler, file_path)
|
|
296
|
+
|
|
297
|
+
@property
|
|
298
|
+
def contents(self) -> dict[str, FileBridgeObject]:
|
|
299
|
+
"""
|
|
300
|
+
:return: A dictionary of object names to the objects (Files or Directories) contained within this Directory.
|
|
301
|
+
This pulls from the cache of this object's related FileBridgeHandler.
|
|
302
|
+
"""
|
|
303
|
+
contents: dict[str, FileBridgeObject] = {}
|
|
304
|
+
path: str = self.get_path()
|
|
305
|
+
for name in self._handler.list_directory(path):
|
|
306
|
+
file_path: str = path + "/" + name
|
|
307
|
+
if self._handler.is_file(file_path):
|
|
308
|
+
contents[name] = self._handler.get_file_object(file_path)
|
|
309
|
+
else:
|
|
310
|
+
contents[name] = self._handler.get_directory_object(file_path)
|
|
311
|
+
return contents
|
|
312
|
+
|
|
313
|
+
def is_file(self) -> bool:
|
|
314
|
+
return False
|
|
315
|
+
|
|
316
|
+
def get_files(self) -> dict[str, File]:
|
|
317
|
+
"""
|
|
318
|
+
:return: A mapping of file name to File for every file in this Directory.
|
|
319
|
+
This pulls from the cache of this object's related FileBridgeHandler.
|
|
320
|
+
"""
|
|
321
|
+
return {x: y for x, y in self.contents.items() if y.is_file()}
|
|
322
|
+
|
|
323
|
+
def get_directories(self) -> dict[str, Directory]:
|
|
324
|
+
"""
|
|
325
|
+
:return: A mapping of directory name to Directory for every directory in this Directory.
|
|
326
|
+
This pulls from the cache of this object's related FileBridgeHandler.
|
|
327
|
+
"""
|
|
328
|
+
return {x: y for x, y in self.contents.items() if not y.is_file()}
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def split_path(file_path: str) -> tuple[str, str]:
|
|
332
|
+
"""
|
|
333
|
+
:param file_path: A file path where directories are separated the "/" characters.
|
|
334
|
+
:return: A tuple of two strings that splits the path on its last slash. The first string is the name of the
|
|
335
|
+
file/directory at the given file path and the second string is the location to that file.
|
|
336
|
+
"""
|
|
337
|
+
last_slash: int = file_path.rfind("/")
|
|
338
|
+
if last_slash == -1:
|
|
339
|
+
return file_path, None
|
|
340
|
+
return file_path[last_slash + 1:], file_path[:last_slash]
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import re
|
|
2
2
|
from typing import Any, Callable, Iterable
|
|
3
3
|
|
|
4
|
-
from sapiopycommons.general.exceptions import SapioException
|
|
5
|
-
|
|
6
|
-
from sapiopycommons.recordmodel.record_handler import RecordHandler
|
|
7
|
-
|
|
8
4
|
from sapiopycommons.general.aliases import SapioRecord
|
|
9
|
-
|
|
5
|
+
from sapiopycommons.general.exceptions import SapioException
|
|
10
6
|
from sapiopycommons.general.time_util import TimeUtil
|
|
7
|
+
from sapiopycommons.recordmodel.record_handler import RecordHandler
|
|
11
8
|
|
|
12
9
|
FilterList = Iterable[int] | range | Callable[[int, dict[str, Any]], bool] | None
|
|
13
10
|
"""A FilterList is an object used to determine if a row in the file data should be skipped over. This can take the
|