Skip to content

Commit

Permalink
stay compatible with python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
marph91 committed Aug 28, 2024
1 parent dc6a8f2 commit 99ae3ae
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 119 deletions.
170 changes: 83 additions & 87 deletions joppy/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,35 +206,34 @@ def serialize(self) -> str:
if self.body is not None:
lines.extend([self.body, ""])
for field_ in fields(self):
match field_.name:
case "id":
# ID is always required
if self.id is None:
self.id = uuid.uuid4().hex
lines.append(f"{field_.name}: {self.id}")
case "markup_language":
# required to get an editable note
if self.markup_language is None:
self.markup_language = MarkupLanguage.MARKDOWN
lines.append(f"{field_.name}: {self.markup_language}")
case "source_application":
if self.source_application is None:
self.source_application = "joppy"
lines.append(f"{field_.name}: {self.source_application}")
case "title" | "body":
pass # handled before
case "type_":
self.item_type = ItemType.NOTE
lines.append(f"{field_.name}: {self.item_type}")
case "updated_time":
# required, even if empty
value_raw = getattr(self, field_.name)
value = "" if value_raw is None else value_raw
lines.append(f"{field_.name}: {value}")
case _:
value_raw = getattr(self, field_.name)
if value_raw is not None:
lines.append(f"{field_.name}: {value_raw}")
if field_.name == "id":
# ID is always required
if self.id is None:
self.id = uuid.uuid4().hex
lines.append(f"{field_.name}: {self.id}")
elif field_.name == "markup_language":
# required to get an editable note
if self.markup_language is None:
self.markup_language = MarkupLanguage.MARKDOWN
lines.append(f"{field_.name}: {self.markup_language}")
elif field_.name == "source_application":
if self.source_application is None:
self.source_application = "joppy"
lines.append(f"{field_.name}: {self.source_application}")
elif field_.name in ("title", "body"):
pass # handled before
elif field_.name == "type_":
self.item_type = ItemType.NOTE
lines.append(f"{field_.name}: {self.item_type}")
elif field_.name == "updated_time":
# required, even if empty
value_raw = getattr(self, field_.name)
value = "" if value_raw is None else value_raw
lines.append(f"{field_.name}: {value}")
else:
value_raw = getattr(self, field_.name)
if value_raw is not None:
lines.append(f"{field_.name}: {value_raw}")
return "\n".join(lines)


Expand Down Expand Up @@ -263,26 +262,25 @@ def serialize(self) -> str:
if self.title is not None:
lines.extend([self.title, ""])
for field_ in fields(self):
match field_.name:
case "id":
# ID is always required
if self.id is None:
self.id = uuid.uuid4().hex
lines.append(f"{field_.name}: {self.id}")
case "title":
pass # handled before
case "type_":
self.item_type = ItemType.FOLDER
lines.append(f"{field_.name}: {self.item_type}")
case "updated_time":
# required, even if empty
value_raw = getattr(self, field_.name)
value = "" if value_raw is None else value_raw
lines.append(f"{field_.name}: {value}")
case _:
value_raw = getattr(self, field_.name)
if value_raw is not None:
lines.append(f"{field_.name}: {value_raw}")
if field_.name == "id":
# ID is always required
if self.id is None:
self.id = uuid.uuid4().hex
lines.append(f"{field_.name}: {self.id}")
elif field_.name == "title":
pass # handled before
elif field_.name == "type_":
self.item_type = ItemType.FOLDER
lines.append(f"{field_.name}: {self.item_type}")
elif field_.name == "updated_time":
# required, even if empty
value_raw = getattr(self, field_.name)
value = "" if value_raw is None else value_raw
lines.append(f"{field_.name}: {value}")
else:
value_raw = getattr(self, field_.name)
if value_raw is not None:
lines.append(f"{field_.name}: {value_raw}")
return "\n".join(lines)


