Debugging with python

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Debugging with python

Post by ponc »

Hi,

I'm new to development with openoffice, and I try to find my way through all the different tutorials, discreptions and so on, related to OOo, pyuno and sidebars. I find it quite hard, to get an overview.

I want to develop an extension, which is placed in the sidebar and has a designed panel. I would prefer a freely dockable panel (like the navigator), but I couldn't find examples for that, except for the task panel, which seemes to be no part of the new OpenOffice anymore.
I try to work with python/pyuno and run into different problems.

First of all I would like to know, where you usually place your code to get debugging messages?
As far as I can see, it's as follows:
  • when I write scripts and place them in the script folder, I receive debug-messages when I set the LogLevel.use = LogLevel.DEBUG in pythonscript.py in the program folder of OpenOffice. But this way I can't create uno-components. Am I correct? (I found SideBarHelperForMacros, but I couldn't figure out how it precisely works.
    viewtopic.php?f=47&t=62176)
  • when I create an .oxt extension, I can create UIElements, but get no debug. (I read somewhere, someone linked the python debugger to visual studio. I also couldn't figure that out.)
  • I can start the pythonscript via a console and connect OpenOffice to it. Like it's described here:https://wiki.openoffice.org/wiki/UNO_co ... nt_testing
    But this seams to connect only the pythonfile to OpenOffice and doesn't implement the whole extension. Besides it only worked with LibreOffice, not OpenOffice.

About logging every call of pyuno I found this:
http://www.openoffice.org/udk/python/py ... ml#logging
Can someone explain how to use this?


Does someone have a good advice about debugging?

Regards,
ponc


Edit: I changed the topic of my post
Last edited by ponc on Wed Dec 11, 2013 5:29 am, edited 2 times in total.
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Sidebar or panel with python / debugging

Post by Villeroy »

Too complicated. Let's wait for the first super geek to publish his first extension for the side bar.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Sidebar or panel with python / debugging

Post by hanya »

ponc wrote:I want to develop an extension, which is placed in the sidebar and has a designed panel. I would prefer a freely dockable panel (like the navigator), but I couldn't find examples for that, except for the task panel, which seemes to be no part of the new OpenOffice anymore.
There is th documentation for sidebar [1, 2]. And I have make an extension embedded in sidebar[3].

[1] https://wiki.openoffice.org/wiki/Sidebar
[2] https://wiki.openoffice.org/wiki/Sidebar_for_Developers
[3] https://github.com/hanya/WatchingWindow
I try to work with python/pyuno and run into different problems.
Please make an another thread for different problem.
First of all I would like to know, where you usually place your code to get debugging messages?
As far as I can see, it's as follows:
  • when I write scripts and place them in the script folder, I receive debug-messages when I set the LogLevel.use = LogLevel.DEBUG in pythonscript.py in the program folder of OpenOffice. But this way I can't create uno-components. Am I correct? (I found SideBarHelperForMacros, but I couldn't figure out how it precisely works.
    viewtopic.php?f=47&t=62176)
pythonscript.py is prepared for macros written in Python, not for UNO components written in Python. pythonloader.py is the one for UNO components. If you want to see debug output from pythonloader, you need change DEBUG variable. But it shows logs and errors happened during loading and activation of the target component.
  • I can start the pythonscript via a console and connect OpenOffice to it. Like it's described here:https://wiki.openoffice.org/wiki/UNO_co ... nt_testing
    But this seams to connect only the pythonfile to OpenOffice and doesn't implement the whole extension. Besides it only worked with LibreOffice, not OpenOffice.
Its for RPC script, not for macros.
About logging every call of pyuno I found this:
http://www.openoffice.org/udk/python/py ... ml#logging
Can someone explain how to use this?
Set environmental variable described in the section.
Does someone have a good advice about debugging?
Try remote debugger, read the documents of your debugger.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: debugging with python

Post by ponc »

@hanya:
Thanks for your reply. I changed the topic of this thread and will open a new one for the sidebar. ( I already found your extension.)
I will post a reply after I have figured it out.

ponc
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: debugging with python

Post by ponc »

hanya wrote:
pythonscript.py is prepared for macros written in Python, not for UNO components written in Python. pythonloader.py is the one for UNO components. If you want to see debug output from pythonloader, you need change DEBUG variable. But it shows logs and errors happened during loading and activation of the target component.
I changed the debug variable, but I can't find the debug output.
How do i have to start the extension to receive the debug output? 1) as a skript 2) install it 3) start from a console as mentioned above?
And where do I find the output? console or logfile?
Its for RPC script, not for macros.
So it's not for any use for me?
As far as I understand, starting the skript this way starts only the skript and not the whole extension? And what is XML-RPC used for?
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: debugging with python

