Skip to content

Commit f537421

Browse files
authored
Version 3.0.8
* Corrected file handling behaviours. * Type hint corrections.
1 parent ffd7805 commit f537421

File tree

5 files changed

+52
-41
lines changed

5 files changed

+52
-41
lines changed

pypdfeditor_core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
__system__ = platform.system()
1717
__author__ = "Nianze A. TAO (Omozawa SUENO)"
18-
__version__ = "3.0.7"
18+
__version__ = "3.0.8"
1919
__all__ = ["main", "reset", "remove"]
2020

2121

pypdfeditor_core/application.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import getpass
1111
import subprocess as sp
1212
from pathlib import Path
13-
from typing import Dict
13+
from typing import List, Dict, Union, Optional
1414
import pymupdf as fitz
1515
from PyQt6 import QtCore
1616
from PyQt6.QtGui import QColor, QPixmap, QCloseEvent
17-
from PyQt6.QtWidgets import QColorDialog
17+
from PyQt6.QtWidgets import QColorDialog, QWidget
1818
from .language import set_language, lag_s, lag_p
1919
from .windows import (
2020
MainR,
@@ -158,7 +158,7 @@ def __init__(self, system: str, version: str):
158158
if not self.settings.value("windowState") == None:
159159
self.restoreState(self.settings.value("windowState"))
160160

161-
def closeEvent(self, event: QCloseEvent) -> None:
161+
def closeEvent(self, event: Optional[QCloseEvent]) -> None:
162162
"""
163163
write settings to settings.json
164164
"""
@@ -212,7 +212,12 @@ def enable_perm_set(self) -> None:
212212
self.tab3.button6.clicked.disconnect()
213213
self.perm_int = 4028 # value of all permissions
214214

215-
def view(self, index=None, widget=None, f_name=None) -> None:
215+
def view(
216+
self,
217+
index: Optional[int] = None,
218+
widget: Optional[QWidget] = None,
219+
f_name: Optional[str] = None,
220+
) -> None:
216221
"""
217222
open file outside the application
218223
@@ -381,7 +386,7 @@ def add1(self) -> None:
381386
f_name, _ = add(
382387
self,
383388
"PDF files (*.pdf);;"
384-
"images (*.png *.jpg *.jpeg *.bmp *.tiff *.svg);;"
389+
"images (*.png *.jpg *.jpeg *.bmp *.tiff *.svg *.JPG);;"
385390
"ebooks (*.epub *.xps *.fb2 *.cbz)",
386391
)
387392
if _:
@@ -460,7 +465,7 @@ def add4(self) -> None:
460465
del doc, state
461466
return
462467

463-
def get_data(self, par1, par2, par3, par5) -> None:
468+
def get_data(self, par1: str, par2: str, par3: bool, par5: str) -> None:
464469
"""
465470
obtain settings from setting window
466471
"""
@@ -472,7 +477,7 @@ def get_data(self, par1, par2, par3, par5) -> None:
472477
set_language(self)
473478
self.setCurrentIndex(i)
474479

475-
def get_font_dir(self, par) -> None:
480+
def get_font_dir(self, par: str) -> None:
476481
"""
477482
obtain font file directory from font window
478483
"""
@@ -560,7 +565,7 @@ def preview(self) -> None:
560565
del doc
561566

562567
@staticmethod
563-
def show_index(par, widget):
568+
def show_index(par: List[int], widget: QWidget):
564569
"""
565570
show recent clicked page number
566571
"""
@@ -576,7 +581,7 @@ class Setting(SettingR):
576581

577582
signal = QtCore.pyqtSignal(str, str, bool, str)
578583

579-
def __init__(self, set_dict: dict):
584+
def __init__(self, set_dict: Dict[str, Union[str, bool]]):
580585
super().__init__()
581586
self.s_dir = set_dict["start dir"]
582587
self.o_dir = set_dict["save dir"]
@@ -620,7 +625,7 @@ def _enable_select(self):
620625
self.line1.setReadOnly(False)
621626
self.line2.setReadOnly(False)
622627

623-
def closeEvent(self, event: QCloseEvent) -> None:
628+
def closeEvent(self, event: Optional[QCloseEvent]) -> None:
624629
"""
625630
re-write closeEvent
626631
"""
@@ -647,7 +652,7 @@ def set_language(self, language: str) -> None:
647652
"""
648653
lag_p(self, language)
649654

650-
def closeEvent(self, event: QCloseEvent) -> None:
655+
def closeEvent(self, event: Optional[QCloseEvent]) -> None:
651656
"""
652657
close event
653658
"""
@@ -723,7 +728,7 @@ def change_text_font(self) -> None:
723728
fitz.TOOLS.store_shrink(100) # delete MuPDF cache
724729
del cover, shape, page, r1, doc
725730

726-
def closeEvent(self, event: QCloseEvent) -> None:
731+
def closeEvent(self, event: Optional[QCloseEvent]) -> None:
727732
"""
728733
close event
729734
"""

pypdfeditor_core/functions.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
import json
1010
import time
11-
from typing import Union, Optional, Tuple, List
11+
from typing import Union, Optional, Tuple, List, Dict
1212
from pathlib import Path
1313
from pymupdf.mupdf import FzErrorFormat, FzErrorLibrary
1414
from pymupdf import Document, Page, Pixmap, Rect, Point, Font
@@ -38,7 +38,7 @@ class Doc(Document):
3838
"""
3939

4040
pass_word: Optional[str] = None
41-
rotatedPages = {}
41+
rotatedPages: Dict[int, int] = {}
4242

4343

4444
def copy(doc: Doc) -> Doc:
@@ -76,7 +76,10 @@ def open_pdf(file_name: str, parent: QWidget) -> Tuple[Optional[Doc], bool]:
7676
except RuntimeError:
7777
return _open_warning(parent)
7878
if not doc.is_pdf:
79-
if file_name.endswith(SUPPORT_IMG_FORMAT[:-1]):
79+
_support_img_format = SUPPORT_IMG_FORMAT[:-1] + tuple(
80+
i.upper() for i in SUPPORT_IMG_FORMAT[:-1]
81+
)
82+
if file_name.endswith(_support_img_format):
8083
try: # handle wrong image formats
8184
pdf_bites = Pixmap(file_name).tobytes()
8285
doc = Doc("png", pdf_bites)
@@ -106,7 +109,7 @@ def open_pdf(file_name: str, parent: QWidget) -> Tuple[Optional[Doc], bool]:
106109
return doc, True
107110

108111

109-
def render_pdf_page(page_data: Doc.load_page) -> QtGui.QPixmap:
112+
def render_pdf_page(page_data: Page) -> QtGui.QPixmap:
110113
"""
111114
render PDF page
112115
@@ -131,7 +134,7 @@ def render_pdf_page(page_data: Doc.load_page) -> QtGui.QPixmap:
131134
return pixmap
132135

133136

134-
def pdf_split(doc: Doc) -> List:
137+
def pdf_split(doc: Doc) -> List[int]:
135138
"""
136139
split the selected PDF file into pages;
137140
@@ -190,7 +193,7 @@ def add_watermark(
190193
return doc
191194

192195

193-
def setting_warning(set_file_name: str, parent: QWidget) -> dict:
196+
def setting_warning(set_file_name: str, parent: QWidget) -> Dict[str, Union[str, bool]]:
194197
"""
195198
import settings from the JSON file
196199
@@ -327,7 +330,7 @@ def set_icon(
327330
TOOLS.store_shrink(100) # delete MuPDF cache
328331

329332

330-
def add(main: QWidget, _format: str) -> Tuple[str]:
333+
def add(main: QWidget, _format: str) -> Tuple[str, str]:
331334
"""
332335
add a file
333336
@@ -336,14 +339,15 @@ def add(main: QWidget, _format: str) -> Tuple[str]:
336339
:return: [f_name, state]
337340
"""
338341
f_name, state = QFileDialog.getOpenFileName(main, "Open files", main.s_dir, _format)
339-
if state and not f_name.endswith(SUPPORT_FORMAT):
342+
_support_format = SUPPORT_FORMAT + tuple(i.upper() for i in SUPPORT_FORMAT)
343+
if state and not f_name.endswith(_support_format):
340344
return "", ""
341345
if state and main.dir_store_state:
342346
main.s_dir = os.path.dirname(f_name)
343347
return f_name, state
344348

345349

346-
def save(main: QWidget, _format: str) -> Tuple[str]:
350+
def save(main: QWidget, _format: str) -> Tuple[str, str]:
347351
"""
348352
save a file
349353
@@ -645,7 +649,7 @@ def choose(widget: QtWidgets.QLineEdit, c_dir: str) -> None:
645649
widget.setText(root)
646650

647651

648-
def remove_invalid_xref_key(metadata: dict) -> dict:
652+
def remove_invalid_xref_key(metadata: Dict[str, str]) -> Dict[str, str]:
649653
"""
650654
remove invalid xref key(s)
651655
"""
@@ -697,8 +701,8 @@ def set_metadata0(doc: Doc, author: Optional[str]) -> None:
697701

698702

699703
def set_metadata1(
700-
metadata: dict, title: str, author: str, subject: str, keywords: str
701-
) -> dict:
704+
metadata: Dict[str, str], title: str, author: str, subject: str, keywords: str
705+
) -> Dict[str, str]:
702706
"""
703707
set metadata to pdf document
704708
@@ -731,7 +735,7 @@ def set_metadata1(
731735
return metadata
732736

733737

734-
def toc2plaintext(toc: List) -> str:
738+
def toc2plaintext(toc: List[List[Union[str, int]]]) -> str:
735739
"""
736740
:param toc: table of content <- DOCUMENT.get_toc()
737741
:return: plaintext
@@ -744,7 +748,7 @@ def toc2plaintext(toc: List) -> str:
744748
return plaintext
745749

746750

747-
def plaintext2toc(plaintext: str) -> List[List]:
751+
def plaintext2toc(plaintext: str) -> List[List[Union[str, int]]]:
748752
"""
749753
:param plaintext: plaintext
750754
:return: table of content -> DOCUMENT.get_toc()
@@ -759,7 +763,7 @@ def plaintext2toc(plaintext: str) -> List[List]:
759763
return toc
760764

761765

762-
def find_font(font_dirs: List) -> Tuple[dict]:
766+
def find_font(font_dirs: List[str]) -> Tuple[Dict[str, str], Dict[str, str]]:
763767
"""
764768
find all TrueType font files (.ttf): all their font name and file addresses
765769
then write their directories to a json file
@@ -785,7 +789,7 @@ def find_font(font_dirs: List) -> Tuple[dict]:
785789
return name_dict, dir_dict
786790

787791

788-
def store_font_path(name_dict: dict, cache_file_name: str) -> None:
792+
def store_font_path(name_dict: Dict[str, str], cache_file_name: str) -> None:
789793
"""
790794
store the font file dict
791795
@@ -796,7 +800,7 @@ def store_font_path(name_dict: dict, cache_file_name: str) -> None:
796800
json.dump(name_dict, f, sort_keys=True, indent=4, separators=(",", ": "))
797801

798802

799-
def read_from_font_cache(cache_file_name: str) -> Tuple[dict]:
803+
def read_from_font_cache(cache_file_name: str) -> Tuple[Dict[str, str], Dict[str, str]]:
800804
"""
801805
read font directories from json file
802806

pypdfeditor_core/window_effect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
from ctypes import POINTER, c_int, WinDLL, Structure, byref, cast
77
from ctypes.wintypes import RECT, UINT, HWND, MSG
8+
from PyQt6.QtCore import QRect
89

910

1011
class PWindowPOS(Structure):
@@ -69,7 +70,7 @@ def add_shadow_effect(self, h_wnd: int) -> None:
6970
self._extend(h_wnd, byref(margins))
7071

7172
@staticmethod
72-
def monitorNCCALCSIZE(_msg: MSG, geometry) -> None:
73+
def monitorNCCALCSIZE(_msg: MSG, geometry: QRect) -> None:
7374
"""
7475
resize the window to fit the screen
7576

pypdfeditor_core/windows.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
application window forms
55
"""
66
import re
7-
from typing import Tuple, Union
7+
from typing import Tuple, Union, Optional
88
from PyQt6.sip import voidptr
99
from PyQt6.QtGui import (
1010
QIcon,
@@ -60,7 +60,7 @@ class SwitchBtn(QWidget):
6060

6161
stateChanged = QtCore.pyqtSignal(bool)
6262

63-
def __init__(self, parent=None):
63+
def __init__(self, parent: Optional[QWidget] = None):
6464
super().__init__(parent)
6565
self.checked = False
6666
self.bgColorOff = QColor("#e2e2dd")
@@ -80,10 +80,11 @@ def __init__(self, parent=None):
8080
self.timer.timeout.connect(self.update_value)
8181
self.setFont(QFont("calibri", 14))
8282

83-
def setStyleSheet(self, style_sheet: str) -> None:
83+
def setStyleSheet(self, style_sheet: Optional[str]) -> None:
8484
"""
8585
define set-style-sheet behaviour
8686
"""
87+
assert style_sheet is not None
8788
style_sheet = re.sub(r"\s+", "", style_sheet)
8889
on_style = str(re.findall(r"SwitchBtn:on{.+?}", style_sheet))
8990
off_style = str(re.findall(r"SwitchBtn:off{.+?}", style_sheet))
@@ -122,7 +123,7 @@ def update_value(self) -> None:
122123
self.timer.stop()
123124
self.update()
124125

125-
def mousePressEvent(self, event: QMouseEvent) -> None:
126+
def mousePressEvent(self, event: Optional[QMouseEvent]) -> None:
126127
"""
127128
mousePressEvent
128129
"""
@@ -154,7 +155,7 @@ def isChecked(self) -> bool:
154155
"""
155156
return self.checked
156157

157-
def paintEvent(self, event: QPaintEvent) -> None:
158+
def paintEvent(self, event: Optional[QPaintEvent]) -> None:
158159
"""
159160
paintEvent
160161
"""
@@ -251,7 +252,7 @@ class TableWidget(QTableWidget):
251252

252253
Index = QtCore.pyqtSignal(tuple)
253254

254-
def mousePressEvent(self, event: QMouseEvent) -> None:
255+
def mousePressEvent(self, event: Optional[QMouseEvent]) -> None:
255256
"""
256257
re-write mousePressEvent
257258
"""
@@ -468,7 +469,7 @@ def restoreState(self, state: QtCore.QByteArray) -> None:
468469
x, y = [int(i) for i in str(state)[2:-1].split(",")]
469470
self.move(x, y)
470471

471-
def paintEvent(self, event: QPaintEvent) -> None:
472+
def paintEvent(self, event: Optional[QPaintEvent]) -> None:
472473
"""
473474
re-write paintEvent
474475
"""
@@ -486,7 +487,7 @@ def paintEvent(self, event: QPaintEvent) -> None:
486487

487488
# -------well, why do the following ugly codes exist?-------
488489
# -------they are used to re-enable the window animations under Windows platform-------
489-
def mouseMoveEvent(self, event: QMouseEvent) -> None:
490+
def mouseMoveEvent(self, event: Optional[QMouseEvent]) -> None:
490491
"""
491492
re-write mouseMoveEvent
492493
move the frameless window
@@ -499,7 +500,7 @@ def mouseMoveEvent(self, event: QMouseEvent) -> None:
499500
self.windowEffect.move_window(int(self.winId()))
500501
return QTabWidget.mouseMoveEvent(self, event)
501502

502-
def mouseDoubleClickEvent(self, event: QMouseEvent) -> None:
503+
def mouseDoubleClickEvent(self, event: Optional[QMouseEvent]) -> None:
503504
"""
504505
re-write mouseDoubleClickEvent
505506
"""
@@ -566,7 +567,7 @@ def nativeEvent(
566567

567568
# -------here ends the ugly code-------
568569

569-
def resizeEvent(self, event: QResizeEvent) -> None:
570+
def resizeEvent(self, event: Optional[QResizeEvent]) -> None:
570571
"""
571572
re-write resizeEvent
572573
"""

0 commit comments

Comments
 (0)