Skip to content

Commit

Permalink
Flake8, bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cary-rowen committed Jan 7, 2024
1 parent bdab709 commit 1e4194e
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"recommendations": [
"ms-python.python",
"ms-python.vscode-pylance",
"redhat.vscode-yaml"
"redhat.vscode-yaml",
"ms-python.flake8"
],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
Expand Down
15 changes: 5 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
{
"editor.accessibilitySupport": "on",
"python.linting.enabled": true,
"python.linting.maxNumberOfProblems": 10000,
"python.linting.flake8Args": [
"flake8.args": [
"--config=flake8.ini"
],
"python.linting.flake8Enabled": true,
"python.linting.pylintEnabled": false,
"flake8.importStrategy": "fromEnvironment",
"python.autoComplete.extraPaths": [
"../nvda/source",
"../nvda/miscDeps/python"
],
],
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"editor.insertSpaces": false,
"python.analysis.diagnosticSeverityOverrides": {
"reportUndefinedVariable": "none"
},
"python.analysis.stubPath": "${workspaceFolder}/.vscode/typings",
"python.analysis.extraPaths": [
"../nvda/source",
"../nvda/miscDeps/python"
],
"python.defaultInterpreterPath": "../nvda/.venv/scripts/python.exe"
"python.defaultInterpreterPath": "${workspaceFolder}/../nvda/.venv/scripts/python.exe"
}
6 changes: 6 additions & 0 deletions .vscode/typings/__builtins__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def _(msg: str) -> str:
...


def pgettext(context: str, message: str) -> str:
...
120 changes: 78 additions & 42 deletions addon/appModules/wechat.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import wx
from os.path import dirname, join

import api
import appModuleHandler
import speech
import config
import ui
import api
import eventHandler
import controlTypes
import eventHandler
import mouseHandler
import speech
import ui
import winUser
import wx
from NVDAObjects import NVDAObjectTextInfo
from versionInfo import version_year
from scriptHandler import script
from nvwave import playWaveFile
from os.path import join, dirname
role = controlTypes.Role if version_year>=2022 else controlTypes.role.Role
from scriptHandler import script
from versionInfo import version_year

role = controlTypes.Role if version_year >= 2022 else controlTypes.role.Role


class AppModule(appModuleHandler.AppModule):
ReportOCRResultTimer=None
OCRResult=None
ReportOCRResultTimer = None
OCRResult = None
SOUND_LINK = join(dirname(__file__), "link.wav")

SOUND_POPUP = join(dirname(__file__), "popup.wav")
confspec = {
"isAutoMSG": "boolean(default=False)"
"isAutoMSG": "boolean(default=False)"
}
config.conf.spec["WeChatEnhancement"] = confspec
isAutoMSG = config.conf["WeChatEnhancement"]["isAutoMSG"]
Expand All @@ -34,85 +37,116 @@ def event_NVDAObject_init(self, obj):

def event_nameChange(self, obj, nextHandler):
try:
if obj.role==role.LISTITEM and obj.parent.name=="消息" and obj.simpleFirstChild:
if obj.role == role.LISTITEM and obj.parent.name == "消息" and obj.simpleFirstChild:
playWaveFile(self.SOUND_POPUP)
if self.isAutoMSG:
if obj.name==None:
if obj.name is None:
children = obj.recursiveDescendants
for child in children:
if not speech.isBlank(child.name): ui.message(child.name)
elif obj.simpleFirstChild.role==role.BUTTON:
if not speech.isBlank(child.name):
ui.message(child.name)
elif obj.simpleFirstChild.role == role.BUTTON:
ui.message("%s %s" % (obj.simpleFirstChild.name, obj.name))
except: pass
except ValueError:
pass
nextHandler()

