How to make title-change call work in webkit2gtk? - python-3.x

I am trying to port an old GTK webkit code written in python2 to webkit2 and python3 (Ubuntu has removed old webkit v1). The js code changes the title of the html, and python detected it to close it. However, it is not working in webkit2.
In the old code, there is the line
self.webview.connect('title-changed', title_changed) #title_changed is a python function, which will do something
which shows TypeError: <WebKit2.WebView object at 0x7f14b687ecd0 (WebKitWebView at 0x557297cdb7c0)>: unknown signal name: title-changed.
How to port this code to webkit2?
According to https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html there is webkit_web_view_get_title (), but is there any way to detect change of title?

Since title is a property, it automatically comes with a notify signal you can connect to. Use notify::title.
Use the following code, for example
self.webview.connect('notify::title', title_changed)

Related

pywinuto - click_input() function clicks on random node of tree view

I'm trying to automate the mmc (snap in console) type desktop app. Where I need to expand the tree.
Try 1 -
But when I do use expand() function it generates error popup which says that "the child nodes does not exists". After this script simply stops with no error message on console unless ok button is clicked on error popup. This I suspect because when tree node expanded it triggers some event which brings child nodes under it and somehow pywinauto is not getting Childs of this node. Please note that this error popup does not come up when the step is done manually.
Try 2 -
When I tried with select() then too same behavior observed as above. It generated same error popup.
Try 3 -
I tried click() and click_input() functions and it clicks on random tree node but not on the tree node on which it is called.
The all above trials are of my application which is not public.
For reproducing this issue I have tried it on common application available on windows OS. And we can see that the 3rd is still reproducible.
Reference code -
import time
from pywinauto.application import Application
from pywinauto import Desktop
app = Application().start(r'cmd.exe /c "C:\Windows\system32\perfmon.msc"', wait_for_idle=False)
app = Application(backend="win32").connect(title='Performance Monitor', timeout=10)
main_wind = app.window(best_match='MMCMainFrame', top_level_only=False)
console_wind = main_wind.child_window(best_match="MDIClients").child_window(best_match='MMCChildFrm').child_window(class_name="MMCViewWindow")
tree = console_wind.TreeView # print_control_identifiers()
children = tree.get_item(["Performance","Data Collector Sets"]).expand().get_child("System").expand().get_child('System Diagnostics')
print(children.text())
# below line will select the System Diagnostics
children.select()
time.sleep(4)
# Below line should click on System Diagnostics but it does NOT and same happens for click() function
children.click_input()
Any help will be really helpful in knowing why this click() and click_input() clicks on random tree node and Why expand() and select() method generates the non functional error popup?
Please mention if there is concrete workaround to this.
versions -
Windows OS 10, build 20H2
Python 3.10.4
comtypes 1.1.11
pip 22.0.4
pywin32 303
pywinauto 0.6.8
setuptools 61.2.0
six 1.16.0
wheel 0.37.1
TIA..
I have referenced some stack overflow and github issue tracker as for this problem as below but nothing works.
Treeview problem
select() for TreeView items (and similar) leads error if this selection calls other dialogs etc
Python: Click by coordinate inside a window
This was all because of incorrect backend API used for snap in control application. Unfortunately my POC I did on win32 API where it worked because I was doing some initial steps manually hence the problem I was facing(explained in try1) was not there and everything was working perfectly fine.
As soon as I switched the backend to UIA it gave me different identifiers for the controls that I used previously. Now I used this identifiers and started using UIA documentation and everything started working smoothly.
Also in my investigation there is no proper way to identify the backend API for the desktop application unless you try both and figure out which works for you.
Suggestion to readers - If you are using win32 API as backend there are different api methods available for that in the documentation. and for UIA backend different. read documentation carefully.
You can try the uispy tool and find that perfmon.msc can be automated with uia

Golem Task respons back with runtime error 2, can't determine the cause

