Skip to content

Commit

Permalink
Note model file separation (#15)
Browse files Browse the repository at this point in the history
* Note Model From HTML Files

* Recipe update

* Refactor Imports and Project Structure
  • Loading branch information
ohare93 authored Dec 3, 2020
1 parent 7f481fb commit da03b71
Show file tree
Hide file tree
Showing 56 changed files with 536 additions and 293 deletions.
6 changes: 3 additions & 3 deletions brain_brew/build_tasks/crowd_anki/crowd_anki_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from brain_brew.build_tasks.crowd_anki.media_to_crowd_anki import MediaGroupToCrowdAnki
from brain_brew.build_tasks.crowd_anki.note_models_to_crowd_anki import NoteModelsToCrowdAnki
from brain_brew.build_tasks.crowd_anki.notes_to_crowd_anki import NotesToCrowdAnki
from brain_brew.representation.build_config.build_task import TopLevelBuildTask
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.configuration.build_config.build_task import TopLevelBuildTask
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.generic.media_file import MediaFile
from brain_brew.representation.json.crowd_anki_export import CrowdAnkiExport
from brain_brew.representation.json.wrappers_for_crowd_anki import CrowdAnkiJsonWrapper
Expand Down Expand Up @@ -63,7 +63,7 @@ def execute(self):

note_models: List[dict] = self.note_model_transform.execute()

nm_name_to_id: dict = {model.name: model.id for model in self.note_model_transform.note_models}
nm_name_to_id: dict = {model.part_id: model.part.id for model in self.note_model_transform.note_models}
notes = self.notes_transform.execute(nm_name_to_id)

media_files: Set[MediaFile] = set()
Expand Down
8 changes: 4 additions & 4 deletions brain_brew/build_tasks/crowd_anki/headers_from_crowdanki.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from dataclasses import dataclass, field
from typing import Union, Optional

from brain_brew.representation.build_config.build_task import BuildPartTask
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.configuration.build_config.build_task import BuildPartTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.json.crowd_anki_export import CrowdAnkiExport
from brain_brew.representation.json.wrappers_for_crowd_anki import CA_NOTE_MODELS, CA_NOTES, CA_MEDIA_FILES, \
CA_CHILDREN, CA_TYPE
from brain_brew.representation.json.wrappers_for_crowd_anki import CrowdAnkiJsonWrapper
from brain_brew.representation.yaml.headers_repr import Headers
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.representation.yaml.headers import Headers

headers_skip_keys = [CA_NOTE_MODELS, CA_NOTES, CA_MEDIA_FILES]
headers_default_values = {
Expand Down
6 changes: 3 additions & 3 deletions brain_brew/build_tasks/crowd_anki/headers_to_crowd_anki.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from typing import Union

from brain_brew.build_tasks.crowd_anki.headers_from_crowdanki import headers_default_values
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.headers_repr import Headers
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.headers import Headers


@dataclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from typing import Union

from brain_brew.build_tasks.deck_parts.media_group_from_folder import MediaGroupFromFolder
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.representation.json.crowd_anki_export import CrowdAnkiExport
from brain_brew.representation.yaml.media_group_repr import MediaGroup
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.representation.yaml.media_group import MediaGroup
from brain_brew.transformers.media_group_from_location import create_media_group_from_location


Expand Down
6 changes: 3 additions & 3 deletions brain_brew/build_tasks/crowd_anki/media_to_crowd_anki.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from dataclasses import dataclass
from typing import Union, List, Set

from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.interfaces.yamale_verifyable import YamlRepr
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.representation.generic.media_file import MediaFile
from brain_brew.representation.yaml.media_group_repr import MediaGroup
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.representation.yaml.media_group import MediaGroup
from brain_brew.transformers.media_group_save_to_location import save_media_groups_to_location


Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from dataclasses import dataclass, field
from typing import Optional, Union

from brain_brew.representation.build_config.build_task import BuildPartTask

from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.configuration.build_config.build_task import BuildPartTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.json.crowd_anki_export import CrowdAnkiExport
from brain_brew.representation.json.wrappers_for_crowd_anki import CrowdAnkiJsonWrapper
from brain_brew.representation.yaml.note_model_repr import NoteModel
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.representation.yaml.note_model import NoteModel


@dataclass
Expand Down
16 changes: 8 additions & 8 deletions brain_brew/build_tasks/crowd_anki/note_models_to_crowd_anki.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from dataclasses import dataclass, field
from typing import Union, List

from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.interfaces.yamale_verifyable import YamlRepr
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.note_model_repr import NoteModel
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.representation.yaml.note_model import NoteModel


@dataclass
Expand Down Expand Up @@ -55,11 +55,11 @@ def from_repr(cls, data: Union[Representation, dict, str]):
part_to_read=rep.part_id
)

def get_note_model(self) -> NoteModel:
self.part = PartHolder.from_file_manager(self.part_to_read).part
def get_note_model(self) -> PartHolder[NoteModel]:
self.part = PartHolder.from_file_manager(self.part_to_read)
return self.part # Todo: add filters in here

part: NoteModel = field(init=False)
part: PartHolder[NoteModel] = field(init=False)
part_to_read: str

@dataclass
Expand All @@ -81,7 +81,7 @@ def from_repr(cls, data: Union[Representation, dict, List[str]]):
note_models=[nm.get_note_model() for nm in note_model_items]
)

