Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Build script fixes
  • Loading branch information
melianmiko committed Sep 15, 2024
1 parent ec1840e commit 158b843
Show file tree
Hide file tree
Showing 30 changed files with 47 additions and 203 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: Debian package
path: ./scripts/build_debian/openfreebuds_*.deb
path: ./scripts/build_debian/openfreebuds*
if-no-files-found: error
2 changes: 1 addition & 1 deletion openfreebuds/driver/huawei/handler/service_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OfbHuaweiVoiceLanguageHandler(OfbDriverHandlerHuawei):
properties = [
("service", "language")
]
handle_commands = [b'\x0c\x02']
commands = [b'\x0c\x02']
ignore_commands = [b"\x0c\x01"]

async def on_init(self):
Expand Down
8 changes: 4 additions & 4 deletions openfreebuds/driver/huawei/package.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from openfreebuds.driver.huawei.utils import crc16char, build_table_row
from openfreebuds.driver.huawei.utils import crc16_xmodem, build_table_row
from openfreebuds.exceptions import OfbPackageChecksumError


Expand Down Expand Up @@ -85,7 +85,7 @@ def to_bytes(self):

# Build package
result = b"Z" + (len(body) + 1).to_bytes(2, byteorder="big") + b"\x00" + body
result += crc16char(result)
result += crc16_xmodem(result)
return result

@staticmethod
Expand All @@ -104,8 +104,8 @@ def from_bytes(data: bytes, validate_checksum=False):
if validate_checksum:
crc_data = data[0:-2]
crc_value = data[-2:]
if crc16char(crc_data) != crc_value:
raise OfbPackageChecksumError(f"{crc16char(crc_data)} != {crc_value}")
if crc16_xmodem(crc_data) != crc_value:
raise OfbPackageChecksumError(f"{crc16_xmodem(crc_data)} != {crc_value}")

length = int.from_bytes(data[1:3], byteorder="big")

Expand Down
82 changes: 1 addition & 81 deletions openfreebuds/driver/huawei/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def build_table_row(ln, val, description_table=None):
return str(val).ljust(ln) + " | "