Repo for all code I've been using is updated here . When I run the requestor script it exits with a runtime error 2 (File not found). I am not sure how to further debug this or fix it. So far I've converted my code over to a python slim docker image to better mirror the example. It also works for me when I spin up a docker image that typing and running "/golem/work/imageclassifier.py --trainmodel" works from root. I switched all my code to use absolute paths. I also did make sure the shebang (#!) uses linux end of file characters rather than windows before which was giving me errors. Fixed a bug where my script returns error code 2 when called with no args to now pass.
clf.fit(trainDataGlobal, trainLabelsGlobal)
pkl_file = "classifier.pkl"
with open(pkl_file, 'wb') as file:
pickle.dump(clf, file)
is the only piece I could think of that causes the issue, but as far as I can tell this is the proper way to pickle something in python. Requestor script is also heavily based on the simple service example and I tried to mirror my design to that. I just need help in getting more information while debugging, or guidance on how to move forward from here

What does "Gdk-WARNING **: gdk-frame-clock: layout continuously requested, giving up after 4 tries" mean?

When I run the gtk3 python code using pygobject in Ubuntu 18.04, the code runs without any warnings. But when I build it on Ubuntu 16.04 for snap, I am getting these warnings:
Gdk-WARNING **: gdk-frame-clock: layout continuously requested, giving up after 4 tries
There is no part of the code that tries to refresh the screen even twice within a second(unless there is some check_resize events). I am unable to understand what is causing this error. I don't have a Ubuntu 16.04 with me. so I can't debug either.
Steps to reproduce
Install the edge release from the snap.
sudo snap install --edge halo-weather
and run it.
halo-weather
It's an opensource project of a simple weather application named Halo. Here is the entry point python script.
Can somebody give me some directions?
The error generally means you are directly trying to modify the UI out of sync with GTK thread.
So we need to make sure that all UI updating functions are not directly called and instead queued to be called by Gtk using any of the following:
GLib.idle_add(callback, ...)
GLib.timeout_add(interval, callback, ...) # X milliseconds delay
GLib.timeout_add_seconds(interval, callback, ...) # X seconds delay
Hope that will help someone.

node-serialport 6.x.x doesn't receive data or errors on Windows (but does on Mac)

I am using the following library to connect via USB to an external device:
https://github.com/node-serialport/node-serialport
I set up the following sample project to narrow down where the issue might be:
https://github.com/nyoung697/node-serial-test
This code works on my Mac. The device I am connecting to accepts an ascii command and the carriage return to terminate that command.
In the example project, I am passing in 'V\r' and am expecting the version of the device to be returned. When I run this code on my Mac, I get the expected response.
However, when I run it on my Windows 10 machine, I do not get any response at all. Neither the port.on('data') or port.on('error') events are hit.
I tested downgrading the serialport library to version 4.0.7 and it does work.
Does anyone have any ideas what might have changed between 4 => 6?
P.S. I edited this question, as I originally thought it had something to do with Electron, however after doing this simple test with only node.js, I believe I have narrowed it down to this version of the library on Windows.
Okay... I just figured this out.
All I needed to do was add the property 'rtscts' to the openOptions object I pass in to the constructor and set it to true.
const SERIAL_PORT_SETTINGS = {
baudRate: 115200,
rtscts: true
};
let port = new SerialPort(devicePath, SERIAL_PORT_SETTINGS);
I figured this out by digging around in the release notes. I noticed that from v5.x.x to 6.0.0 the breaking changes included making this value default to false.
windows: We previously hard coded to have RTS on for windows at all times it now default to off.

How can I write unit tests against code that uses QDesktopServices openUrl?

I'm working on a python (3.5) program that use a PyQt5 GUI. In the GUI, I need to add some help links to the documentation on a website. I manage to make it work with:
QDesktopServices.openUrl(QUrl("my_url"))
It works fine but I want to be sure that it will always be the case.
A quick and dirty unittest is to call the function and simply notice that there is no error. I would like to make a test that will check that the correct website page did show up. What should I use?
Checking for an error is not going to work at all, because Qt itself never raises errors (of course, Python or PyQt might do, but for completely unrelated reasons). The best you can do is check the return value of openUrl, which will simply return True or False depending on whether it was "successful". But note the following from the Qt docs for openUrl:
Warning: A return value of true indicates that the application has
successfully requested the operating system to open the URL in an
external application. The external application may still fail to
launch or fail to open the requested URL. This result will not be
reported back to the application.
If you want more control, I suggest you use Python's webbrowser module instead. This would, for example, allow you to register your own mock-browser class for the purposes of testing. The webbrowser module is written in pure Python and the code is quite straightforward.

Resources