diff --git a/tagstudio/src/core/library.py b/tagstudio/src/core/library.py index 7eedb8b82..e70ac4b36 100644 --- a/tagstudio/src/core/library.py +++ b/tagstudio/src/core/library.py @@ -2167,14 +2167,18 @@ def mirror_entry_fields(self, entry_ids: list[int]) -> None: def get_field_attr(self, entry_field: dict, attribute: str): """Returns the value of a specified attribute inside an Entry field.""" - if attribute.lower() == "id": - return list(entry_field.keys())[0] - elif attribute.lower() == "content": - return entry_field[self.get_field_attr(entry_field, "id")] - else: - return self.get_field_obj(self.get_field_attr(entry_field, "id"))[ + match attribute.lower(): + case "id": + return val[0] if not any(val := list(entry_field.keys())[:1]) else -1 + case "content": + return entry_field[self.get_field_attr(entry_field, "id")] + case _: + #_ensure_field: dict = self.get_field_obj(self.get_field_attr(entry_field, "id")) + #return _ensure_field.get(attribute.lower()) + return self.get_field_obj(self.get_field_attr(entry_field, "id"))[ attribute.lower() ] + def get_field_obj(self, field_id: int) -> dict: """ diff --git a/tagstudio/src/core/ts_core.py b/tagstudio/src/core/ts_core.py index 63ac30e63..7d3d70a60 100644 --- a/tagstudio/src/core/ts_core.py +++ b/tagstudio/src/core/ts_core.py @@ -109,63 +109,46 @@ def match_conditions(self, entry_id: int) -> None: cond_file = self.lib.library_dir / TS_FOLDER_NAME / "conditions.json" # TODO: Make this stored somewhere better instead of temporarily in this JSON file. entry: Entry = self.lib.get_entry(entry_id) + if not cond_file.is_file(): + print("No conditions file found.") + return + try: - if cond_file.is_file(): - with open(cond_file, "r", encoding="utf8") as f: - json_dump = json.load(f) - for c in json_dump["conditions"]: - match: bool = False - for path_c in c["path_conditions"]: - if str(Path(path_c).resolve()) in str(entry.path): - match = True - break - if match: - if fields := c.get("fields"): - for field in fields: - field_id = self.lib.get_field_attr(field, "id") - content = field[field_id] - - if ( - self.lib.get_field_obj(int(field_id))["type"] - == "tag_box" - ): - existing_fields: list[int] = ( - self.lib.get_field_index_in_entry( - entry, field_id - ) - ) - if existing_fields: - self.lib.update_entry_field( - entry_id, - existing_fields[0], - content, - "append", - ) - else: - self.lib.add_field_to_entry( - entry_id, field_id - ) - self.lib.update_entry_field( - entry_id, -1, content, "append" - ) - - if ( - self.lib.get_field_obj(int(field_id))["type"] - in TEXT_FIELDS - ): - if not self.lib.does_field_content_exist( - entry_id, field_id, content - ): - self.lib.add_field_to_entry( - entry_id, field_id - ) - self.lib.update_entry_field( - entry_id, -1, content, "replace" - ) - except: - print("Error in match_conditions...") - # input() - pass + f = open(cond_file, "r", encoding="utf8") + except Exception as e: + print(f"Error opening conditions file: {e}") + return + + with f: + json_dump = json.load(f) + for c in json_dump.get("conditions", []): + match: bool = any(str(Path(path_c).resolve()) in str(entry.path) for path_c in c.get("path_conditions", [])) + + if not match: + continue + + if not any(fields := c.get("fields", [])): + continue + + for field in fields: + field_id = self.lib.get_field_attr(field, "id") + content = field.get(field_id) + + if self.lib.get_field_obj(int(field_id))["type"] == "tag_box": + existing_fields: list[int] = self.lib.get_field_index_in_entry(entry, field_id) + + if existing_fields: + self.lib.update_entry_field(entry_id, existing_fields[0], content, "append") + else: + self.lib.add_field_to_entry(entry_id, field_id) + self.lib.update_entry_field(entry_id, -1, content, "append") + + if self.lib.get_field_obj(int(field_id))["type"] in TEXT_FIELDS: + if not self.lib.does_field_content_exist(entry_id, field_id, content): + self.lib.add_field_to_entry(entry_id, field_id) + self.lib.update_entry_field(entry_id, -1, content, "replace") + + def build_url(self, entry_id: int, source: str): """Tries to rebuild a source URL given a specific filename structure."""