PySimultan 0.1.54__py3-none-any.whl → 0.2__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- PySimultan-0.2.dist-info/LICENSE.txt +17 -0
- PySimultan-0.2.dist-info/METADATA +36 -0
- PySimultan-0.2.dist-info/RECORD +82 -0
- {PySimultan-0.1.54.dist-info → PySimultan-0.2.dist-info}/WHEEL +1 -1
- PySimultan-0.2.dist-info/top_level.txt +1 -0
- {PySimultan → PySimultan2}/__init__.py +11 -6
- PySimultan2/config.py +52 -0
- PySimultan2/data_model.py +713 -0
- PySimultan2/default_types.py +507 -0
- PySimultan2/files.py +371 -0
- PySimultan2/geometry/__init__.py +4 -0
- PySimultan2/geometry/geometry_base.py +654 -0
- PySimultan2/geometry/utils.py +181 -0
- PySimultan2/multi_values.py +277 -0
- PySimultan2/object_mapper.py +200 -0
- PySimultan2/resources/AssimpNet.dll +0 -0
- PySimultan2/resources/AvalonDock.dll +0 -0
- PySimultan2/resources/BruTile.dll +0 -0
- PySimultan2/resources/ClosedXML.dll +0 -0
- PySimultan2/resources/ComponentBuilder.dll +0 -0
- PySimultan/resources/ComponentBuilder.exe.config → PySimultan2/resources/ComponentBuilder.dll.config +6 -2
- PySimultan2/resources/ComponentBuilder.runtimeconfig.json +18 -0
- {PySimultan → PySimultan2}/resources/ComponentBuilder.xml +11877 -6754
- PySimultan2/resources/ControlzEx.dll +0 -0
- PySimultan2/resources/Cyotek.Drawing.BitmapFont.dll +0 -0
- PySimultan2/resources/DocumentFormat.OpenXml.dll +0 -0
- PySimultan2/resources/ExcelNumberFormat.dll +0 -0
- PySimultan2/resources/Fluent.dll +0 -0
- PySimultan2/resources/GeometryViewer.dll +0 -0
- {PySimultan → PySimultan2}/resources/GeometryViewer.xml +2201 -1155
- PySimultan2/resources/HelixToolkit.Core.Wpf.dll +0 -0
- PySimultan2/resources/HelixToolkit.SharpDX.Core.Wpf.dll +0 -0
- PySimultan2/resources/HelixToolkit.SharpDX.Core.dll +0 -0
- PySimultan2/resources/HelixToolkit.dll +0 -0
- PySimultan2/resources/MathNet.Numerics.dll +0 -0
- PySimultan2/resources/Microsoft.AspNetCore.Authorization.dll +0 -0
- PySimultan2/resources/Microsoft.AspNetCore.Metadata.dll +0 -0
- PySimultan2/resources/Microsoft.Extensions.DependencyInjection.Abstractions.dll +0 -0
- PySimultan2/resources/Microsoft.Extensions.Logging.Abstractions.dll +0 -0
- PySimultan2/resources/Microsoft.Extensions.Options.dll +0 -0
- PySimultan2/resources/Microsoft.Extensions.Primitives.dll +0 -0
- PySimultan2/resources/Microsoft.WindowsAPICodePack.Shell.dll +0 -0
- PySimultan2/resources/Microsoft.WindowsAPICodePack.dll +0 -0
- PySimultan2/resources/Microsoft.Xaml.Behaviors.dll +0 -0
- PySimultan2/resources/Newtonsoft.Json.Bson.dll +0 -0
- PySimultan2/resources/Newtonsoft.Json.dll +0 -0
- PySimultan2/resources/SIMULTAN.AutoUpdate.Client.dll +0 -0
- PySimultan2/resources/SIMULTAN.AutoUpdate.DataTransferLibrary.dll +0 -0
- PySimultan2/resources/SIMULTAN.AutoUpdate.DataTransferLibrary.dll.config +15 -0
- PySimultan2/resources/SIMULTAN.Lang.dll +0 -0
- {PySimultan → PySimultan2}/resources/SIMULTAN.Lang.xml +5282 -3252
- PySimultan2/resources/SIMULTAN.Plugins.dll +0 -0
- {PySimultan → PySimultan2}/resources/SIMULTAN.Plugins.xml +110 -0
- PySimultan2/resources/SIMULTAN.UI.dll +0 -0
- {PySimultan → PySimultan2}/resources/SIMULTAN.UI.xml +10483 -4937
- PySimultan2/resources/SIMULTAN.dll +0 -0
- PySimultan2/resources/SIMULTAN.xml +34122 -0
- PySimultan2/resources/SharpDX.D3DCompiler.dll +0 -0
- PySimultan2/resources/SharpDX.DXGI.dll +0 -0
- PySimultan2/resources/SharpDX.Direct2D1.dll +0 -0
- PySimultan2/resources/SharpDX.Direct3D11.dll +0 -0
- PySimultan2/resources/SharpDX.Direct3D9.dll +0 -0
- {PySimultan → PySimultan2}/resources/SharpDX.Mathematics.dll +0 -0
- PySimultan2/resources/SharpDX.dll +0 -0
- {PySimultan → PySimultan2}/resources/SitePlanner.dll +0 -0
- {PySimultan → PySimultan2}/resources/SitePlanner.xml +959 -1110
- PySimultan2/resources/Sprache.dll +0 -0
- PySimultan2/resources/System.Data.OleDb.dll +0 -0
- PySimultan2/resources/System.Net.Http.Formatting.dll +0 -0
- PySimultan2/resources/XAMLMarkupExtensions.dll +0 -0
- PySimultan2/resources/assimp.dll +0 -0
- {PySimultan → PySimultan2}/resources/defaultsettings.xml +0 -0
- PySimultan2/simultan_object.py +366 -0
- PySimultan2/taxonomy_maps.py +169 -0
- PySimultan2/utils.py +1383 -0
- PySimultan/config.py +0 -57
- PySimultan/data_model.py +0 -376
- PySimultan/default_types.py +0 -1771
- PySimultan/geo_default_types.py +0 -763
- PySimultan/geometry.py +0 -309
- PySimultan/resources/AssimpNet.dll +0 -0
- PySimultan/resources/AssimpNet.xml +0 -12074
- PySimultan/resources/BruTile.dll +0 -0
- PySimultan/resources/BruTile.xml +0 -1845
- PySimultan/resources/ControlzEx.dll +0 -0
- PySimultan/resources/Cyotek.Drawing.BitmapFont.dll +0 -0
- PySimultan/resources/DXFImportExport.dll +0 -0
- PySimultan/resources/DotSpatial.Projections.xml +0 -5879
- PySimultan/resources/EXCELImportExport.dll +0 -0
- PySimultan/resources/Fluent.dll +0 -0
- PySimultan/resources/Fluent.xml +0 -9103
- PySimultan/resources/GeoJSON.Net.dll +0 -0
- PySimultan/resources/GeometryViewer.Data.dll +0 -0
- PySimultan/resources/GeometryViewer.Data.xml +0 -4443
- PySimultan/resources/GeometryViewer.Shared.dll +0 -0
- PySimultan/resources/GeometryViewer.Shared.xml +0 -588
- PySimultan/resources/GeometryViewer.dll +0 -0
- PySimultan/resources/HelixToolkit.Wpf.SharpDX.dll +0 -0
- PySimultan/resources/HelixToolkit.Wpf.SharpDX.xml +0 -43219
- PySimultan/resources/HelixToolkit.Wpf.dll +0 -0
- PySimultan/resources/HelixToolkit.Wpf.xml +0 -22363
- PySimultan/resources/HelixToolkit.dll +0 -0
- PySimultan/resources/HelixToolkit.xml +0 -144
- PySimultan/resources/ImportLog_SMART_CAMPUS_TU_WIEN_BIBLIOTHEK_20210305_ZONENMODELL_23_11_2021-06_54_54.txt +0 -1
- PySimultan/resources/LibGit2Sharp.dll +0 -0
- PySimultan/resources/LibGit2Sharp.dll.config +0 -4
- PySimultan/resources/LibGit2Sharp.xml +0 -13770
- PySimultan/resources/MathNet.Numerics.dll +0 -0
- PySimultan/resources/MathNet.Numerics.xml +0 -57152
- PySimultan/resources/Microsoft.WindowsAPICodePack.Shell.dll +0 -0
- PySimultan/resources/Microsoft.WindowsAPICodePack.Shell.xml +0 -25496
- PySimultan/resources/Microsoft.WindowsAPICodePack.dll +0 -0
- PySimultan/resources/Microsoft.WindowsAPICodePack.xml +0 -2934
- PySimultan/resources/Newtonsoft.Json.dll +0 -0
- PySimultan/resources/Newtonsoft.Json.xml +0 -11305
- PySimultan/resources/ParameterStructure.dll +0 -0
- PySimultan/resources/ParameterStructure.xml +0 -10750
- PySimultan/resources/SIMULTAN.AutoUpdate.Client.dll +0 -0
- PySimultan/resources/SIMULTAN.DataExchange.dll +0 -0
- PySimultan/resources/SIMULTAN.Lang.dll +0 -0
- PySimultan/resources/SIMULTAN.Legacy.dll +0 -0
- PySimultan/resources/SIMULTAN.Legacy.xml +0 -254
- PySimultan/resources/SIMULTAN.Plugins.dll +0 -0
- PySimultan/resources/SIMULTAN.Project.dll +0 -0
- PySimultan/resources/SIMULTAN.Project.dll.config +0 -11
- PySimultan/resources/SIMULTAN.UI.dll +0 -0
- PySimultan/resources/SIMULTAN.Util.dll +0 -0
- PySimultan/resources/SIMULTAN.Util.xml +0 -3430
- PySimultan/resources/SharpDX.D3DCompiler.dll +0 -0
- PySimultan/resources/SharpDX.D3DCompiler.xml +0 -5897
- PySimultan/resources/SharpDX.DXGI.dll +0 -0
- PySimultan/resources/SharpDX.DXGI.xml +0 -8737
- PySimultan/resources/SharpDX.Direct2D1.dll +0 -0
- PySimultan/resources/SharpDX.Direct2D1.xml +0 -46691
- PySimultan/resources/SharpDX.Direct3D11.dll +0 -0
- PySimultan/resources/SharpDX.Direct3D11.xml +0 -31826
- PySimultan/resources/SharpDX.Direct3D9.dll +0 -0
- PySimultan/resources/SharpDX.Direct3D9.xml +0 -36489
- PySimultan/resources/SharpDX.Mathematics.xml +0 -16449
- PySimultan/resources/SharpDX.dll +0 -0
- PySimultan/resources/Sprache.dll +0 -0
- PySimultan/resources/Sprache.xml +0 -1199
- PySimultan/resources/System.Net.Http.Formatting.dll +0 -0
- PySimultan/resources/System.Net.Http.Formatting.xml +0 -2094
- PySimultan/resources/System.Windows.Interactivity.dll +0 -0
- PySimultan/resources/TalkGit.dll +0 -0
- PySimultan/resources/TalkGit.dll.config +0 -11
- PySimultan/resources/WebServiceConnector.dll +0 -0
- PySimultan/resources/WebServiceConnector.dll.config +0 -11
- PySimultan/resources/XAMLMarkupExtensions.dll +0 -0
- PySimultan/resources/XAMLMarkupExtensions.xml +0 -862
- PySimultan/resources/Xceed.Wpf.AvalonDock.Themes.Aero.dll +0 -0
- PySimultan/resources/Xceed.Wpf.AvalonDock.Themes.Metro.dll +0 -0
- PySimultan/resources/Xceed.Wpf.AvalonDock.Themes.VS2010.dll +0 -0
- PySimultan/resources/Xceed.Wpf.AvalonDock.dll +0 -0
- PySimultan/resources/Xceed.Wpf.Toolkit.dll +0 -0
- PySimultan/resources/assimp.dll +0 -0
- PySimultan/resources/schema.yaml +0 -13
- PySimultan/resources/simultan_exception_07_10_2021-08_34_24.txt +0 -17
- PySimultan/resources/simultan_exception_07_10_2021-10_13_31.txt +0 -17
- PySimultan/resources/simultan_exception_11_10_2021-19_45_42.txt +0 -17
- PySimultan/resources/simultan_exception_22_11_2021-16_41_52.txt +0 -14
- PySimultan/resources/simultan_exception_22_11_2021-17_37_40.txt +0 -14
- PySimultan/simultan_geometry_utils.py +0 -81
- PySimultan/simultan_utils.py +0 -165
- PySimultan/slots.py +0 -51
- PySimultan/template_tools.py +0 -631
- PySimultan/utils.py +0 -298
- PySimultan-0.1.54.dist-info/LICENSE +0 -674
- PySimultan-0.1.54.dist-info/LICENSE.txt +0 -674
- PySimultan-0.1.54.dist-info/METADATA +0 -690
- PySimultan-0.1.54.dist-info/RECORD +0 -119
- PySimultan-0.1.54.dist-info/entry_points.txt +0 -3
- PySimultan-0.1.54.dist-info/top_level.txt +0 -1
- PySimultan-0.1.54.dist-info/zip-safe +0 -1
- {PySimultan → PySimultan2}/resources/DotSpatial.Projections.dll +0 -0
- {PySimultan → PySimultan2}/resources/GeometryViewer.dll.config +0 -0
- {PySimultan → PySimultan2}/resources/SIMULTAN.AutoUpdate.Client.dll.config +0 -0
- {PySimultan → PySimultan2}/resources/SitePlanner.dll.config +0 -0
- {PySimultan → PySimultan2}/resources/Sprache.Calc.dll +0 -0
- {PySimultan → PySimultan2}/resources/__init__.py +0 -0
- {PySimultan → PySimultan2}/resources/setup.bat +0 -0
PySimultan2/files.py
ADDED
@@ -0,0 +1,371 @@
|
|
1
|
+
"""
|
2
|
+
This module contains classes and functions to handle files and directories.
|
3
|
+
"""
|
4
|
+
from __future__ import annotations
|
5
|
+
from datetime import datetime
|
6
|
+
|
7
|
+
import contextlib
|
8
|
+
import os
|
9
|
+
import io
|
10
|
+
import shutil
|
11
|
+
import tempfile
|
12
|
+
from typing import List, Union, Optional
|
13
|
+
import shutil
|
14
|
+
import zipfile
|
15
|
+
# from System.IO import FileInfo # public FileInfo (string fileName);
|
16
|
+
|
17
|
+
from SIMULTAN.Data.Assets import ResourceEntry, ResourceFileEntry, ContainedResourceFileEntry, Asset
|
18
|
+
from SIMULTAN.Data.Taxonomy import SimTaxonomyEntry, SimTaxonomyEntryReference, SimTaxonomy
|
19
|
+
from SIMULTAN.Data.Components import SimComponent, ComponentMapping
|
20
|
+
|
21
|
+
from .config import default_data_model
|
22
|
+
|
23
|
+
from typing import TYPE_CHECKING
|
24
|
+
if TYPE_CHECKING:
|
25
|
+
from .data_model import DataModel
|
26
|
+
from .simultan_object import SimultanObject
|
27
|
+
|
28
|
+
|
29
|
+
@contextlib.contextmanager
|
30
|
+
def cd(newdir, cleanup=lambda: True):
|
31
|
+
prevdir = os.getcwd()
|
32
|
+
os.chdir(os.path.expanduser(newdir))
|
33
|
+
try:
|
34
|
+
yield
|
35
|
+
finally:
|
36
|
+
os.chdir(prevdir)
|
37
|
+
cleanup()
|
38
|
+
|
39
|
+
|
40
|
+
@contextlib.contextmanager
|
41
|
+
def tempdir():
|
42
|
+
dir_path = tempfile.mkdtemp()
|
43
|
+
|
44
|
+
def cleanup():
|
45
|
+
shutil.rmtree(dir_path)
|
46
|
+
|
47
|
+
with cd(dir_path, cleanup):
|
48
|
+
yield dir_path
|
49
|
+
|
50
|
+
|
51
|
+
def add_tag_to_resource(resource: Union[ResourceFileEntry, ContainedResourceFileEntry],
|
52
|
+
tag: Union[SimTaxonomyEntry, SimTaxonomyEntryReference]):
|
53
|
+
"""
|
54
|
+
Add a tag to an asset.
|
55
|
+
|
56
|
+
:param resource: The resource to add the tag to.
|
57
|
+
:param tag: The tag to add to the asset.
|
58
|
+
:return: None
|
59
|
+
"""
|
60
|
+
if isinstance(tag, SimTaxonomyEntry):
|
61
|
+
tag = SimTaxonomyEntryReference(tag)
|
62
|
+
|
63
|
+
if tag not in resource.Tags:
|
64
|
+
resource.Tags.Add(tag)
|
65
|
+
|
66
|
+
|
67
|
+
def add_asset_to_component(comp: [SimComponent, SimultanObject],
|
68
|
+
asset: Union[ResourceFileEntry, ContainedResourceFileEntry],
|
69
|
+
content_id: str = '',
|
70
|
+
tag: SimTaxonomyEntry = None) -> Asset:
|
71
|
+
"""
|
72
|
+
Add an asset to a component with a content id.
|
73
|
+
:param comp: Component to add the asset; ParameterStructure.Components.SimComponent
|
74
|
+
:param asset: Asset to be added; ParameterStructure.Assets.ResourceFileEntry
|
75
|
+
:param content_id: Content id of the asset; string; E.g. '0' as the page of a pdf
|
76
|
+
:param tag: Tag to be added to the asset.
|
77
|
+
:return:
|
78
|
+
"""
|
79
|
+
wrapped_obj = comp if isinstance(comp, SimComponent) else comp._wrapped_obj
|
80
|
+
|
81
|
+
if tag is not None:
|
82
|
+
add_tag_to_resource(asset, tag)
|
83
|
+
|
84
|
+
return ComponentMapping.AddAsset(wrapped_obj, asset, content_id)
|
85
|
+
|
86
|
+
|
87
|
+
def remove_asset_from_component(comp: Union[SimComponent, SimultanObject],
|
88
|
+
asset: Asset) -> None:
|
89
|
+
"""
|
90
|
+
Remove an asset from a component with a content id.
|
91
|
+
:param comp: Component to remove the asset from; ParameterStructure.Components.SimComponent
|
92
|
+
:param asset:
|
93
|
+
:return:
|
94
|
+
"""
|
95
|
+
wrapped_obj = comp if isinstance(comp, SimComponent) else comp._wrapped_obj
|
96
|
+
return ComponentMapping.RemoveAsset(wrapped_obj, asset)
|
97
|
+
|
98
|
+
|
99
|
+
def create_asset_from_string(filename: str,
|
100
|
+
content: str,
|
101
|
+
data_model: DataModel,
|
102
|
+
tag: Optional[Union[SimTaxonomyEntry, SimTaxonomyEntryReference]] = None) -> ResourceFileEntry:
|
103
|
+
"""
|
104
|
+
Create a new asset from a string. The asset is added to the data model.
|
105
|
+
:param filename: Name of the file to be created. E.g. 'new_file.txt'
|
106
|
+
:param content: Content of the file. E.g. 'This is the content of the file.'
|
107
|
+
:param data_model: Data model to add the asset to.
|
108
|
+
:param tag: Tag to be added to the asset.
|
109
|
+
:return: ResourceFileEntry
|
110
|
+
"""
|
111
|
+
with tempdir() as dirpath:
|
112
|
+
filepath = os.path.join(dirpath, filename)
|
113
|
+
with open(filepath, 'w') as f:
|
114
|
+
f.write(content)
|
115
|
+
|
116
|
+
resource = data_model.add_resource(filepath)
|
117
|
+
|
118
|
+
if tag is not None:
|
119
|
+
add_tag_to_resource(resource, tag)
|
120
|
+
|
121
|
+
return resource
|
122
|
+
|
123
|
+
|
124
|
+
def create_asset_from_str_io(filename: str,
|
125
|
+
content: io.StringIO,
|
126
|
+
data_model: DataModel,
|
127
|
+
tag: Union[SimTaxonomyEntry, SimTaxonomyEntryReference] = None) -> ResourceFileEntry:
|
128
|
+
"""
|
129
|
+
Create a new asset from a string io. The asset is added to the data model.
|
130
|
+
:param filename: Name of the file to be created. E.g. 'new_file.txt'
|
131
|
+
:param content: Content of the file. E.g. 'This is the content of the file.'
|
132
|
+
:param data_model: Data model to add the asset to.
|
133
|
+
:param tag: Tag to be added to the asset.
|
134
|
+
:return: ResourceFileEntry
|
135
|
+
"""
|
136
|
+
with tempdir() as dirpath:
|
137
|
+
filepath = os.path.join(dirpath, filename)
|
138
|
+
with open(filepath, 'w') as f:
|
139
|
+
f.write(content.getvalue())
|
140
|
+
|
141
|
+
resource = data_model.add_resource(filepath)
|
142
|
+
|
143
|
+
if tag is not None:
|
144
|
+
add_tag_to_resource(resource, tag)
|
145
|
+
|
146
|
+
return resource
|
147
|
+
|
148
|
+
|
149
|
+
def create_asset_from_file(file_info: FileInfo,
|
150
|
+
data_model: DataModel,
|
151
|
+
tag: Union[SimTaxonomyEntry, SimTaxonomyEntryReference] = None) -> Union[
|
152
|
+
ResourceFileEntry, ContainedResourceFileEntry]:
|
153
|
+
"""
|
154
|
+
Create a new asset from a file. The asset is added to the data model.
|
155
|
+
:param file_info: FileInfo object of the file to be added.
|
156
|
+
:param data_model: Data model to add the asset to.
|
157
|
+
:param tag: Tag to be added to the asset.
|
158
|
+
:return: ResourceFileEntry
|
159
|
+
"""
|
160
|
+
resource = data_model.add_resource(file_info.file_path)
|
161
|
+
|
162
|
+
if tag is not None:
|
163
|
+
add_tag_to_resource(resource, tag)
|
164
|
+
|
165
|
+
return resource
|
166
|
+
|
167
|
+
|
168
|
+
class MetaMock(type):
|
169
|
+
def __call__(cls, *args, **kwargs):
|
170
|
+
resource_entry = kwargs.get('resource_entry', None)
|
171
|
+
if resource_entry is not None and hasattr(resource_entry, 'Key'):
|
172
|
+
obj = cls._cls_instances.get(resource_entry.Key, None)
|
173
|
+
if obj is not None:
|
174
|
+
return obj
|
175
|
+
|
176
|
+
obj = cls.__new__(cls)
|
177
|
+
obj.__init__(*args, **kwargs)
|
178
|
+
if obj.resource_entry is not None:
|
179
|
+
cls._cls_instances[obj.resource_entry.Key] = obj
|
180
|
+
return obj
|
181
|
+
|
182
|
+
|
183
|
+
class FileInfo(object, metaclass=MetaMock):
|
184
|
+
|
185
|
+
_cls_instances = {}
|
186
|
+
|
187
|
+
@classmethod
|
188
|
+
def from_string(cls, filename: str, content: str, *args, **kwargs) -> FileInfo:
|
189
|
+
"""
|
190
|
+
Create a file info object from a string.
|
191
|
+
:param filename: Name of the file to be created. E.g. 'new_file.txt'
|
192
|
+
:param content: Content of the file. E.g. 'This is the content of the file.'
|
193
|
+
:param args:
|
194
|
+
:param kwargs:
|
195
|
+
:return: FileInfo
|
196
|
+
"""
|
197
|
+
|
198
|
+
data_model = kwargs.get('data_model', default_data_model)
|
199
|
+
resource = create_asset_from_string(filename, content, *args, **kwargs)
|
200
|
+
|
201
|
+
file_info = cls(resource_entry=resource,
|
202
|
+
data_model=data_model)
|
203
|
+
file_info.write_content(content)
|
204
|
+
return file_info
|
205
|
+
|
206
|
+
def __init__(self, file_path=None, *args, **kwargs):
|
207
|
+
"""
|
208
|
+
Custom file info object to be used with the with statement. This object is used to open a file and close it
|
209
|
+
automatically.
|
210
|
+
Example:
|
211
|
+
|
212
|
+
file_path = 'path/to/file.txt'
|
213
|
+
file_info = FileInfo(file_path, 'r')
|
214
|
+
|
215
|
+
with file_info as f:
|
216
|
+
print(f.read())
|
217
|
+
|
218
|
+
:param file_path:
|
219
|
+
:param args:
|
220
|
+
:param kwargs:
|
221
|
+
"""
|
222
|
+
# do custom stuff here
|
223
|
+
self._resource_entry: Union[ResourceFileEntry, ContainedResourceFileEntry, None] = None
|
224
|
+
|
225
|
+
if file_path is not None:
|
226
|
+
self.file_path: str = file_path
|
227
|
+
else:
|
228
|
+
self.file_path = kwargs.get('resource_entry').File.FullPath
|
229
|
+
|
230
|
+
self.data_model: Union[DataModel, None] = kwargs.get('data_model', None)
|
231
|
+
self.resource_entry = kwargs.get('resource_entry', None)
|
232
|
+
|
233
|
+
self.encoding = kwargs.get('encoding', 'utf-8')
|
234
|
+
|
235
|
+
self.args = args
|
236
|
+
self.kwargs = kwargs
|
237
|
+
|
238
|
+
@property
|
239
|
+
def resource_entry(self) -> Union[ResourceFileEntry, ContainedResourceFileEntry, None]:
|
240
|
+
if self._resource_entry is None:
|
241
|
+
if self.data_model is None:
|
242
|
+
self.data_model = default_data_model
|
243
|
+
if self.data_model is not None:
|
244
|
+
self.resource_entry = self.data_model.add_resource(self.file_path)
|
245
|
+
return self._resource_entry
|
246
|
+
|
247
|
+
@resource_entry.setter
|
248
|
+
def resource_entry(self, value):
|
249
|
+
|
250
|
+
if value is not None:
|
251
|
+
self._cls_instances[value.Key] = self
|
252
|
+
else:
|
253
|
+
del self._cls_instances[self._resource_entry.Key]
|
254
|
+
self._resource_entry = value
|
255
|
+
|
256
|
+
@property
|
257
|
+
def file_size(self) -> int:
|
258
|
+
return os.path.getsize(self.file_path)
|
259
|
+
|
260
|
+
@property
|
261
|
+
def last_modified(self) -> datetime:
|
262
|
+
return datetime.fromtimestamp(os.path.getmtime(self.file_path))
|
263
|
+
|
264
|
+
@resource_entry.setter
|
265
|
+
def resource_entry(self, value):
|
266
|
+
self._resource_entry = value
|
267
|
+
|
268
|
+
@property
|
269
|
+
def name(self) -> str:
|
270
|
+
return os.path.basename(self.file_path)
|
271
|
+
|
272
|
+
@name.setter
|
273
|
+
def name(self, value: str):
|
274
|
+
os.rename(self.file_path, os.path.join(os.path.dirname(self.file_path), value))
|
275
|
+
self.file_path = os.path.join(os.path.dirname(self.file_path), value)
|
276
|
+
|
277
|
+
@property
|
278
|
+
def full_path(self) -> str:
|
279
|
+
return os.path.abspath(self.file_path)
|
280
|
+
|
281
|
+
@property
|
282
|
+
def size(self) -> int:
|
283
|
+
return os.path.getsize(self.file_path)
|
284
|
+
|
285
|
+
@property
|
286
|
+
def is_zip(self) -> bool:
|
287
|
+
return self.file_path.endswith('.zip')
|
288
|
+
|
289
|
+
@property
|
290
|
+
def files(self) -> List[str]:
|
291
|
+
if self.is_zip:
|
292
|
+
with zipfile.ZipFile(self.file_path, 'r') as z:
|
293
|
+
return z.namelist()
|
294
|
+
else:
|
295
|
+
return []
|
296
|
+
|
297
|
+
@property
|
298
|
+
def exists(self) -> bool:
|
299
|
+
return os.path.exists(self.file_path)
|
300
|
+
|
301
|
+
@property
|
302
|
+
def content(self) -> str:
|
303
|
+
return self.get_content(encoding=self.encoding)
|
304
|
+
|
305
|
+
@content.setter
|
306
|
+
def content(self, value: str):
|
307
|
+
self.write_content(value)
|
308
|
+
|
309
|
+
def __enter__(self):
|
310
|
+
self.file_obj = open(self.file_path, *self.args, **self.kwargs)
|
311
|
+
return self.file_obj
|
312
|
+
|
313
|
+
def __exit__(self, *args):
|
314
|
+
self.file_obj.close()
|
315
|
+
|
316
|
+
def get_content(self, encoding='utf-8') -> Optional[Union[str, dict[str, str]]]:
|
317
|
+
"""
|
318
|
+
Get the content of the file.
|
319
|
+
:param encoding: Encoding of the file.
|
320
|
+
:return: File content
|
321
|
+
"""
|
322
|
+
if self.exists:
|
323
|
+
if self.file_path.endswith('.zip'):
|
324
|
+
content = {}
|
325
|
+
with zipfile.ZipFile(self.file_path, 'r') as z:
|
326
|
+
for file in z.namelist():
|
327
|
+
content[file] = z.read(file).decode(encoding)
|
328
|
+
return content
|
329
|
+
else:
|
330
|
+
with open(self.file_path, 'r', encoding=encoding) as f:
|
331
|
+
return f.read()
|
332
|
+
else:
|
333
|
+
return
|
334
|
+
|
335
|
+
def copy(self, new_file_path: Optional) -> FileInfo:
|
336
|
+
"""
|
337
|
+
Copy the file to a new location.
|
338
|
+
:param new_file_path: New file path.
|
339
|
+
:return: FileInfo
|
340
|
+
"""
|
341
|
+
shutil.copy(self.full_path, new_file_path)
|
342
|
+
return FileInfo(new_file_path)
|
343
|
+
|
344
|
+
def write_content(self, content: str, encoding='utf-8') -> None:
|
345
|
+
"""
|
346
|
+
Write content to the file.
|
347
|
+
:param content: Content to be written to the file.
|
348
|
+
:param encoding: Encoding of the file.
|
349
|
+
:return: None
|
350
|
+
"""
|
351
|
+
with open(self.file_path, 'w', encoding=encoding) as f:
|
352
|
+
f.write(content)
|
353
|
+
|
354
|
+
def append_content(self, content: str, encoding='utf-8') -> None:
|
355
|
+
"""
|
356
|
+
Append content to the file.
|
357
|
+
:param content: Content to be appended to the file.
|
358
|
+
:param encoding: Encoding of the file.
|
359
|
+
:return: None
|
360
|
+
"""
|
361
|
+
with open(self.file_path, 'a', encoding=encoding) as f:
|
362
|
+
f.write(content)
|
363
|
+
|
364
|
+
def delete(self) -> None:
|
365
|
+
"""
|
366
|
+
Delete the file.
|
367
|
+
:return: None
|
368
|
+
"""
|
369
|
+
if self.resource_entry is not None:
|
370
|
+
self.data_model.delete_resource(self.resource_entry)
|
371
|
+
os.remove(self.file_path)
|