Post by hanya »

ponc wrote:I changed the debug variable, but I can't find the debug output.
How do i have to start the extension to receive the debug output? 1) as a skript 2) install it 3) start from a console as mentioned above?
And where do I find the output? console or logfile?
On my Linux environment, I can see logs in standard output. On Windows environment, you can not see stdout, so you have to store the log output to the file.
Its for RPC script, not for macros.
So it's not for any use for me?
As far as I understand, starting the skript this way starts only the skript and not the whole extension? And what is XML-RPC used for?
The RPC uses original UNO-remote protocol. The RPC script is executed in the standalone Python and the office uses own Python instance inside, they are separated instance of Python. To debug Python macros as RPC script is useful for macros. But the components can not be debugged with the way.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: Debugging with python

Post by ponc »

I got the remote debugger working with aptana. That's really helpfull, also for the understanding of uno.

But I still can't get messages about errors in my python code. I found the following thread, but I didn't get it yet:
viewtopic.php?f=45&t=32809
So could you give me a hint how to get python errors?

When I'm done with this, I will bundle the information detailed in this thread. Because that's what I've missed.

Thanks a lot for your help.

ponc
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Debugging with python

Post by hanya »

When I changed to the following, I see the log in USER_PROFILE/Scripts/python/log.txt file on AOO 4.0.1:

Code: Select all

LogLevel.use = LogLevel.DEBUG
LOG_STDOUT = False
The following description is described for Apache OpenOffice 4.1.0.
For Python macros working in Apache OpenOffice 4.1, I can see log output with the following settings on Xubuntu:

Code: Select all

export PYSCRIPT_LOG_LEVEL=DEBUG
export PYSCRIPT_LOG_STDOUT=0
If PYSCRIPT_LOG_STDOUT is not "1", log output is stored in USER_PROFILE/Scripts/python/log.txt file.
 Edit: Added the description for AOO 4.0. 
Last edited by hanya on Thu Dec 12, 2013 10:06 am, edited 1 time in total.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: Debugging with python

Post by ponc »

the settings in my file pythonscript.py look like this (AOO 4.0.1):

Code: Select all

# Configuration ----------------------------------------------------
#LogLevel.use = LogLevel.None                # production level
#LogLevel.use = LogLevel.ERROR               # for script developers
LogLevel.use = LogLevel.DEBUG               # for script framework developers
LOG_STDOUT = False                           # True, writes to stdout (difficult on windows)
                                            # False, writes to user/Scripts/python/log.txt
ENABLE_EDIT_DIALOG=False                    # offers a minimal editor for editing.
#-------------------------------------------------------------------
But I can't find a log.txt file.

Is this the full path, which is meant?
C:\Users\ponc\AppData\Roaming\OpenOffice\4\user\Scripts\python

or should it be the one for the macros?:
C:\Program Files (x86)\OpenOffice 4\share\Scripts\python

And in which file do I have to put "export PYSCRIPT_LOG_LEVEL=DEBUG"? Also pythonscript.py?
Is AOO 4.1 the same as AOO 4.0.1?
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Debugging with python

Post by hanya »

Is it broken on Windows environment?
And in which file do I have to put "export PYSCRIPT_LOG_LEVEL=DEBUG"? Also pythonscript.py?
Is AOO 4.1 the same as AOO 4.0.1?
It can be used since AOO 4.1.0.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: Debugging with python

Post by ponc »

Do you mean LibreOffice 4.1 ? Or a dev-Version?

Anyway, I was a bit blind. print works in the console of aptana and I can use try/except with traceback.print_exc().
That's not a perfect solution, but better then nothing.

Another thing I tried was to pipe stderr at commandline: swriter.exe 2>&1 | find ' '
and set LOG_STDOUT = True
But I got only debug messages from AOO while starting, not from my extension
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Debugging with python

Post by hanya »

Do you mean LibreOffice 4.1 ? Or a dev-Version?
For AOO 4.1.0-dev.

I could see debug log for pythonscript with modifying the log status in the file on AOO 4.0.0 working in Windows XP.
And also I could see the log from PyUNO bridge with the following environmental variables:

Code: Select all

> set PYUNO_LOGLEVEL=CALL
> set PYUNO_LOGTARGET=file:///G:/usr/local/OO/AOO400/user/Scripts/python/loaderlog.txt
 Edit: Remove unused G:\ 