def event_gainFocus(self, obj, nextHandler, isFocus=False):
# 读出被转发的消息
try:
if obj.name==None:
if obj.role==role.LISTITEM:
children = obj.recursiveDescendants
for child in children:
if not speech.isBlank(child.name): ui.message(child.name)
return
except AttributeError: pass
if obj.windowClassName == "ChatRecordWnd":
if obj.role == role.LISTITEM:
date = ""
msg = []
children = obj.recursiveDescendants
for child in children:
if not speech.isBlank(child.name):
from datetime import datetime
try:
datetime.strptime(child.name, "%m-%d %H:%M:%S")
date = child.name
except ValueError:
msg.append(child.name)
msg.append(date)
obj.name = ','.join(msg)
# 消息列表中特殊消息音效提醒
try:
if obj.role==role.LISTITEM and obj.parent.name=="消息":
if obj.value != None:
if obj.role == role.LISTITEM and obj.parent.name == "消息":
if obj.value is not None:
playWaveFile(self.SOUND_LINK)
except AttributeError: pass
except AttributeError:
pass
# 群组中成员昵称的报告方式
try:
if obj.role==role.BUTTON and obj.simpleParent.role==role.LISTITEM:
if obj.next.firstChild.firstChild.role==role.STATICTEXT:
if obj.role == role.BUTTON and obj.simpleParent.role == role.LISTITEM:
if obj.next.firstChild.firstChild.role == role.STATICTEXT:
obj.name = obj.next.firstChild.firstChild.name
except AttributeError: pass
except AttributeError:
pass

nextHandler()


@script(
description="是否自动朗读新消息",
category="PC微信增强",
gesture="kb:f3"
)
def script_autoMSG(self,gesture):
self.isAutoMSG=not self.isAutoMSG
config.conf["WeChatEnhancement"]["isAutoMSG"]=self.isAutoMSG
def script_autoMSG(self, gesture):
self.isAutoMSG = not self.isAutoMSG
config.conf["WeChatEnhancement"]["isAutoMSG"] = self.isAutoMSG
if self.isAutoMSG:
ui.message("自动读出新消息")
else:
ui.message("默认")

def FindDocumentObject(self):
fg = api.getForegroundObject()
if fg.simpleFirstChild:
child = fg.simpleFirstChild
api.setNavigatorObject(child)
obj = api.getNavigatorObject()
if obj.role == role.DOCUMENT:
eventHandler.executeEvent("gainFocus", obj)

@script(
description="定位网页文档",
category="PC微信增强",
gesture="kb:f6"
)
def script_setWindow(self, gesture):
self.FindDocumentObject()

def event_foreground(self, obj, nextHandler):
if obj.windowClassName == "ImagePreviewWnd":
wx.CallLater(800, self.clickButton, "提取文字", 0)
wx.CallLater(100, self.ReportOCRResult)
elif obj.windowClassName in ("CefWebViewWnd", "SubscriptionWnd"):
wx.CallLater(1000, self.FindDocumentObject)
else:
if self.ReportOCRResultTimer:
self.ReportOCRResultTimer.Stop()
self.OCRResult=None
self.OCRResult = None
nextHandler()

def ReportOCRResult(self):
fg=api.getForegroundObject()
fg = api.getForegroundObject()
try:
if fg.simpleLastChild.role==role.STATICTEXT:
if fg.simpleLastChild.role == role.STATICTEXT:
if self.OCRResult != fg.simpleLastChild.name:
self.OCRResult = fg.simpleLastChild.name
ui.message(self.OCRResult)
fg.simpleLastChild.name = self.OCRResult
else: self.OCRResult = None
except: pass
else:
self.OCRResult = None
except AttributeError:
pass
self.ReportOCRResultTimer = wx.CallLater(100, self.ReportOCRResult)

@script(
description="关闭微信内置浏览器窗口",
category="PC微信增强",
gesture="kb:control+w"
)
def script_close(self,gesture):
def script_close(self, gesture):
if api.getForegroundObject().windowClassName in ("CefWebViewWnd", "ImagePreviewWnd"):
self.clickButton("关闭", 0)

Expand All @@ -123,9 +157,11 @@ def clickButton(self, name, depth):
Depth = 0
for child in obj.recursiveDescendants:
Depth += 1
if depth !=0 and Depth >= depth: break
if depth != 0 and Depth >= depth:
break
if child.role == role.BUTTON and child.name == name:
self.click(child)

