Skip to content

Commit

Permalink
added persistent extra settings (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
mortii committed Aug 24, 2024
1 parent a550e94 commit 34563f7
Show file tree
Hide file tree
Showing 14 changed files with 700 additions and 167 deletions.
44 changes: 21 additions & 23 deletions ankimorphs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
from aqt.utils import tooltip
from aqt.webview import AnkiWebView

from . import ankimorphs_config
from . import ankimorphs_globals as am_globals
from . import (
ankimorphs_config,
ankimorphs_globals,
browser_utils,
debugging_utils,
message_box_utils,
name_file_utils,
reviewing_utils,
Expand All @@ -47,6 +48,7 @@
)
from .ankimorphs_config import AnkiMorphsConfig, AnkiMorphsConfigFilter
from .ankimorphs_db import AnkiMorphsDB
from .extra_settings import ankimorphs_extra_settings, extra_settings_keys
from .generators.generators_window import GeneratorWindow
from .known_morphs_exporter import KnownMorphsExporterDialog
from .progression.progression_window import ProgressionWindow
Expand Down Expand Up @@ -141,7 +143,7 @@ def load_am_profile_configs() -> None:
assert mw is not None

profile_settings_path = Path(
mw.pm.profileFolder(), ankimorphs_globals.PROFILE_SETTINGS_FILE_NAME
mw.pm.profileFolder(), am_globals.PROFILE_SETTINGS_FILE_NAME
)
try:
with open(profile_settings_path, encoding="utf-8") as file:
Expand All @@ -165,20 +167,16 @@ def init_db() -> None:
def create_am_directories_and_files() -> None:
assert mw is not None

names_file_path: Path = Path(
mw.pm.profileFolder(), ankimorphs_globals.NAMES_TXT_FILE_NAME
)
names_file_path: Path = Path(mw.pm.profileFolder(), am_globals.NAMES_TXT_FILE_NAME)
known_morphs_dir_path: Path = Path(
mw.pm.profileFolder(), ankimorphs_globals.KNOWN_MORPHS_DIR_NAME
mw.pm.profileFolder(), am_globals.KNOWN_MORPHS_DIR_NAME
)
priority_files_dir_path: Path = Path(
mw.pm.profileFolder(), ankimorphs_globals.PRIORITY_FILES_DIR_NAME
mw.pm.profileFolder(), am_globals.PRIORITY_FILES_DIR_NAME
)

if not names_file_path.is_file():
with open(names_file_path, mode="w", encoding="utf-8"):
# just opening the file creates it
pass
# Create the file if it doesn't exist
names_file_path.touch(exist_ok=True)

if not known_morphs_dir_path.exists():
Path(known_morphs_dir_path).mkdir()
Expand All @@ -191,18 +189,18 @@ def register_addon_dialogs() -> None:
# We use the Anki dialog manager to handle our dialogs

aqt.dialogs.register_dialog(
name=ankimorphs_globals.SETTINGS_DIALOG_NAME, creator=SettingsDialog
name=am_globals.SETTINGS_DIALOG_NAME, creator=SettingsDialog
)
aqt.dialogs.register_dialog(
name=ankimorphs_globals.GENERATOR_DIALOG_NAME,
name=am_globals.GENERATOR_DIALOG_NAME,
creator=GeneratorWindow,
)
aqt.dialogs.register_dialog(
name=ankimorphs_globals.PROGRESSION_DIALOG_NAME,
name=am_globals.PROGRESSION_DIALOG_NAME,
creator=ProgressionWindow,
)
aqt.dialogs.register_dialog(
name=ankimorphs_globals.KNOWN_MORPHS_EXPORTER_DIALOG_NAME,
name=am_globals.KNOWN_MORPHS_EXPORTER_DIALOG_NAME,
creator=KnownMorphsExporterDialog,
)

Expand Down Expand Up @@ -242,7 +240,7 @@ def init_tool_menu_and_actions() -> None:
am_tool_menu.addAction(guide_action)
am_tool_menu.addAction(changelog_action)

if ankimorphs_globals.DEV_MODE:
if am_globals.DEV_MODE:
test_action = create_test_action()
am_tool_menu.addAction(test_action)

Expand Down Expand Up @@ -394,7 +392,7 @@ def rebuild_seen_morphs(_changes: OpChangesAfterUndo) -> None:
################################################################
AnkiMorphsDB.rebuild_seen_morphs_today()

if ankimorphs_globals.DEV_MODE:
if am_globals.DEV_MODE:
with AnkiMorphsDB() as am_db:
print("Seen_Morphs:")
am_db.print_table("Seen_Morphs")
Expand Down Expand Up @@ -444,7 +442,7 @@ def create_settings_action(am_config: AnkiMorphsConfig) -> QAction:
action = QAction("&Settings", mw)
action.setShortcut(am_config.shortcut_settings)
action.triggered.connect(
partial(aqt.dialogs.open, name=ankimorphs_globals.SETTINGS_DIALOG_NAME)
partial(aqt.dialogs.open, name=am_globals.SETTINGS_DIALOG_NAME)
)
return action

Expand Down Expand Up @@ -542,7 +540,7 @@ def browse_study_morphs_for_text_action(web_view: AnkiWebView, menu: QMenu) -> N
selected_text = web_view.selectedText()
if selected_text == "":
return
action = QAction(f"Browse in {ankimorphs_globals.EXTRA_FIELD_STUDY_MORPHS}", menu)
action = QAction(f"Browse in {am_globals.EXTRA_FIELD_STUDY_MORPHS}", menu)
action.triggered.connect(
lambda: browser_utils.browse_study_morphs_for_highlighted_morph(selected_text)
)
Expand All @@ -555,7 +553,7 @@ def create_generators_dialog_action(am_config: AnkiMorphsConfig) -> QAction:
action.triggered.connect(
partial(
aqt.dialogs.open,
name=ankimorphs_globals.GENERATOR_DIALOG_NAME,
name=am_globals.GENERATOR_DIALOG_NAME,
)
)
return action
Expand All @@ -567,7 +565,7 @@ def create_progression_dialog_action(am_config: AnkiMorphsConfig) -> QAction:
action.triggered.connect(
partial(
aqt.dialogs.open,
name=ankimorphs_globals.PROGRESSION_DIALOG_NAME,
name=am_globals.PROGRESSION_DIALOG_NAME,
)
)
return action
Expand All @@ -579,7 +577,7 @@ def create_known_morphs_exporter_action(am_config: AnkiMorphsConfig) -> QAction:
action.triggered.connect(
partial(
aqt.dialogs.open,
name=ankimorphs_globals.KNOWN_MORPHS_EXPORTER_DIALOG_NAME,
name=am_globals.KNOWN_MORPHS_EXPORTER_DIALOG_NAME,
)
)
return action
Expand Down
2 changes: 1 addition & 1 deletion ankimorphs/ankimorphs_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

# Semantic Versioning https://semver.org/
__version__ = "3.0.1"
__version__ = "3.1.0"

DEV_MODE: bool = False

Expand Down
119 changes: 119 additions & 0 deletions ankimorphs/extra_settings/ankimorphs_extra_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from pathlib import Path

from aqt import mw
from aqt.qt import QByteArray, QSettings # pylint:disable=no-name-in-module

from ..ui.generator_output_dialog_ui import Ui_GeneratorOutputDialog
from ..ui.generators_window_ui import Ui_GeneratorsWindow
from ..ui.known_morphs_exporter_dialog_ui import Ui_KnownMorphsExporterDialog
from ..ui.progression_window_ui import Ui_ProgressionWindow
from . import extra_settings_keys as keys # pylint:disable=no-name-in-module
from .extra_settings_keys import (
FileFormatsKeys,
GeneratorsOutputKeys,
GeneratorsWindowKeys,
KnownMorphsExporterKeys,
PreprocessKeys,
ProgressionWindowKeys,
)


class AnkiMorphsExtraSettings(QSettings):
def __init__(self) -> None:
assert mw is not None
extra_settings_path = Path(
mw.pm.profileFolder(), "ankimorphs_extra_settings.ini"
)
super().__init__(str(extra_settings_path), QSettings.Format.IniFormat)

def save_generators_window_settings(
self, ui: Ui_GeneratorsWindow, geometry: QByteArray
) -> None:
# fmt: off
self.beginGroup(keys.Dialogs.GENERATORS_WINDOW)
self.setValue(GeneratorsWindowKeys.WINDOW_GEOMETRY, geometry)
self.setValue(GeneratorsWindowKeys.MORPHEMIZER, ui.morphemizerComboBox.currentText())
self.setValue(GeneratorsWindowKeys.INPUT_DIR, ui.inputDirLineEdit.text())

self.beginGroup(GeneratorsWindowKeys.FILE_FORMATS)
self.setValue(FileFormatsKeys.TXT, ui.txtFilesCheckBox.isChecked())
self.setValue(FileFormatsKeys.SRT, ui.srtFilesCheckBox.isChecked())
self.setValue(FileFormatsKeys.VTT, ui.vttFilesCheckBox.isChecked())
self.setValue(FileFormatsKeys.MD, ui.mdFilesCheckBox.isChecked())
self.endGroup() # file format group

self.beginGroup(GeneratorsWindowKeys.PREPROCESS)
self.setValue(PreprocessKeys.IGNORE_SQUARE_BRACKETS, ui.squareBracketsCheckBox.isChecked())
self.setValue(PreprocessKeys.IGNORE_ROUND_BRACKETS, ui.roundBracketsCheckBox.isChecked())
self.setValue(PreprocessKeys.IGNORE_SLIM_ROUND_BRACKETS, ui.slimRoundBracketsCheckBox.isChecked())
self.setValue(PreprocessKeys.IGNORE_NAMES_MORPHEMIZER, ui.namesMorphemizerCheckBox.isChecked())
self.setValue(PreprocessKeys.IGNORE_NAMES_IN_FILE, ui.namesFileCheckBox.isChecked())
self.setValue(PreprocessKeys.IGNORE_NUMBERS, ui.numbersCheckBox.isChecked())
self.endGroup() # preprocess group
self.endGroup() # generators window group
# fmt: on

def save_known_morphs_exporter_settings(
self, ui: Ui_KnownMorphsExporterDialog, geometry: QByteArray
) -> None:
# fmt: off
self.beginGroup(keys.Dialogs.KNOWN_MORPHS_EXPORTER)
self.setValue(KnownMorphsExporterKeys.WINDOW_GEOMETRY, geometry)
self.setValue(KnownMorphsExporterKeys.OUTPUT_DIR, ui.outputLineEdit.text())
self.setValue(KnownMorphsExporterKeys.LEMMA, ui.storeOnlyMorphLemmaRadioButton.isChecked())
self.setValue(KnownMorphsExporterKeys.INFLECTION, ui.storeMorphLemmaAndInflectionRadioButton.isChecked())
self.setValue(KnownMorphsExporterKeys.INTERVAL, ui.knownIntervalSpinBox.value())
self.setValue(KnownMorphsExporterKeys.OCCURRENCES, ui.addOccurrencesColumnCheckBox.isChecked())
self.endGroup()
# fmt: on

def save_progression_window_settings(
self, ui: Ui_ProgressionWindow, geometry: QByteArray
) -> None:
# fmt: off
self.beginGroup(keys.Dialogs.PROGRESSION_WINDOW)
self.setValue(KnownMorphsExporterKeys.WINDOW_GEOMETRY, geometry)
self.setValue(ProgressionWindowKeys.PRIORITY_FILE, ui.morphPriorityCBox.currentText())
self.setValue(ProgressionWindowKeys.LEMMA_EVALUATION, ui.lemmaRadioButton.isChecked())
self.setValue(ProgressionWindowKeys.INFLECTION_EVALUATION, ui.inflectionRadioButton.isChecked())
self.setValue(ProgressionWindowKeys.PRIORITY_RANGE_START, ui.minPrioritySpinBox.value())
self.setValue(ProgressionWindowKeys.PRIORITY_RANGE_END, ui.maxPrioritySpinBox.value())
self.setValue(ProgressionWindowKeys.BIN_SIZE, ui.binSizeSpinBox.value())
self.setValue(ProgressionWindowKeys.BIN_TYPE_NORMAL, ui.normalRadioButton.isChecked())
self.setValue(ProgressionWindowKeys.BIN_TYPE_CUMULATIVE, ui.cumulativeRadioButton.isChecked())
self.endGroup()
# fmt: on

def save_generator_priority_file_settings(
self, ui: Ui_GeneratorOutputDialog, geometry: QByteArray
) -> None:
# fmt: off
self.beginGroup(keys.Dialogs.GENERATOR_OUTPUT_PRIORITY_FILE)
self.setValue(GeneratorsOutputKeys.WINDOW_GEOMETRY, geometry)
self.setValue(GeneratorsOutputKeys.OUTPUT_FILE_PATH, ui.outputLineEdit.text())
self.setValue(GeneratorsOutputKeys.LEMMA_FORMAT, ui.storeOnlyMorphLemmaRadioButton.isChecked())
self.setValue(GeneratorsOutputKeys.INFLECTION_FORMAT, ui.storeMorphLemmaAndInflectionRadioButton.isChecked())
self.setValue(GeneratorsOutputKeys.MIN_OCCURRENCE_SELECTED, ui.minOccurrenceRadioButton.isChecked())
self.setValue(GeneratorsOutputKeys.MIN_OCCURRENCE_CUTOFF, ui.minOccurrenceSpinBox.value())
self.setValue(GeneratorsOutputKeys.COMPREHENSION_SELECTED, ui.comprehensionRadioButton.isChecked())
self.setValue(GeneratorsOutputKeys.COMPREHENSION_CUTOFF, ui.comprehensionSpinBox.value())
self.setValue(GeneratorsOutputKeys.OCCURRENCES_COLUMN_SELECTED, ui.addOccurrencesColumnCheckBox.isChecked())
self.endGroup()
# fmt: on

def save_generator_study_plan_settings(
self, ui: Ui_GeneratorOutputDialog, geometry: QByteArray
) -> None:
# fmt: off
self.beginGroup(keys.Dialogs.GENERATOR_OUTPUT_STUDY_PLAN)
self.setValue(GeneratorsOutputKeys.WINDOW_GEOMETRY, geometry)
self.setValue(GeneratorsOutputKeys.OUTPUT_FILE_PATH, ui.outputLineEdit.text())
self.setValue(GeneratorsOutputKeys.LEMMA_FORMAT, ui.storeOnlyMorphLemmaRadioButton.isChecked())
self.setValue(GeneratorsOutputKeys.INFLECTION_FORMAT, ui.storeMorphLemmaAndInflectionRadioButton.isChecked())
self.setValue(GeneratorsOutputKeys.MIN_OCCURRENCE_SELECTED, ui.minOccurrenceRadioButton.isChecked())
self.setValue(GeneratorsOutputKeys.MIN_OCCURRENCE_CUTOFF, ui.minOccurrenceSpinBox.value())
self.setValue(GeneratorsOutputKeys.COMPREHENSION_SELECTED, ui.comprehensionRadioButton.isChecked())
self.setValue(GeneratorsOutputKeys.COMPREHENSION_CUTOFF, ui.comprehensionSpinBox.value())
self.setValue(GeneratorsOutputKeys.OCCURRENCES_COLUMN_SELECTED, ui.addOccurrencesColumnCheckBox.isChecked())
self.endGroup()
# fmt: on
63 changes: 63 additions & 0 deletions ankimorphs/extra_settings/extra_settings_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Dialogs:
GENERATORS_WINDOW = "generators_window"
KNOWN_MORPHS_EXPORTER = "known_morphs_exporter"
PROGRESSION_WINDOW = "progression_window"
GENERATOR_OUTPUT_PRIORITY_FILE = "generator_output_priority_file"
GENERATOR_OUTPUT_STUDY_PLAN = "generator_output_study_plan"


class GeneratorsWindowKeys:
WINDOW_GEOMETRY = "window_geometry"
MORPHEMIZER = "morphemizer"
FILE_FORMATS = "file_formats"
PREPROCESS = "preprocess"
INPUT_DIR = "input_dir"


class FileFormatsKeys:
TXT = "txt"
SRT = "srt"
VTT = "vtt"
MD = "md"


class PreprocessKeys:
IGNORE_SQUARE_BRACKETS = "ignore_square_brackets"
IGNORE_ROUND_BRACKETS = "ignore_round_brackets"
IGNORE_SLIM_ROUND_BRACKETS = "ignore_slim_round_brackets"
IGNORE_NAMES_MORPHEMIZER = "ignore_names_morphemizer"
IGNORE_NAMES_IN_FILE = "ignore_names_in_file"
IGNORE_NUMBERS = "ignore_numbers"


class KnownMorphsExporterKeys:
WINDOW_GEOMETRY = "window_geometry"
OUTPUT_DIR = "output_dir"
LEMMA = "lemma"
INFLECTION = "inflection"
INTERVAL = "interval"
OCCURRENCES = "occurrences"


class ProgressionWindowKeys:
WINDOW_GEOMETRY = "window_geometry"
PRIORITY_FILE = "priority_file"
LEMMA_EVALUATION = "lemma_evaluation"
INFLECTION_EVALUATION = "inflection_evaluation"
PRIORITY_RANGE_START = "priority_range_start"
PRIORITY_RANGE_END = "priority_range_end"
BIN_SIZE = "bin_size"
BIN_TYPE_NORMAL = "bin_type_normal"
BIN_TYPE_CUMULATIVE = "bin_type_cumulative"


class GeneratorsOutputKeys:
WINDOW_GEOMETRY = "window_geometry"
LEMMA_FORMAT = "lemma_format"
OUTPUT_FILE_PATH = "output_file_path"
INFLECTION_FORMAT = "inflection_format"
MIN_OCCURRENCE_SELECTED = "min_occurrence_selected"
MIN_OCCURRENCE_CUTOFF = "min_occurrence_cutoff"
COMPREHENSION_SELECTED = "comprehension_selected"
COMPREHENSION_CUTOFF = "comprehension_cutoff"
OCCURRENCES_COLUMN_SELECTED = "occurrences_column_selected"
Loading

0 comments on commit 34563f7

Please sign in to comment.