Last edited by hanya on Fri Dec 13, 2013 10:53 am, edited 1 time in total.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: Debugging with python

Post by ponc »

Where and how do I have to set these variables? In pythonloader.py or pythonscript.py?
And "set PYUNO_LOGTARGET=file:/..." has to be done from console?
 Edit: Ok, it's done with the bash. 
Please give some more information with your advises. I'm not an expert and just started with AOO.
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Debugging with python

Post by hanya »

On Windows environment, start cmd.exe and input the above two lines staring with "set ". And then start your office with "soffice.exe" command from your installed location.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: Debugging with python

Post by ponc »

"set PYUNO_LOGLEVEL=CALL": That works! Great! Thanks a lot!

Is it possible to pipe the output to the console back again? (I tried, but didn't succeed)

In Aptana/Eclipse print works and is shown in the internal console. Is this message sent by the interpreter? Why is there no message, when an error occures?
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Debugging with python

Post by hanya »

It seems no way to get output from the office on Windows environment.
In Aptana/Eclipse print works and is shown in the internal console. Is this message sent by the interpreter?
Please read PyDev code.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: Debugging with python

Post by ponc »

I think I'm close to a solution. I can get error messages in a console, when I do the following:

open console. navigate to the AOO program folder (C:\Program Files (x86)\OpenOffice 4\program)
> set PYUNO_LOGLEVEL=CALL
> python
and from python:
>>> import os
>>> os.system('swriter.exe 2>&1 | find " "')

Now I get the log from the PyUNO bridge. But the pipe isn't read completely. So I have to start the script, which is done by the extension, at least twice, to see the end of the pipe.
Can I force python to read the whole pipe? Then it would be perfect.
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: Debugging with python

Post by ponc »

To me it seemes, that PyDev doesn't pipe all output to stdout. Or maybe the buffer is too small on windows. I couldn't figure it out.

Anyway, I decided to do it completely in eclipse and write a second script "start_debug", which capsules my whole script in a try except block. When an error occures, except shows the traceback. The Extension starts "start_debug" , which will be removed after debugging.
That works fine.

Here is my code for the debug script:

Code: Select all

# -*- coding: utf-8 -*-
import uno
from unohelper import Base, ImplementationHelper
from com.sun.star.task import XJobExecutor
import traceback
import sys
 
 
def pydevBrk():  
    # adjust your path 
    sys.path.append(r'C:\Users\ponc\Desktop\Programme\eclipse\plugins\org.python.pydev_3.1.0.201312121632\pysrc')  
    from pydevd import settrace
    settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True) 
    

class start_debug( Base, XJobExecutor):
  
    def __init__(self, ctx):
        self.ctx = ctx
        
        # append extension directory to pythonpath 
        path_to_current = __file__.decode("utf-8")
        pyPath = path_to_current.split('start_debug.py')
        sys.path.append(pyPath[0])
        
    def trigger(self, args):
        pydevBrk()
        print('debug')
        
        try:            
            import MyModule
            # set context in MyClass.__init__
            mC = MyModule.MyClass(self.ctx)
            mC.trigger(None)
        except:
            traceback.print_exc()

    
g_ImplementationHelper = ImplementationHelper()
g_ImplementationHelper.addImplementation( \
        start_debug,                                # UNO object class
        "openoffice.ponc.debug",                    # Implementation name
        ("com.sun.star.task.Job",),)                # List of implemented services

Last edited by ponc on Mon Dec 16, 2013 7:54 pm, edited 1 time in total.
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
User avatar
RoryOF
Moderator
Posts: 34612
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Debugging with python

Post by RoryOF »

I know very little about Python, but a PyDev FAQ says
If you're on windows, press Ctrl+Break to get a list of all the running threads (which should be attached to the bug). Note that you may have to increase the buffer of the console to get all info (so, in the console open the properties menu -- Alt+Space -- select the 'properties' and in 'screen buffer size: height' raise it to something as 9999 -- and only then press Ctrl+Break)
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
ponc
Posts: 86
Joined: Mon Dec 09, 2013 7:34 pm

Re: Debugging with python

Post by ponc »

Thanks for your reply. But I think, I will stay with my solution as it shows only the errors and not all the output from pydev. And I don't need a second console.

ponc
OO 4.0.1 , LO 4.4.3.2 on win7 // LO 4.4.3.2 on Ubuntu 14.04
https://github.com/XRoemer/Organon
Post Reply