def click(self, obj):
l, t, w, h = obj.location
x, y = int(l + w / 2), int(t + h / 2)
Expand Down
45 changes: 27 additions & 18 deletions addon/appModules/wechatappex.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import appModuleHandler
import api
import ui
import mouseHandler
import winUser
from scriptHandler import script
import controlTypes
import ui
from versionInfo import version_year

role = controlTypes.Role if version_year>=2022 else controlTypes.role.Role
role = controlTypes.Role if version_year >= 2022 else controlTypes.role.Role


def find_nth_from_end(string, substring, n):
if n <= 0 or substring not in string:
return -1
count = string.count(substring)
if n > count: # 如果n大于出现次数,则直接返回-1
return -1
start = len(string)
for _ in range(n):
idx = string.rfind(substring, 0, start)
start = idx # 更新下次查找的结束位置为当前找到的位置
return start # 返回找到的索引


class AppModule(appModuleHandler.AppModule):

def event_gainFocus(self, obj, nextHandler, isFocus=False):
try:
if obj.treeInterceptor and obj.role==role.LIST and "discuss_list" == obj.IA2Attributes.get("class"):
o=obj.firstChild.firstChild
while o:
ui.message(o.firstChild.name)
o=o.next
elif obj.treeInterceptor and obj.role==role.LISTITEM and "js_comment" in obj.IA2Attributes.get("class"):
ui.message(obj.firstChild.name)
elif obj.treeInterceptor and obj.role==role.BUTTON and "sns_opr_btn sns_praise_btn" == obj.IA2Attributes.get("class"):
ui.message(obj.simplePrevious.name)
except: pass
message = []
if obj.treeInterceptor and obj.role == role.BUTTON \
and obj.IA2Attributes.get('class', '') == 'sns_opr_btn sns_praise_btn':
while "discuss_user_avatar" not in obj.IA2Attributes.get('class', ''):
if obj.IA2Attributes.get('display', '') == 'inline':
message.append(obj.name)
obj = obj.simplePrevious
temp = obj.name
parts = temp.split(' ,')
temp = ','.join(parts[:-3]) if len(parts) >= 3 else temp
message.append(temp)
ui.message(','.join(message))
nextHandler()

5 changes: 3 additions & 2 deletions addon/doc/zh_CN/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* 作者: 沉浮 & Eureka
* NVDA 兼容版本: NVDA2021.2 及以上
* 兼容微信版本: 微信 3.9.7+
* 兼容微信版本: 微信 3.9.8+

**注: 使微信支持 NVDA 对象文本查看的思路来源于“NV宝盒”。**

Expand All @@ -13,4 +13,5 @@
4. 支持使用 NVDA 的对象文本查看浏览微信的控件信息。
5. 针对群成员昵称朗读的改进: 开启了“显示群成员昵称”则只读群成员昵称,关闭该选项则只读微信昵称。
6. 当在微信中打开包含文字的图片时会自动提取文字并读出结果。

7. 尝试为会话列表中直接回车打开的“订阅号”窗口设置焦点。
8. 改进了转发消息窗口的消息朗读顺序(昵称 → 内容 → 时间)。
2 changes: 1 addition & 1 deletion buildVars.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _(arg):
# Translators: Long description to be shown for this add-on on add-on information from add-ons manager
"addon_description": _("""Enhance the experience of using PC WeChat for NVDA users."""),
# version
"addon_version": "1.7.7",
"addon_version": "1.8.0",
# Author(s)
"addon_author": "沉浮, Cary-Rowen <[email protected]>",
# URL for the add-on documentation support
Expand Down
2 changes: 2 additions & 0 deletions flake8.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ ignore =

builtins = # inform flake8 about functions we consider built-in.
_, # translation lookup
ngettext, # translation lookup
pgettext, # translation lookup
npgettext, # translation lookup

exclude = # don't bother looking in the following subdirectories / files.
.git,
Expand Down

0 comments on commit 1e4194e

Please sign in to comment.