Skip to content
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

Headers Override for Name and CrowdAnkiUUID #47

Merged
merged 3 commits into from
Sep 2, 2023
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
20 changes: 17 additions & 3 deletions brain_brew/build_tasks/overrides/headers_override.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Optional, Union

from brain_brew.configuration.representation_base import RepresentationBase
Expand All @@ -16,26 +16,40 @@ def task_name(cls) -> str:
@classmethod
def yamale_schema(cls) -> str:
return f'''\
crowdanki_uuid: str(required=False)
deck_description_html_file: str(required=False)
name: str(required=False)
'''

@dataclass
class Representation(RepresentationBase):
deck_description_html_file: Optional[str]
crowdanki_uuid: Optional[str] = field(default=None)
deck_description_html_file: Optional[str] = field(default=None)
name: Optional[str] = field(default=None)

@classmethod
def from_repr(cls, data: Union[Representation, dict]):
rep: cls.Representation = data if isinstance(data, cls.Representation) else cls.Representation.from_dict(data)
return cls(
rep=rep,
deck_desc_html_file=HTMLFile.create_or_get(rep.deck_description_html_file)
crowdanki_uuid=rep.crowdanki_uuid,
deck_desc_html_file=HTMLFile.create_or_get(rep.deck_description_html_file),
name=rep.name
)

rep: Representation
crowdanki_uuid: Optional[str]
deck_desc_html_file: Optional[HTMLFile]
name: Optional[str]

def override(self, header: Headers):
if self.deck_desc_html_file:
header.description = self.deck_desc_html_file.get_data(deep_copy=True)

if self.crowdanki_uuid:
header.crowdanki_uuid = self.crowdanki_uuid

if self.name:
header.name = self.name

return header
1 change: 1 addition & 0 deletions brain_brew/representation/json/wrappers_for_crowd_anki.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
CA_TYPE = "__type__"
CA_NAME = "name"
CA_DESCRIPTION = "desc"
CA_UUID = "crowdanki_uuid"

NOTE_MODEL = "note_model_uuid"
FLAGS = "flags"
Expand Down
16 changes: 14 additions & 2 deletions brain_brew/representation/yaml/headers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass

from brain_brew.representation.json.wrappers_for_crowd_anki import CA_NAME, CA_DESCRIPTION
from brain_brew.representation.json.wrappers_for_crowd_anki import CA_NAME, CA_DESCRIPTION, CA_UUID
from brain_brew.representation.yaml.yaml_object import YamlObject


Expand All @@ -19,6 +19,10 @@ def encode(self) -> dict:
def name(self) -> str:
return self.data[CA_NAME]

@name.setter
def name(self, desc: str):
self.data[CA_NAME] = desc

@property
def description(self) -> str:
return self.data.get(CA_DESCRIPTION, "")
Expand All @@ -27,6 +31,14 @@ def description(self) -> str:
def description(self, desc: str):
self.data[CA_DESCRIPTION] = desc

@property
def crowdanki_uuid(self) -> str:
return self.data.get(CA_UUID, "")

@crowdanki_uuid.setter
def crowdanki_uuid(self, desc: str):
self.data[CA_UUID] = desc

@property
def data_without_name(self) -> dict:
return {k: v for k, v in self.data.items() if k != CA_NAME}
return {k: v for k, v in sorted(self.data.items()) if k != CA_NAME}
2 changes: 2 additions & 0 deletions brain_brew/schemas/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ headers_from_yaml_part:
override: include('headers_override', required=False)

headers_override:
crowdanki_uuid: str(required=False)
deck_description_html_file: str(required=False)
name: str(required=False)

media_group_from_crowd_anki:
part_id: str()
Expand Down