Skip to content

Commit 69a9dff

Browse files
committed
Removed numpy dependency
1 parent e2f0dfd commit 69a9dff

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/pywinctl/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ class Re:
5656
NOTENDSWITH: lambda s1, s2, fl: not s2.endswith(s1),
5757
MATCH: lambda s1, s2, fl: bool(s1.search(s2)),
5858
NOTMATCH: lambda s1, s2, fl: not (bool(s1.search(s2))),
59-
EDITDISTANCE: lambda s1, s2, fl: _levenshtein(s1, s2, fl),
59+
EDITDISTANCE: lambda s1, s2, fl: _levenshtein(s1, s2) >= fl,
6060
DIFFRATIO: lambda s1, s2, fl: difflib.SequenceMatcher(None, s1, s2).ratio() * 100 >= fl
6161
}
6262

6363

64-
def _levenshtein(seq1: str, seq2: str, similarity: int = 90) -> bool:
64+
def _levenshtein(seq1: str, seq2: str) -> float:
6565
# https://stackabuse.com/levenshtein-distance-and-text-similarity-in-python/
6666
# Adapted to return a similarity percentage, which is easier to define
67+
# Removed numpy to reduce dependencies. This is likely slower, but titles are not too long
6768
size_x = len(seq1) + 1
6869
size_y = len(seq2) + 1
6970
matrix = [[0 for y in range(size_y)] for x in range(size_x)]
@@ -87,7 +88,7 @@ def _levenshtein(seq1: str, seq2: str, similarity: int = 90) -> bool:
8788
matrix[x][y - 1] + 1
8889
)
8990
dist = matrix[size_x - 1][size_y - 1]
90-
return ((1 - dist / max(len(seq1), len(seq2))) * 100) >= similarity
91+
return (1 - dist / max(len(seq1), len(seq2))) * 100
9192

9293

9394
class BaseWindow:

src/pywinctl/_pywinctl_linux.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def checkPermissions(activate: bool = False):
7575
macOS ONLY: Check Apple Script permissions for current script/app and, optionally, shows a
7676
warning dialog and opens security preferences
7777
78-
:param activate: If ''True'', shows a dialog and opens security preferences. Defaults to ''False''
78+
:param activate: If ''True'' and if permissions are not granted, shows a dialog and opens security preferences.
79+
Defaults to ''False''
7980
:return: returns ''True'' if permissions are already granted or platform is not macOS
8081
"""
8182
return True

src/pywinctl/_pywinctl_macos.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def checkPermissions(activate: bool = False):
2828
macOS ONLY: Check Apple Script permissions for current script/app and, optionally, shows a
2929
warning dialog and opens security preferences
3030
31-
:param activate: If ''True'', shows a dialog and opens security preferences. Defaults to ''False''
31+
:param activate: If ''True'' and if permissions are not granted, shows a dialog and opens security preferences.
32+
Defaults to ''False''
3233
:return: returns ''True'' if permissions are already granted or platform is not macOS
3334
"""
3435
# https://stackoverflow.com/questions/26591560/how-to-grant-applescript-permissions-through-applescript

src/pywinctl/_pywinctl_win.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def checkPermissions(activate: bool = False):
3030
macOS ONLY: Check Apple Script permissions for current script/app and, optionally, shows a
3131
warning dialog and opens security preferences
3232
33-
:param activate: If ''True'', shows a dialog and opens security preferences. Defaults to ''False''
33+
:param activate: If ''True'' and if permissions are not granted, shows a dialog and opens security preferences.
34+
Defaults to ''False''
3435
:return: returns ''True'' if permissions are already granted or platform is not macOS
3536
"""
3637
return True
@@ -707,6 +708,27 @@ def getDisplay(self) -> str:
707708
pass
708709
return name
709710

711+
def _getContent(self):
712+
# https://stackoverflow.com/questions/14500026/python-how-to-get-the-text-label-from-another-program-window
713+
# Does not work with Terminal or Chrome. Besides, I don't think it can be done in Linux nor macOS
714+
715+
content = []
716+
717+
def findContent(childWindow):
718+
bufferSize = win32gui.SendMessage(childWindow, win32con.WM_GETTEXTLENGTH, 0, 0) * 2
719+
buf = win32gui.PyMakeBuffer(bufferSize)
720+
win32gui.SendMessage(childWindow, win32con.WM_GETTEXT, bufferSize, buf)
721+
a = buf.tobytes().decode('UTF-16', 'replace')
722+
if a:
723+
b = a.split("\r\n")
724+
content.append(b)
725+
726+
children = self.getChildren()
727+
for child in children:
728+
findContent(child)
729+
return content
730+
731+
710732
@property
711733
def isMinimized(self) -> bool:
712734
"""

0 commit comments

Comments
 (0)