def crc16char(data):
def crc16_xmodem(data):
crc16_tab = [0, 4129, 8258, 12387, 16516, 20645, 24774, 28903, -32504, -28375, -24246, -20117, -15988, -11859,
-7730,
-3601, 4657, 528, 12915, 8786, 21173, 17044, 29431, 25302, -27847, -31976, -19589, -23718, -11331,
Expand Down Expand Up @@ -35,83 +35,3 @@ def crc16char(data):
s = s & 0b1111111111111111 # use only 16 bits

return s.to_bytes(2, "big")


def bytes2array(data):
out = []
for i in range(len(data)):
out.append(int.from_bytes(data[i:i + 1], "big", signed=True))
return out


def array2bytes(data):
b = b""
for a in data:
b += a.to_bytes(1, 'big', signed=True)
return b


class TLVPackage:
def __init__(self, pkg_type: int, data: array):
self.type = pkg_type
self.data = data
self.length = len(data)

def get_bytes(self):
return self.data.tobytes()

def get_string(self):
return self.data.tobytes().decode("utf8")


class TLVResponse:
def __init__(self):
self.contents: list[TLVPackage] = []

def __iter__(self):
return self.contents.__iter__()

def append(self, item):
self.contents.append(item)

def find_by_type(self, type_key) -> TLVPackage:
for a in self.contents:
if a.type == type_key:
return a
return TLVPackage(type_key, array("b", b""))

def find_by_types(self, types_list):
for a in self.contents:
if a.type in types_list:
return a
return TLVPackage(types_list[0], array("b", b""))


class TLVException(Exception):
pass


def parse_tlv(data: array) -> TLVResponse:
response = TLVResponse()
i = 0

while i <= len(data) - 2:
b_cur = data[i]
b_next = data[i + 1]
if (b_next & 128) != 0:
b_post = data[i + 2]
length = ((b_next & 127) << 7) + (b_post & 127)
pos = i + 3
else:
length = b_next & 127
pos = i + 2
i = pos + length
if length != 0:
if i > len(data):
raise TLVException("TLV Package overflow")
arr = data[pos:pos + length]
response.append(TLVPackage(b_cur, arr))
else:
response.append(TLVPackage(b_cur, array("b", b"")))

return response
2 changes: 0 additions & 2 deletions openfreebuds/manager/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@


class IOpenFreebuds(Subscription):
MAINLOOP_TIMEOUT = 1

STATE_DESTROYED = -1
STATE_STOPPED = 0
STATE_DISCONNECTED = 1
Expand Down
5 changes: 0 additions & 5 deletions openfreebuds/manager/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,6 @@ async def _mainloop_inner(self):

await asyncio.sleep(2)

async def _use_device_driver(self, driver: OfbDriverGeneric):
if self._driver:
await self._driver.stop()
self._driver = driver

async def _set_state(self, new_state: int):
if self._state == new_state:
return
Expand Down
1 change: 0 additions & 1 deletion openfreebuds/utils/stupid_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
class RemoteError(Exception):
def __init__(self, data):
super().__init__(data)
self.rpc_error_name = data["class"]
self.rpc_trace = data["trace"]
self.args = data["args"]

Expand Down
10 changes: 0 additions & 10 deletions openfreebuds_backend/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ def get_app_storage_path():
return os.getcwd() + "/data"


def open_in_file_manager(path):
log.info("open_dir " + path)


def open_file(path):
log.info("open_file " + path)

Expand Down Expand Up @@ -43,11 +39,5 @@ def bt_list_devices():
return []


def ask_string(message, callback):
logging.info("ask_str" + message)
if callback:
callback(None)


def is_dark_taskbar():
return False
4 changes: 0 additions & 4 deletions openfreebuds_backend/linux/linux_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ def get_app_storage_path():
return pathlib.Path.home() / ".config"


def open_in_file_manager(path):
subprocess.Popen(["xdg-open", path])


def open_file(path):
subprocess.Popen(["xdg-open", path])

Expand Down
4 changes: 0 additions & 4 deletions openfreebuds_backend/windows/misc_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ def get_app_storage_path():
return pathlib.Path.home() / "AppData/Roaming"


def open_in_file_manager(path):
os.startfile(path)


def open_file(path):
subprocess.Popen(["notepad.exe", path])

Expand Down
15 changes: 0 additions & 15 deletions openfreebuds_backend/windows/ui_win32.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import logging
import threading
import tkinter.simpledialog
import winreg

# noinspection PyUnresolvedReferences
from winsdk.windows.ui.viewmanagement import UISettings, UIColorType

log = logging.getLogger("OfbWindowsBackend")


# noinspection PyUnusedLocal
def ask_string(message, callback):
def run_async():
result = tkinter.simpledialog.askstring("OpenFreebuds", prompt=message)
callback(result)
threading.Thread(target=run_async).start()


def is_dark_taskbar():
with winreg.OpenKey(
Expand Down
7 changes: 0 additions & 7 deletions openfreebuds_qt/app/helper/setting_tab_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ def set_active_tab(self, section: int, tab: int):
def active_tab(self):
return self._active_entry

def retranslate_ui(self):
for section in self._sections:
if section.list_item is not None:
section.list_item.setText(self.root.tr(section.label))
for item in section.items:
item.list_item.label.setText(self.root.tr(item.label))

def add_tab(self, label: str, content: QWidget):
section_num = len(self._sections) - 1
tab = len(self._sections[section_num].items)
Expand Down
1 change: 0 additions & 1 deletion openfreebuds_qt/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __init__(self, ctx: IOfbQtApplication):

self.ctx = ctx
self.ofb = ctx.ofb
self._exit_started: bool = False

self.setupUi(self)

Expand Down
1 change: 0 additions & 1 deletion openfreebuds_qt/app/module/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .about import OfbQtAboutModule
from .choose_device import OfbQtChooseDeviceModule
from .common import OfbQtCommonModule
from .common_with_shortcuts import OfbQtCommonWithShortcutsModule
from .device_info import OfbQtDeviceInfoModule
from .device_other import OfbQtDeviceOtherSettingsModule
from .dual_connect import OfbQtDualConnectModule
Expand Down
3 changes: 0 additions & 3 deletions openfreebuds_qt/app/module/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ def open_website(self):
@pyqtSlot()
def open_github(self):
webbrowser.open(LINK_GITHUB)

def retranslate_ui(self):
self.retranslateUi(self)
3 changes: 0 additions & 3 deletions openfreebuds_qt/app/module/choose_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,3 @@ async def on_auto_config_toggle(self, value):

if value is True:
await OfbQtDeviceAutoSelect.trigger(self.ofb)

def retranslate_ui(self):
self.retranslateUi(self)
3 changes: 0 additions & 3 deletions openfreebuds_qt/app/module/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ def __init__(self, parent: QWidget, context: IOfbQtApplication):
self.ofb = context.ofb
self.list_item: Optional[OfbQListItem] = None

def retranslate_ui(self):
pass

async def update_ui(self, event: OfbCoreEvent):
pass
17 changes: 0 additions & 17 deletions openfreebuds_qt/app/module/common_with_shortcuts.py

This file was deleted.

3 changes: 0 additions & 3 deletions openfreebuds_qt/app/module/device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ def __init__(self, *args, **kwargs):

self.setupUi(self)

def retranslate_ui(self):
self.retranslateUi(self)

async def update_ui(self, event: OfbCoreEvent):
async with qt_error_handler("OfbQtDeviceInfoModule_UpdateUi", self.ctx):
if event.is_changed("info") or event.kind_match(OfbEventKind.DEVICE_CHANGED):
Expand Down
3 changes: 0 additions & 3 deletions openfreebuds_qt/app/module/device_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ async def update_ui(self, event: OfbCoreEvent):
)
self.service_language_box.setCurrentIndex(-1)

def retranslate_ui(self):
self.retranslateUi(self)

@asyncSlot(bool)
async def on_low_latency_toggle(self, value: bool):
async with qt_error_handler("OfbQtDeviceOtherSettingsModule_SetLowLatency", self.ctx):
Expand Down
3 changes: 0 additions & 3 deletions openfreebuds_qt/app/module/dual_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ def __init__(self, *args, **kwargs):

self.setupUi(self)

def retranslate_ui(self):
self.retranslateUi(self)

async def update_ui(self, event: OfbCoreEvent):
async with qt_error_handler("OfbQtDualConnectModule_UpdateUi", self.ctx):
# Setup visibility
Expand Down
2 changes: 1 addition & 1 deletion openfreebuds_qt/app/module/gestures.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def set_right_available(self, value: bool):
return
if self.right_combo is not None:
index = self.grid.indexOf(self.left_combo)
r, c, rs, cs = self.grid.getItemPosition(index)
r, c, rs, _ = self.grid.getItemPosition(index)
self.grid.takeAt(index)
self.grid.addWidget(self.left_combo, r, c, rs, 1 if value else 2)
self.right_combo.setVisible(value)
Expand Down
13 changes: 6 additions & 7 deletions openfreebuds_qt/app/module/hotkeys_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@

from openfreebuds.shortcuts import OfbShortcuts
from openfreebuds.utils.logger import create_logger
from openfreebuds_qt.app.module import OfbQtCommonModule
from openfreebuds_qt.config import OfbQtConfigParser
from openfreebuds_qt.designer.hotkeys import Ui_OfbQtHotkeysModule
from openfreebuds_qt.qt_i18n import get_shortcut_names
from openfreebuds_qt.utils.hotkeys.recorder import OfbQtHotkeyRecorder
from openfreebuds_qt.utils.hotkeys.service import OfbQtHotkeyService
from openfreebuds_qt.app.module.common_with_shortcuts import OfbQtCommonWithShortcutsModule
from openfreebuds_qt.utils.qt_utils import qt_error_handler, blocked_signals
from openfreebuds_qt.config import OfbQtConfigParser
from openfreebuds_qt.designer.hotkeys import Ui_OfbQtHotkeysModule

log = create_logger("OfbQtHotkeysModule")


class OfbQtHotkeysModule(Ui_OfbQtHotkeysModule, OfbQtCommonWithShortcutsModule):
class OfbQtHotkeysModule(Ui_OfbQtHotkeysModule, OfbQtCommonModule):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.shortcut_names = get_shortcut_names()
self.service = OfbQtHotkeyService.get_instance(self.ofb)
self.config = OfbQtConfigParser.get_instance()
self.recorder = OfbQtHotkeyRecorder()
Expand Down Expand Up @@ -70,6 +72,3 @@ async def on_edit_shortcut(self, index: int, column: int):
self.service.start()

self.table.setItem(index, column, QTableWidgetItem(self.config.get("hotkeys", shortcut, "Disabled")))

def retranslate_ui(self):
self.retranslateUi(self)
3 changes: 0 additions & 3 deletions openfreebuds_qt/app/module/sound_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ async def _on_change(value: int):
self.custom_eq_rows_layout.addWidget(slider)
self._eq_rows.append(slider)

def retranslate_ui(self):
self.retranslateUi(self)

async def update_ui(self, event: OfbCoreEvent):
sound = await self.ofb.get_property("sound")
self.list_item.setVisible(sound is not None)
Expand Down
Loading

0 comments on commit 158b843

Please sign in to comment.