Skip to content

[chore]Make get_field_attr safe and improve control flow in ts_core.py #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions tagstudio/src/core/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -2167,14 +2167,15 @@ 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"))[
attribute.lower()
]
match attribute.lower():
case "id":
return val[0] if not any(val := list(entry_field.keys())[:1]) else -1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the -1 used for? this looked at first glance as a rewrite to structural pattern matching, are there anything else?
the dense nature of the code makes it hard to read compared to list(entry_field.keys())[0]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

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())


def get_field_obj(self, field_id: int) -> dict:
"""
Expand Down
95 changes: 39 additions & 56 deletions tagstudio/src/core/ts_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Loading