note_models: List[NoteModel]
note_models: List[PartHolder[NoteModel]]

def execute(self) -> List[dict]:
return [model.encode_as_crowdanki() for model in self.note_models]
return [model.part.encode_as_crowdanki() for model in self.note_models]
8 changes: 4 additions & 4 deletions brain_brew/build_tasks/crowd_anki/notes_from_crowd_anki.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from typing import Union, Optional, List

from brain_brew.build_tasks.crowd_anki.shared_base_notes import SharedBaseNotes
from brain_brew.representation.build_config.build_task import BuildPartTask
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.configuration.build_config.build_task import BuildPartTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.json.crowd_anki_export import CrowdAnkiExport
from brain_brew.representation.json.wrappers_for_crowd_anki import CrowdAnkiJsonWrapper, CrowdAnkiNoteWrapper
from brain_brew.representation.yaml.note_repr import Notes, Note
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.representation.yaml.notes import Notes, Note


@dataclass
Expand Down
6 changes: 3 additions & 3 deletions brain_brew/build_tasks/crowd_anki/notes_to_crowd_anki.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from typing import Optional, Union, List

from brain_brew.build_tasks.crowd_anki.shared_base_notes import SharedBaseNotes
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.interfaces.yamale_verifyable import YamlRepr
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.representation.json.wrappers_for_crowd_anki import CrowdAnkiNoteWrapper
from brain_brew.representation.yaml.note_repr import Notes, Note
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.representation.yaml.notes import Notes, Note
from brain_brew.utils import blank_str_if_none


Expand Down
10 changes: 5 additions & 5 deletions brain_brew/build_tasks/csvs/csvs_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from typing import List, Dict, Union

from brain_brew.build_tasks.csvs.shared_base_csvs import SharedBaseCsvs
from brain_brew.representation.build_config.build_task import TopLevelBuildTask
from brain_brew.representation.configuration.csv_file_mapping import FileMapping
from brain_brew.representation.configuration.note_model_mapping import NoteModelMapping
from brain_brew.representation.yaml.note_repr import Notes, Note
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.configuration.build_config.build_task import TopLevelBuildTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.representation.yaml.notes import Notes, Note
from brain_brew.transformers.file_mapping import FileMapping
from brain_brew.transformers.note_model_mapping import NoteModelMapping
from brain_brew.utils import join_tags


Expand Down
10 changes: 5 additions & 5 deletions brain_brew/build_tasks/csvs/notes_from_csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from typing import Dict, List, Union, Optional

from brain_brew.build_tasks.csvs.shared_base_csvs import SharedBaseCsvs
from brain_brew.representation.build_config.build_task import BuildPartTask
from brain_brew.representation.configuration.csv_file_mapping import FileMapping
from brain_brew.representation.configuration.note_model_mapping import NoteModelMapping
from brain_brew.representation.yaml.note_repr import Note, Notes
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.configuration.build_config.build_task import BuildPartTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.representation.yaml.notes import Note, Notes
from brain_brew.transformers.file_mapping import FileMapping
from brain_brew.transformers.note_model_mapping import NoteModelMapping
from brain_brew.utils import split_tags


Expand Down
6 changes: 3 additions & 3 deletions brain_brew/build_tasks/csvs/shared_base_csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from dataclasses import dataclass, field
from typing import List, Dict

