Skip to content

Commit

Permalink
General Code sanitation and several fixes (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
mufeedali committed Aug 8, 2024
2 parents 337db7f + 450ac13 commit 939bef4
Show file tree
Hide file tree
Showing 44 changed files with 815 additions and 632 deletions.
38 changes: 38 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

## Code Style

We use [Black](https://black.readthedocs.io/en/stable/) (PEP 8 compliant) for code formatting.

Install it to format your code after writing code or use it in your IDE as code formatter.

To format run:

```sh
black dialect
```

We also use [isort](https://pycqa.github.io/isort/) for imports sorting.

```sh
isort dialect
```

### Type Annotations

We try to use Python type annotations whenever is possible.

#### When to omit annotations?

- Explicit classes instantiation like:

```python
var = Class()
```

- Non-nullable argument with default value:

```python
# We omit `int` typing for `number`
def method(text: str, number = 5, other: int | None = 20):
...
```
1 change: 1 addition & 0 deletions data/app.drey.Dialect.metainfo.xml.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<url type="homepage">https://dialectapp.org/</url>
<url type="donation">https://opencollective.com/dialect/</url>
<url type="bugtracker">https://github.com/dialect-app/dialect/issues/</url>
<url type="help">https://github.com/dialect-app/dialect/discussions/</url>
<url type="translate">https://hosted.weblate.org/engage/dialect/</url>
<url type="vcs-browser">https://github.com/dialect-app/dialect/</url>
<!-- developer_name tag deprecated with Appstream 1.0 -->
Expand Down
2 changes: 0 additions & 2 deletions data/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
subdir('resources')

# Install desktop
desktop_conf = configuration_data()
desktop_conf.set('icon', application_id)
Expand Down
17 changes: 0 additions & 17 deletions data/resources/about.blp

This file was deleted.

20 changes: 0 additions & 20 deletions data/resources/dialect.gresource.xml

This file was deleted.

24 changes: 0 additions & 24 deletions data/resources/meson.build

This file was deleted.

4 changes: 2 additions & 2 deletions dialect/define.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2020 gi-lom
# Copyright 2020-2022 Mufeed Ali
# Copyright 2020-2022 Rafael Mardojai CM
# Copyright 2020 Mufeed Ali
# Copyright 2020 Rafael Mardojai CM
# SPDX-License-Identifier: GPL-3.0-or-later


Expand Down
10 changes: 10 additions & 0 deletions dialect/define.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
APP_ID: str
PROFILE: str
RES_PATH: str
VERSION: str

TRANS_NUMBER: int

LANG_ALIASES: dict[str, str]

LANGUAGES: dict[str, str]
21 changes: 21 additions & 0 deletions dialect/dialect.gresource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/app/drey/Dialect">
<file compressed="true">style.css</file>
<file compressed="true">style-dark.css</file>

<file compressed="true" preprocess="xml-stripblanks">preferences.ui</file>
<file compressed="true" preprocess="xml-stripblanks">shortcuts.ui</file>
<file compressed="true" preprocess="xml-stripblanks">window.ui</file>
<file compressed="true" preprocess="xml-stripblanks">widgets/lang_row.ui</file>
<file compressed="true" preprocess="xml-stripblanks">widgets/lang_selector.ui</file>
<file compressed="true" preprocess="xml-stripblanks">widgets/provider_preferences.ui</file>
<file compressed="true" preprocess="xml-stripblanks">widgets/theme_switcher.ui</file>

<file compressed="true" preprocess="xml-stripblanks" alias="appdata.xml">@appstream-path@</file>
</gresource>

<gresource prefix="/app/drey/Dialect/icons/scalable/emblems/">
<file preprocess="xml-stripblanks" alias="dialect-settings-symbolic.svg">icons/settings-symbolic.svg</file>
</gresource>
</gresources>
File renamed without changes
32 changes: 17 additions & 15 deletions dialect/languages.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Copyright 2021-2022 Mufeed Ali
# Copyright 2021-2022 Rafael Mardojai CM
# Copyright 2021 Mufeed Ali
# Copyright 2021 Rafael Mardojai CM
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Callable

from gi.repository import Gio, GObject

from dialect.define import LANGUAGES


def get_lang_name(code):
def get_lang_name(code: str) -> str | None:
name = LANGUAGES.get(code)
if name:
name = gettext(name)
Expand All @@ -17,54 +19,54 @@ def get_lang_name(code):
class LangObject(GObject.Object):
__gtype_name__ = "LangObject"

code = GObject.Property(type=str)
name = GObject.Property(type=str)
selected = GObject.Property(type=bool, default=False)
code: str = GObject.Property(type=str) # type: ignore
name: str = GObject.Property(type=str) # type: ignore
selected: bool = GObject.Property(type=bool, default=False) # type: ignore

def __init__(self, code, name, selected=False):
def __init__(self, code: str, name: str, selected=False):
super().__init__()

self.code = code
self.name = name
self.selected = selected

def __str__(self):
def __str__(self) -> str:
return self.code


class LanguagesListModel(GObject.GObject, Gio.ListModel):
__gtype_name__ = "LanguagesListModel"

def __init__(self, names_func=get_lang_name):
def __init__(self, names_func: Callable[[str], str | None] = get_lang_name):
super().__init__()

self.names_func = names_func
self.langs = []
self.langs: list[LangObject] = []

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

def do_get_item(self, position):
def do_get_item(self, position: int) -> LangObject:
return self.langs[position]

def do_get_item_type(self):
return LangObject

def do_get_n_items(self):
def do_get_n_items(self) -> int:
return len(self.langs)

def set_langs(self, langs, auto=False):
def set_langs(self, langs: list[str], auto=False):
removed = len(self.langs)
self.langs.clear()

if auto:
self.langs.append(LangObject("auto", _("Auto")))

for code in langs:
self.langs.append(LangObject(code, self.names_func(code)))
self.langs.append(LangObject(code, self.names_func(code) or code))

self.items_changed(0, removed, len(self.langs))

def set_selected(self, code):
def set_selected(self, code: str):
for item in self.langs:
item.props.selected = item.code == code
65 changes: 34 additions & 31 deletions dialect/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2020 gi-lom
# Copyright 2020-2022 Mufeed Ali
# Copyright 2020-2022 Rafael Mardojai CM
# Copyright 2020 Mufeed Ali
# Copyright 2020 Rafael Mardojai CM
# SPDX-License-Identifier: GPL-3.0-or-later

# Initial setup
Expand All @@ -17,7 +17,7 @@
gi.require_version("Secret", "1")
gi.require_version("Soup", "3.0")

from gi.repository import Adw, Gio, GLib, Gst, Gtk
from gi.repository import Adw, Gio, GLib, Gst
except ImportError or ValueError:
logging.error("Error: GObject dependencies not met.")

Expand All @@ -33,36 +33,38 @@ def __init__(self):
self.set_resource_base_path(RES_PATH)

# App window
self.window = None
self.window: DialectWindow | None = None
# CLI
self.argv = {}
self._signal_handler = None
self.argv: dict[str, str] = {}
self._signal_handler: int | None = None

# Add command line options
self.add_main_option(
"selection",
b"n",
ord("n"),
GLib.OptionFlags.NONE,
GLib.OptionArg.NONE,
"Translate text from the primary clipboard",
None,
)
self.add_main_option("text", b"t", GLib.OptionFlags.NONE, GLib.OptionArg.STRING, "Text to translate", None)
self.add_main_option("src", b"s", GLib.OptionFlags.NONE, GLib.OptionArg.STRING, "Source lang code", None)
self.add_main_option("dest", b"d", GLib.OptionFlags.NONE, GLib.OptionArg.STRING, "Destination lang code", None)
self.add_main_option("text", ord("t"), GLib.OptionFlags.NONE, GLib.OptionArg.STRING, "Text to translate", None)
self.add_main_option("src", ord("s"), GLib.OptionFlags.NONE, GLib.OptionArg.STRING, "Source lang code", None)
self.add_main_option(
"dest", ord("d"), GLib.OptionFlags.NONE, GLib.OptionArg.STRING, "Destination lang code", None
)

self.setup_actions()

def do_activate(self):
def on_translator_loading(_win, _pspec):
if not self.window.translator_loading:
if self.window and not self.window.translator_loading:
# Remove signal handler
if self._signal_handler:
self.window.disconnect(self._signal_handler)
# Process CLI args
self.process_command_line()

self.window = self.props.active_window
self.window = self.props.active_window # type: ignore

if not self.window:
width, height = Settings.get().window_size
Expand All @@ -83,7 +85,7 @@ def on_translator_loading(_win, _pspec):

self.window.present()

def do_command_line(self, command_line):
def do_command_line(self, command_line: Gio.ApplicationCommandLine):
options = command_line.get_options_dict()

# Save CLI args values
Expand All @@ -103,7 +105,7 @@ def process_command_line(self):
return

text = ""
langs = {"src": None, "dest": None}
langs: dict[str, str | None] = {"src": None, "dest": None}
selection = "selection" in self.argv

if "text" in self.argv:
Expand All @@ -129,15 +131,15 @@ def setup_actions(self):
pronunciation.connect("change-state", self._on_pronunciation)
self.add_action(pronunciation)

preferences = Gio.SimpleAction.new("preferences", None)
preferences = Gio.SimpleAction(name="preferences")
preferences.connect("activate", self._on_preferences)
self.add_action(preferences)

about = Gio.SimpleAction.new("about", None)
about = Gio.SimpleAction(name="about")
about.connect("activate", self._on_about)
self.add_action(about)

quit_action = Gio.SimpleAction.new("quit", None)
quit_action = Gio.SimpleAction(name="quit")
quit_action.connect("activate", self._on_quit)
self.add_action(quit_action)

Expand All @@ -159,31 +161,32 @@ def setup_actions(self):
self.set_accels_for_action("win.listen-src", ["<Primary><Shift>L"])
self.set_accels_for_action("win.show-help-overlay", ["<Primary>question"])

def _on_pronunciation(self, action, value):
def _on_pronunciation(self, action: Gio.SimpleAction, value: GLib.Variant):
"""Update show pronunciation setting"""
action.props.state = value
Settings.get().show_pronunciation = value
Settings.get().show_pronunciation = value # type: ignore

# Update UI
if self.window.trans_src_pron is not None:
self.window.src_pron_revealer.props.reveal_child = value
if self.window.trans_dest_pron is not None:
self.window.dest_pron_revealer.props.reveal_child = value
if self.window:
if self.window.trans_src_pron is not None:
self.window.src_pron_revealer.props.reveal_child = value # type: ignore
if self.window.trans_dest_pron is not None:
self.window.dest_pron_revealer.props.reveal_child = value # type: ignore

def _on_preferences(self, _action, _param):
"""Show preferences window"""
window = DialectPreferencesDialog(self.window)
window.present(self.window)
if self.window:
window = DialectPreferencesDialog(self.window)
window.present(self.window)

def _on_about(self, _action, _param):
"""Show about dialog"""
builder = Gtk.Builder.new_from_resource(f"{RES_PATH}/about.ui")
about = builder.get_object("about")

about.props.application_icon = APP_ID
about.props.version = VERSION
about = Adw.AboutDialog.new_from_appdata(f"{RES_PATH}/appdata.xml", VERSION)
about.props.version = VERSION # For development version
about.props.comments = _("A translation app for GNOME.")
about.props.copyright = _("Copyright 2020–⁠2024 The Dialect Authors")
about.props.developers = ["Mufeed Ali", "Rafael Mardojai CM http://rafaelmardojai.com", "Libretto"]

about.props.translator_credits = _("translator-credits")
about.add_link(_("Donate"), "https://opencollective.com/dialect")

about.present(self.window)
Expand Down
Loading

0 comments on commit 939bef4

Please sign in to comment.