Skip to content

Commit

Permalink
Reworked TUI and GUI using textual and Gtk
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaublitz committed Sep 5, 2024
1 parent 069e789 commit 2113f73
Show file tree
Hide file tree
Showing 17 changed files with 1,403 additions and 472 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ jobs:
- name: Check code formatting
run: black --check .

mypy:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y libcairo-dev libgtk-4-dev cmake gobject-introspection libgirepository1.0-dev libadwaita-1-dev
python -m pip install --upgrade pip
pip install .
pip install mypy types-beautifulsoup4 types-requests
- name: Check code types
run: mypy -p language_practice

pylint:
runs-on: ubuntu-latest
strategy:
Expand All @@ -34,6 +55,8 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y libcairo-dev libgtk-4-dev cmake gobject-introspection libgirepository1.0-dev libadwaita-1-dev
python -m pip install --upgrade pip
pip install .
pip install pylint
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*egg-info
build
dist
*.db
36 changes: 24 additions & 12 deletions language-practice
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ Inflection charts are pulled from wiktionary.
"""

import argparse
import asyncio
import sys

from language_practice.terminal import Application
from language_practice.gui import GuiApplication
from language_practice.sqlite import SqliteHandle
from language_practice.terminal import TerminalApplication


class Once(argparse.Action):
Expand All @@ -37,27 +38,38 @@ def main():
prog="language-practice", description="Flashcard app"
)
parse.add_argument("-t", "--traceback", action="store_true")
parse.add_argument("-r", "--reset", action="store_true")
parse.add_argument("-f", "--file", action=Once, required=True)
parse.add_argument("-d", "--dir", action="store_true")
parse.add_argument("-g", "--gui", action="store_true")
parse.add_argument("-d", "--db", action="store", required=True)
args = parse.parse_args()

try:
app = Application(
args.file,
args.dir,
args.reset,
)
asyncio.run(app.startup())
app.run()
handle = SqliteHandle(args.db)
except Exception as err: # pylint: disable=broad-exception-caught
if args.traceback:
raise err
print(f"{err}")
sys.exit(1)

try:
all_sets = handle.get_all_sets()
if args.gui:
gui = GuiApplication(handle, all_sets)
gui.run()
else:
tui = TerminalApplication(handle, all_sets)
tui.run()
except Exception as err: # pylint: disable=broad-exception-caught
if args.traceback:
raise err

print(f"{err}")
handle.close()
sys.exit(1)
except KeyboardInterrupt:
print("Exiting...")
handle.close()
else:
handle.close()


if __name__ == "__main__":
Expand Down
38 changes: 0 additions & 38 deletions language_practice/cache.py

This file was deleted.

184 changes: 184 additions & 0 deletions language_practice/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
"""
Handles TOML parsing from the configuration file.
"""

from datetime import date
from tomllib import load
from typing import Any, Self

from language_practice.repetition import WordRepetition


# pylint: disable=too-many-instance-attributes
class Entry:
"""
A single entry in the TOML file.
"""

# pylint: disable=too-many-arguments
def __init__(
self,
word: str,
definition: str,
gender: str | None,
aspect: str | None,
usage: str | None,
part_of_speech: str | None,
charts: list[list[list[str]]] | None,
repetition: WordRepetition,
):
self.word = word
self.definition = definition
self.gender = gender
self.aspect = aspect
self.usage = usage
self.part_of_speech = part_of_speech
self.charts = charts
self.repetition = repetition

def get_word(self) -> str:
"""
Get word.
"""
return self.word

def get_definition(self) -> str:
"""
Get definition.
"""
return self.definition

def get_gender(self) -> str | None:
"""
Get gender.
"""
return self.gender

def get_aspect(self) -> str | None:
"""
Get aspect.
"""
return self.aspect

def get_usage(self) -> str | None:
"""
Get usage.
"""
return self.usage

def get_part_of_speech(self) -> str | None:
"""
Get part of speech.
"""
return self.part_of_speech

def get_charts(self) -> list[list[list[str]]] | None:
"""
Get charts.
"""
return self.charts

def get_repetition(self) -> WordRepetition:
"""
Get repetition data structure.
"""
return self.repetition


class Config:
"""
Generic config data structure.
"""

def __init__(self, lang: str | None, entries: list[Entry]):
self.lang = lang
self.words = entries

def __iter__(self):
return iter(self.words)

def __len__(self) -> int:
return len(self.words)

def get_lang(self) -> str | None:
"""
Get the language associated with this word file, if any.
"""
return self.lang

def get_words(self) -> list[Entry]:
"""
Get a list of all words in the TOML file.
"""
return self.words

def extend(self, config: Self):
"""
Extend a TOML config with another TOML config.
"""
if self.lang != config.lang:
raise RuntimeError(
f"Attempted to join a TOML config with lang {self.lang} with \
one with lang {config.lang}"
)

self.words += config.words
return self


class GraphicalConfig(Config):
"""
All entries in the graphical config.
"""

def __init__(self, lang: str | None, dcts: list[dict[str, Any]]):
try:
words = [
Entry(
dct["word"],
dct["definition"],
dct.get("gender", None),
dct.get("aspect", None),
dct.get("usage", None),
dct.get("part_of_speech", None),
dct.get("charts", None),
WordRepetition(2.5, 0, 0, date.today(), False),
)
for dct in dcts
]
super().__init__(lang, words)
except KeyError as err:
raise RuntimeError(f"Key {err} not found") from err


class TomlConfig(Config):
"""
All entries in the TOML file.
"""

def __init__(self, file_path: str):
try:
with open(file_path, "rb") as file_handle:
toml = load(file_handle)
lang = toml.get("lang", None)
if lang is not None and lang not in ["fr", "uk", "ru"]:
raise RuntimeError(
f"Language {lang} is not supported; if you would like it to "
"be, please open a feature request!"
)
words = [
Entry(
dct["word"],
dct["definition"],
dct.get("gender", None),
dct.get("aspect", None),
dct.get("usage", None),
dct.get("part_of_speech", None),
dct.get("charts", None),
WordRepetition(2.5, 0, 0, date.today(), False),
)
for dct in toml["words"]
]
super().__init__(lang, words)
except KeyError as err:
raise RuntimeError(f"Key {err} not found") from err
Loading

0 comments on commit 2113f73

Please sign in to comment.