Skip to content

🏷️ Add strict typing dicts for ts_library.json #40

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

Merged
Merged
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
33 changes: 33 additions & 0 deletions tagstudio/src/core/json_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import TypedDict

class Json_Libary(TypedDict("",{"ts-version":str})):
#"ts-version": str
tags: "list[Json_Tag]"
collations: "list[Json_Collation]"
fields: list #TODO
macros: "list[Json_Macro]"
entries: "list[Json_Entry]"

class Json_Base(TypedDict):
id: int

class Json_Tag(Json_Base,total=False):
name: str
aliases: list[str]
color: str
shorthand: str
subtag_ids: list[int]

class Json_Collation(Json_Base,total=False):
title: str
e_ids_and_pages: list[list[int]]
sort_order: str
cover_id: int

class Json_Entry(Json_Base,total=False):
filename: str
path: str
fields: list[dict] #TODO

class Json_Macro(Json_Base,total=False):
... #TODO
38 changes: 21 additions & 17 deletions tagstudio/src/core/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import traceback
import xml.etree.ElementTree as ET
from enum import Enum

import ujson

from src.core.json_typing import Json_Collation, Json_Entry, Json_Libary, Json_Tag
from src.core import ts_core
from src.core.utils.str import strip_punctuation
from src.core.utils.web import strip_web_protocol
Expand Down Expand Up @@ -84,13 +84,14 @@ def __eq__(self, __value: object) -> bool:
and self.path == __value.path
and self.fields == __value.fields)

def compressed_dict(self) -> dict:
def compressed_dict(self) -> Json_Entry:
"""
An alternative to __dict__ that only includes fields containing
non-default data.
"""
obj = {}
obj['id'] = self.id
obj: Json_Entry = {
"id": self.id
}
if self.filename:
obj['filename'] = self.filename
if self.path:
Expand Down Expand Up @@ -198,13 +199,14 @@ def display_name(self, library: 'Library') -> str:
else:
return (f'{self.name}')

def compressed_dict(self) -> dict:
def compressed_dict(self) -> Json_Tag:
"""
An alternative to __dict__ that only includes fields containing
non-default data.
"""
obj = {}
obj["id"] = self.id
obj: Json_Tag = {
"id":self.id
}
if self.name:
obj["name"] = self.name
if self.shorthand:
Expand Down Expand Up @@ -262,13 +264,14 @@ def __eq__(self, __value: object) -> bool:
and self.path == __value.path
and self.fields == __value.fields)

def compressed_dict(self) -> dict:
def compressed_dict(self) -> Json_Collation:
"""
An alternative to __dict__ that only includes fields containing
non-default data.
"""
obj = {}
obj['id'] = self.id
obj: Json_Collation = {
"id":self.id
}
if self.title:
obj['title'] = self.title
if self.e_ids_and_pages:
Expand Down Expand Up @@ -349,7 +352,7 @@ def __init__(self) -> None:
# Map of every Tag ID to the index of the Tag in self.tags.
self._tag_id_to_index_map: dict[int, int] = {}

self.default_tags = [
self.default_tags: list[Json_Tag] = [
{
"id": 0,
"name": "Archived",
Expand Down Expand Up @@ -576,13 +579,13 @@ def verify_ts_folders(self) -> None:

if not os.path.isdir(full_collage_path):
os.mkdir(full_collage_path)
def verify_default_tags(self, tag_list: list) -> list:

def verify_default_tags(self, tag_list: list[Json_Tag]) -> list[Json_Tag]:
"""
Ensures that the default builtin tags are present in the Library's
save file. Takes in and returns the tag dictionary from the JSON file.
"""
missing: list[int] = []
missing: list[Json_Tag] = []

for dt in self.default_tags:
if dt['id'] not in [t['id'] for t in tag_list]:
Expand Down Expand Up @@ -610,7 +613,7 @@ def open_library(self, path: str) -> int:

try:
with open(os.path.normpath(f'{path}/{ts_core.TS_FOLDER_NAME}/ts_library.json'), 'r', encoding='utf-8') as f:
json_dump = ujson.load(f)
json_dump: Json_Libary = ujson.load(f)
self.library_dir = str(path)
self.verify_ts_folders()
major, minor, patch = json_dump['ts-version'].split('.')
Expand Down Expand Up @@ -828,8 +831,9 @@ def to_json(self):
Creates a JSON serialized string from the Library object.
Used in saving the library to disk.
"""
file_to_save = {"ts-version": ts_core.VERSION,
"ignored_extensions": [],

file_to_save: Json_Libary = {"ts-version": ts_core.VERSION,
"ignored_extensions": [],
"tags": [],
"collations": [],
"fields": [],
Expand Down