fix/reindex_before_collection #21

Merged
Hazel merged 32 commits from fix/reindex_before_collection into experimental 2024-05-06 17:36:28 +00:00
5 changed files with 34 additions and 19 deletions
Showing only changes of commit 3737e0dc81 - Show all commits

View File

@ -7,7 +7,7 @@ logging.getLogger().setLevel(logging.DEBUG)
if __name__ == "__main__":
commands = [
"s: #a Ghost Bath",
"d: 4",
"d: 14",
]

View File

@ -13,8 +13,8 @@ class Collection(Generic[T]):
_data: List[T]
_indexed_values: Dict[str, set]
_indexed_to_objects: Dict[any, list]
_indexed_from_id: Dict[int, Dict[str, Any]]
_indexed_values: Dict[str, Dict[Any, T]]
shallow_list = property(fget=lambda self: self.data)
@ -74,6 +74,10 @@ class Collection(Generic[T]):
del self._indexed_from_id[obj_id]
def _remap(self):
for e in self:
self._map_element(e)
def _find_object(self, __object: T) -> Optional[T]:
for name, value in __object.indexing_values:
if value in self._indexed_values[name]:
@ -94,6 +98,7 @@ class Collection(Generic[T]):
if __object is None:
return
self._remap()
existing_object = self._find_object(__object)
if existing_object is None:
@ -112,15 +117,10 @@ class Collection(Generic[T]):
b = __object.__getattribute__(attribute)
object_trace(f"Syncing [{a}{id(a)}] = [{b}{id(b)}]")
data_to_extend = b.data
a._collection_for.update(b._collection_for)
for synced_with, key in b._collection_for.items():
synced_with.__setattr__(key, a)
a.extend(data_to_extend)
else:
# merge only if the two objects are not the same
if existing_object.id == __object.id:

View File

@ -32,7 +32,6 @@ class InnerData:
"""
Attribute versions keep track, of if the attribute has been changed.
"""
_attribute_versions: Dict[str, int] = None
def __init__(self, object_type, **kwargs):
self._refers_to_instances = set()
@ -249,6 +248,8 @@ class OuterProxy:
return r
INDEX_DEPENDS_ON: List[str] = []
@property
def indexing_values(self) -> List[Tuple[str, object]]:
"""

View File

@ -43,7 +43,8 @@ def get_collection_string(
template: str,
ignore_titles: Set[str] = None,
background: BColors = OPTION_BACKGROUND,
foreground: BColors = OPTION_FOREGROUND
foreground: BColors = OPTION_FOREGROUND,
add_id: bool = False,
) -> str:
if collection.empty:
return ""
@ -55,8 +56,15 @@ def get_collection_string(
r = background
def get_element_str(element) -> str:
nonlocal add_id
r = element.title_string.strip()
if add_id:
r += " " + str(element.id)
return r
element: Base
titel_list: List[str] = [element.title_string.strip() for element in collection if element.title_string not in ignore_titles]
titel_list: List[str] = [get_element_str(element) for element in collection if element.title_string not in ignore_titles]
for i, titel in enumerate(titel_list):
delimiter = ", "
@ -151,13 +159,14 @@ class Song(Base):
self.album_collection.extend(object_list)
return
INDEX_DEPENDS_ON = ("title", "isrc", "source_collection")
@property
def indexing_values(self) -> List[Tuple[str, object]]:
return [
('id', self.id),
('title', unify(self.title)),
('isrc', self.isrc),
*[('url', source.url) for source in self.source_collection]
*self.source_collection.indexing_values(),
]
@property
@ -304,13 +313,14 @@ class Album(Base):
self.label_collection.extend(object_list)
return
INDEX_DEPENDS_ON = ("title", "barcode", "source_collection")
@property
def indexing_values(self) -> List[Tuple[str, object]]:
return [
('id', self.id),
('title', unify(self.title)),
('barcode', self.barcode),
*[('url', source.url) for source in self.source_collection]
*self.source_collection.indexing_values(),
]
@property
@ -545,13 +555,13 @@ class Artist(Base):
# replace the old collection with the new one
self.main_album_collection: Collection = Collection(data=album_list, element_type=Album)
INDEX_DEPENDS_ON = ("name", "source_collection", "contact_collection")
@property
def indexing_values(self) -> List[Tuple[str, object]]:
return [
('id', self.id),
('name', unify(self.name)),
*[('url', source.url) for source in self.source_collection],
*[('contact', contact.value) for contact in self.contact_collection]
*[('contact', contact.value) for contact in self.contact_collection],
*self.source_collection.indexing_values(),
]
@property

View File

@ -167,4 +167,8 @@ class SourceCollection:
@property
def homepage_list(self) -> List[str]:
return [source.homepage for source in self.source_pages]
return [source.homepage for source in self.source_pages]
def indexing_values(self) -> Generator[Tuple[str, str], None, None]:
for index in self._indexed_sources:
yield "url", index