How to tell a Gramps object is being edited?

I am working on an “enhancement” of URL link specification in Notes. Schematically, I break a URL into fragments which are stored as auxiliary data (Notes) of repository, source and citation records. This is an attempt to manage “easily” “internet rot” where even permanent links change over time.

When the mouse hovers over such a fragment, a tooltip pops up to report the closed form of the URL. This is done by querying the database. But, if a fragment is currently edited in a Note window, there is a high probability of a discrepancy between what is edited and what is committed to the DB. In this case, I want to replace the closed form in the tooltip by a warning.

My manipulations are done at very low level because they must work both in GUI and in CLI (report generation; if something goes wrong, the link is replaced by a “message” easily identifiable in the report).

URL closure is computed exclusively by querying the DB. I have then no idea whether the DB record is currently edited or not.

Looking at the code for GUI warning “the object cannot be edited at this time because it is being edited. You need to close it” (see e.g. eventembedlist.py), this warning is issued after raising WindowActiveError. This error itself is emitted in __init__ of ManagedWindow attributes of uistate which an instantiation parameter.

During my DB query, I am not associated at all with any GUI element (window). So, how can I interfere with the ManagedWindow machinery to tell if the Note I am scanning for link fragments is opened in some window?

Schematically, I need to guess a possible “id” (window_key) for my Note and ask if it is known. How can I do this in the simplest possible way?

For primary object editors, the window ID will just be the handle. The GrampsWindowManager instance is available in the DisplayState so you can use code like:

self.uistate.gwm.get_item_from_id(note_handle)

It will return None if the note is not being edited.

Do you mean as can use note_handle just after retrieving it from the DB?

In this case, what is self? My code snippet is a general “utility” not a method of some class of object. I need to “attach” self to some relevant object.

Your utility will have to obtain the DisplayState from somewhere if it needs to know what the GUI is doing (editing a note in this case).