Pyinstaller support for assets

This commit is contained in:
StNicolay 2024-08-21 04:05:36 +03:00
parent d6ecae08bd
commit 04a27b592e
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
5 changed files with 19 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import pydantic
from PyQt6.QtGui import QIcon from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QFileDialog, QLabel, QMessageBox, QWidget from PyQt6.QtWidgets import QFileDialog, QLabel, QMessageBox, QWidget
from request_client import RequestClient from request_client import RequestClient
from utils import resource_path
class File(pydantic.BaseModel): class File(pydantic.BaseModel):
@ -54,7 +55,7 @@ class File(pydantic.BaseModel):
return size, power_labels[n] + "bytes" return size, power_labels[n] + "bytes"
def icon(self) -> QIcon: def icon(self) -> QIcon:
return QIcon("assets/file.png") return QIcon(resource_path("assets/file.png"))
def double_click(self, list: file_widgets.FileListWidget) -> None: def double_click(self, list: file_widgets.FileListWidget) -> None:
location = QFileDialog.getExistingDirectory( location = QFileDialog.getExistingDirectory(

View File

@ -5,9 +5,9 @@ import uuid
from typing import Protocol, Self from typing import Protocol, Self
import create_folder_widget import create_folder_widget
import sync
import pydantic import pydantic
import state import state
import sync
import user import user
from file import File from file import File
from folder import Folder from folder import Folder

View File

@ -1,8 +1,8 @@
from __future__ import annotations from __future__ import annotations
import datetime import datetime
import uuid
import typing import typing
import uuid
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
import file_widgets import file_widgets
@ -11,6 +11,7 @@ import pydantic
from PyQt6.QtGui import QIcon from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QMessageBox, QWidget from PyQt6.QtWidgets import QMessageBox, QWidget
from request_client import RequestClient from request_client import RequestClient
from utils import resource_path
class Folder(pydantic.BaseModel): class Folder(pydantic.BaseModel):
@ -38,7 +39,7 @@ class Folder(pydantic.BaseModel):
return folder_info.FolderInfoWidget(self) return folder_info.FolderInfoWidget(self)
def icon(self) -> QIcon: def icon(self) -> QIcon:
return QIcon("assets/folder.png") return QIcon(resource_path("assets/folder.png"))
def double_click(self, list: file_widgets.FileListWidget) -> None: def double_click(self, list: file_widgets.FileListWidget) -> None:
import file_widgets import file_widgets

View File

@ -3,7 +3,7 @@ import file_widgets
import keyring import keyring
import request_client import request_client
import sync import sync
from PyQt6.QtWidgets import QMainWindow, QStackedWidget, QMessageBox from PyQt6.QtWidgets import QMainWindow, QMessageBox, QStackedWidget
class State(QMainWindow): class State(QMainWindow):

12
desktop_client/utils.py Normal file
View File

@ -0,0 +1,12 @@
import os
import sys
from functools import cache
_DEFAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@cache
def resource_path(relative_path: str) -> str:
"""Get absolute path to resource, works for dev and for PyInstaller"""
base_path = getattr(sys, "_MEIPASS", _DEFAULT)
return os.path.join(base_path, relative_path)