Expand Down Expand Up @@ -361,26 +359,25 @@ def serialize(self) -> str:
if self.title is not None:
lines.extend([self.title, ""])
for field_ in fields(self):
match field_.name:
case "id":
# ID is always required
if self.id is None:
self.id = uuid.uuid4().hex
lines.append(f"{field_.name}: {self.id}")
case "title":
pass # handled before
case "type_":
self.item_type = ItemType.TAG
lines.append(f"{field_.name}: {self.item_type}")
case "updated_time":
# required, even if empty
value_raw = getattr(self, field_.name)
value = "" if value_raw is None else value_raw
lines.append(f"{field_.name}: {value}")
case _:
value_raw = getattr(self, field_.name)
if value_raw is not None:
lines.append(f"{field_.name}: {value_raw}")
if field_.name == "id":
# ID is always required
if self.id is None:
self.id = uuid.uuid4().hex
lines.append(f"{field_.name}: {self.id}")
elif field_.name == "title":
pass # handled before
elif field_.name == "type_":
self.item_type = ItemType.TAG
lines.append(f"{field_.name}: {self.item_type}")
elif field_.name == "updated_time":
# required, even if empty
value_raw = getattr(self, field_.name)
value = "" if value_raw is None else value_raw
lines.append(f"{field_.name}: {value}")
else:
value_raw = getattr(self, field_.name)
if value_raw is not None:
lines.append(f"{field_.name}: {value_raw}")
return "\n".join(lines)


Expand All @@ -402,24 +399,23 @@ class NoteTagData(BaseData):
def serialize(self) -> str:
lines = []
for field_ in fields(self):
match field_.name:
case "id":
# ID is always required
if self.id is None:
self.id = uuid.uuid4().hex
lines.append(f"{field_.name}: {self.id}")
case "type_":
self.item_type = ItemType.NOTE_TAG
lines.append(f"{field_.name}: {self.item_type}")
case "updated_time":
# required, even if empty
value_raw = getattr(self, field_.name)
value = "" if value_raw is None else value_raw
lines.append(f"{field_.name}: {value}")
case _:
value_raw = getattr(self, field_.name)
if value_raw is not None:
lines.append(f"{field_.name}: {value_raw}")
if field_.name == "id":
# ID is always required
if self.id is None:
self.id = uuid.uuid4().hex
lines.append(f"{field_.name}: {self.id}")
elif field_.name == "type_":
self.item_type = ItemType.NOTE_TAG
lines.append(f"{field_.name}: {self.item_type}")
elif field_.name == "updated_time":
# required, even if empty
value_raw = getattr(self, field_.name)
value = "" if value_raw is None else value_raw
lines.append(f"{field_.name}: {value}")
else:
value_raw = getattr(self, field_.name)
if value_raw is not None:
lines.append(f"{field_.name}: {value_raw}")
return "\n".join(lines)


Expand Down
64 changes: 32 additions & 32 deletions joppy/server_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,43 @@ def extract_metadata(serialized_metadata: str) -> Dict[Any, Any]:
metadata[key] = value
return metadata

match len(body_splitted):
case 1:
# metadata only
title = None
note_body = None
metadata = extract_metadata(body_splitted[0])
case 2:
# title + metadata
title = body_splitted[0]
note_body = None
metadata = extract_metadata(body_splitted[1])
case 3:
# title + body + metadata
title = body_splitted[0]
note_body = body_splitted[1]
metadata = extract_metadata(body_splitted[2])
case _:
print("TODO: ", body_splitted)
if len(body_splitted) == 1:
# metadata only
title = None
note_body = None
metadata = extract_metadata(body_splitted[0])
elif len(body_splitted) == 2:
# title + metadata
title = body_splitted[0]
note_body = None
metadata = extract_metadata(body_splitted[1])
elif len(body_splitted) == 3:
# title + body + metadata
title = body_splitted[0]
note_body = body_splitted[1]
metadata = extract_metadata(body_splitted[2])
else:
print("TODO: ", body_splitted)

if title is not None:
metadata["title"] = title
if note_body is not None:
metadata["body"] = note_body

match dt.ItemType(int(metadata["type_"])):
case dt.ItemType.NOTE:
return dt.NoteData(**metadata)
case dt.ItemType.FOLDER:
return dt.NotebookData(**metadata)
case dt.ItemType.TAG:
return dt.TagData(**metadata)
case dt.ItemType.NOTE_TAG:
return dt.NoteTagData(**metadata)
case dt.ItemType.REVISION:
# ignore revisions for now
pass
case _:
print("TODO: ", dt.ItemType(int(metadata["type_"])))
item_type = dt.ItemType(int(metadata["type_"]))
if item_type == dt.ItemType.NOTE:
return dt.NoteData(**metadata)
elif item_type == dt.ItemType.FOLDER:
return dt.NotebookData(**metadata)
elif item_type == dt.ItemType.TAG:
return dt.TagData(**metadata)
elif item_type == dt.ItemType.NOTE_TAG:
return dt.NoteTagData(**metadata)
elif item_type == dt.ItemType.REVISION:
# ignore revisions for now
pass
else:
print("TODO: ", dt.ItemType(int(metadata["type_"])))
return None


Expand Down

0 comments on commit 99ae3ae

Please sign in to comment.