from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.representation.configuration.csv_file_mapping import FileMapping
from brain_brew.representation.configuration.note_model_mapping import NoteModelMapping
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.transformers.file_mapping import FileMapping
from brain_brew.transformers.note_model_mapping import NoteModelMapping


@dataclass
Expand Down
14 changes: 7 additions & 7 deletions brain_brew/build_tasks/deck_parts/from_yaml_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from dataclasses import dataclass
from typing import Union

from brain_brew.representation.build_config.build_task import BuildPartTask
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.headers_repr import Headers
from brain_brew.representation.yaml.media_group_repr import MediaGroup
from brain_brew.representation.yaml.note_model_repr import NoteModel
from brain_brew.representation.yaml.note_repr import Notes
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.configuration.build_config.build_task import BuildPartTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.headers import Headers
from brain_brew.representation.yaml.media_group import MediaGroup
from brain_brew.representation.yaml.note_model import NoteModel
from brain_brew.representation.yaml.notes import Notes
from brain_brew.representation.yaml.yaml_object import YamlObject


Expand Down
8 changes: 4 additions & 4 deletions brain_brew/build_tasks/deck_parts/media_group_from_folder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from dataclasses import dataclass, field
from typing import Optional, Union, List

from brain_brew.representation.build_config.build_task import BuildPartTask
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.media_group_repr import MediaGroup
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.configuration.build_config.build_task import BuildPartTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.media_group import MediaGroup
from brain_brew.transformers.media_group_from_location import create_media_group_from_location


Expand Down
8 changes: 4 additions & 4 deletions brain_brew/build_tasks/deck_parts/media_group_to_folder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from dataclasses import dataclass
from typing import List, Union, Optional

from brain_brew.representation.build_config.build_task import BuildPartTask
from brain_brew.representation.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.media_group_repr import MediaGroup
from brain_brew.representation.yaml.part_holder import PartHolder
from brain_brew.configuration.build_config.build_task import BuildPartTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.yaml.media_group import MediaGroup
from brain_brew.transformers.media_group_save_to_location import save_media_groups_to_location


Expand Down
76 changes: 76 additions & 0 deletions brain_brew/build_tasks/deck_parts/note_model_from_html_parts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from dataclasses import dataclass, field
from typing import Optional, Union, List

from brain_brew.configuration.build_config.build_task import BuildPartTask
from brain_brew.configuration.part_holder import PartHolder
from brain_brew.configuration.representation_base import RepresentationBase
from brain_brew.representation.generic.html_file import HTMLFile
from brain_brew.representation.yaml.note_model import NoteModel
from brain_brew.representation.yaml.note_model_field import Field
from brain_brew.representation.yaml.note_model_template import Template


@dataclass
class NoteModelFromHTMLParts(BuildPartTask):
@classmethod
def task_name(cls) -> str:
return r'note_model_from_html_parts'

@classmethod
def yamale_schema(cls) -> str:
return f'''\
part_id: str()
model_id: str()
css_file: str()
fields: list(include('{Field.task_name()}'))
templates: list(str())
model_name: str(required=False)
save_to_file: str(required=False)
'''

@classmethod
def yamale_dependencies(cls) -> set:
return {Field}

@dataclass
class Representation(RepresentationBase):
part_id: str
model_id: str
css_file: str
fields: List[dict]
templates: List[dict]
model_name: Optional[str] = field(default=None)
save_to_file: 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(
part_id=rep.part_id,
model_id=rep.model_id,
css=HTMLFile.create_or_get(rep.css_file).get_data(deep_copy=True),
fields=list(map(Field.from_dict, rep.fields)),
templates=list(holder.part for holder in map(PartHolder.from_file_manager, rep.templates)),
model_name=rep.model_name or rep.part_id,
save_to_file=rep.save_to_file
)

part_id: str
model_id: str
css: str
fields: List[Field]
templates: List[Template]
model_name: str
save_to_file: Optional[str]

def execute(self):
part = NoteModel(
name=self.model_name,
id=self.model_id,
css=self.css,
fields=self.fields,
templates=self.templates,
required_fields_per_template=[]
)

PartHolder.override_or_create(self.part_id, self.save_to_file, part)
Loading

0 comments on commit da03b71

Please sign in to comment.