Search code examples
pythonlibreofficelibreoffice-writer

libreoffice python threading sleep


I want to implement an autosave feature for libreoffice writer. I'm using a python macro.

This will periodically save the desktop view of the document back to the file on disk and signal to the desktop that the document is not modified.

This requirement is not satisfied by the indisputably excellent autorecovery and backup features already offered by this great application suite.

Here is my first naive failed attempt :

def autosave():
    from time import sleep
    doc = XSCRIPTCONTEXT.getDocument()
    args=()
    while True :
        if doc.isModified() :
            doc.storeToURL(doc.getURL(),args)
            doc.setModified(False)
        sleep(60)
        

This works in that the file is updated and the desktop informed. The problem is that the sleep locks the UI.

I have tried putting the timer into a thread that runs in background and then fires an interrupt -- something like this :

def autosave_timer() :
    while True :
        autosave()
        sleep(60)

def autosave():
    from time import sleep
    doc = XSCRIPTCONTEXT.getDocument()
    args=()
    if doc.isModified() :
            doc.storeToURL(doc.getURL(),args)
            doc.setModified(False)

def run_autosave() :
    thread=Thread(target=autosave_timer)        
    thread.start()
    

The problem with this is that it runs only when run_autosave is explicitly called whereas to my understanding (flawed obviously) the thread should run continously.

Is there an obvious small error or is there a better approach?


Solution

  • I would argue that the problem must lie elsewhere.
    If you run lowriter from the command line and then start the macro manually, in this case I called it logger.py and start the function run_autosave, it happily loops, saving, not saving or complaining, until LibreOffice is closed.

    Clearly, it's a work in progress and needs to be refined but the basics work.

    from threading import Thread
    from time import sleep
    
    def autosave_timer() :
        cnt = 0
        while True :
            cnt += 1
            autosave(cnt)
            sleep(5)
    
    def autosave(cnt):
        doc = XSCRIPTCONTEXT.getDocument()
        url = doc.getURL()
        if not url:
            print("Document is unnamed - Not Saving "+str(cnt))
            return True
        args=()
        if doc.isModified():
            try:
                doc.storeToURL(doc.getURL(),args)
                doc.setModified(False)
                print("Document "+url+" saved "+str(cnt))
            except Exception as e:
                print(str(e))
        else:
            print(url+" Document unchanged "+str(cnt))
    
    def run_autosave():
        thread=Thread(target=autosave_timer)        
        thread.start()
    

    Runs as:

    $ lowriter
    Document is unnamed - Not Saving 1
    Document is unnamed - Not Saving 2
    Document is unnamed - Not Saving 3
    Document is unnamed - Not Saving 4
    Document is unnamed - Not Saving 5
    Document is unnamed - Not Saving 6
    file:///home/rolf/Documents/aaa.odt Document unchanged 7
    file:///home/rolf/Documents/aaa.odt Document unchanged 8
    file:///home/rolf/Documents/aaa.odt Document unchanged 9
    Document file:///home/rolf/Documents/aaa.odt saved 10
    file:///home/rolf/Documents/aaa.odt Document unchanged 11
    file:///home/rolf/Documents/aaa.odt Document unchanged 12