This commit is contained in:
Andrej730
2024-05-31 18:02:20 +05:00
parent c05abc4d5b
commit 162e66b775
5 changed files with 47 additions and 27 deletions
+17 -17
View File
@@ -24,7 +24,7 @@ import zipfile
import functools
import ifcopenshell
from pathlib import Path
from typing import Optional, Any, Union, Callable
from typing import Optional, Any, Union, Callable, Generator
from . import ifcopenshell_wrapper
from .entity_instance import entity_instance
@@ -59,13 +59,13 @@ class Transaction:
value,
)
def batch(self):
def batch(self) -> None:
self.is_batched = True
self.batch_delete_index = len(self.operations)
self.batch_delete_ids = set()
self.batch_inverses = []
def unbatch(self):
def unbatch(self) -> None:
for inverses in self.batch_inverses:
if inverses:
self.operations.insert(self.batch_delete_index, {"action": "batch_delete", "inverses": inverses})
@@ -74,11 +74,11 @@ class Transaction:
self.batch_delete_ids = set()
self.batch_inverses = []
def store_create(self, element):
def store_create(self, element: ifcopenshell.entity_instance) -> None:
if element.id():
self.operations.append({"action": "create", "value": self.serialise_entity_instance(element)})
def store_edit(self, element, index, value):
def store_edit(self, element: ifcopenshell.entity_instance, index: int, value: Any) -> None:
if element.id():
self.operations.append(
{
@@ -120,7 +120,7 @@ class Transaction:
return False
return value == element
def rollback(self):
def rollback(self) -> None:
for operation in self.operations[::-1]:
if operation["action"] == "create":
element = self.file.by_id(operation["value"]["id"])
@@ -153,7 +153,7 @@ class Transaction:
for index, value in data:
inverse[index] = self.unserialise_value(inverse, value)
def commit(self):
def commit(self) -> None:
for operation in self.operations:
if operation["action"] == "create":
e = self.file.create_entity(operation["value"]["type"], id=operation["value"]["id"])
@@ -260,19 +260,19 @@ class file:
file_dict[self.file_pointer()] = weakref.ref(self)
def __del__(self):
def __del__(self) -> None:
del file_dict[self.file_pointer()]
def set_history_size(self, size):
def set_history_size(self, size: int) -> None:
self.history_size = size
while len(self.history) > self.history_size:
self.history.pop(0)
def begin_transaction(self):
def begin_transaction(self) -> None:
if self.history_size:
self.transaction = Transaction(self)
def end_transaction(self):
def end_transaction(self) -> None:
if self.transaction:
self.history.append(self.transaction)
if len(self.history) > self.history_size:
@@ -280,19 +280,19 @@ class file:
self.future = []
self.transaction = None
def discard_transaction(self):
def discard_transaction(self) -> None:
if self.transaction:
self.transaction.rollback()
self.transaction = None
def undo(self):
def undo(self) -> None:
if not self.history:
return
transaction = self.history.pop()
transaction.rollback()
self.future.append(transaction)
def redo(self):
def redo(self) -> None:
if not self.future:
return
transaction = self.future.pop()
@@ -405,7 +405,7 @@ class file:
else:
return getattr(self.wrapped_data, attr)
def __getitem__(self, key):
def __getitem__(self, key: Union[numbers.Integral, str, bytes]) -> entity_instance:
if isinstance(key, numbers.Integral):
return entity_instance(self.wrapped_data.by_id(key), self)
elif isinstance(key, (str, bytes)):
@@ -501,7 +501,7 @@ class file:
return [entity_instance(e, self) for e in fn(inst.wrapped_data, max_levels)]
def get_inverse(
self, inst: ifcopenshell.entity_instance, allow_duplicate=False, with_attribute_indices=False
self, inst: ifcopenshell.entity_instance, allow_duplicate: bool = False, with_attribute_indices: bool = False
) -> list[ifcopenshell.entity_instance]:
"""Return a list of entities that reference this entity
@@ -572,7 +572,7 @@ class file:
self.transaction.unbatch()
return self.wrapped_data.unbatch()
def __iter__(self):
def __iter__(self) -> Generator[ifcopenshell.entity_instance, None, None]:
return iter(self[id] for id in self.wrapped_data.entity_names())
def write(self, path: "os.PathLike | str", format: Optional[str] = None, zipped: bool = False) -> None: