OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Managing pythonpath for scripts

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
ishanthilina
General User
General User


Joined: 17 Mar 2012
Posts: 46

PostPosted: Sun Mar 18, 2012 5:17 am    Post subject: Managing pythonpath for scripts Reply with quote

Hi,

According to http://www.openoffice.org/udk/python/python-bridge.html#multiple_source_files it is possible to import python files in the pythonpath folder to the script. I wrote the following simple script to check the above task.

In the root directory for the extension ,

Code:
import uno
import unohelper

def run():
   
    import tester
    print tester.tt().te()
   

# pythonloader looks for a static g_ImplementationHelper variable
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation( \
   run,"tester.tt",
        ("com.sun.star.sheet.AddIn",),)


In the pythonpath folder,

Code:
class tt():
    """
    """
   
    def __init__(self):
        """
        """
   pass
    def te():
   return 'ss'




But I get the following error when try to validate the plugin using the unopkg tool.

Code:
pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/lu90zdwi.tmp_/test.zip/test/pythonpath/__init__.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/lu90zdwi.tmp_/test.zip/test/pythonpath/__init__.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/lu90zdwi.tmp_/test.zip/test/pythonpath/pythonpath.zip
 WARNING: (com.sun.star.uno.RuntimeException) { { Message = "<type 'exceptions.AttributeError'>: 'module' object has no attribute 'writeRegistryInfo', traceback follows\X000a  /usr/lib/libreoffice/basis-link/program/pythonloader.py:138 in function writeRegistryInfo() [return mod.writeRegistryInfo( self.ctx.ServiceManager, regKey )]\X000a", Context = (com.sun.star.uno.XInterface) @0 } }
pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/lu90zdwi.tmp_/test.zip/test/pythonpath/tester.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/lu90zdwi.tmp_/test.zip/test/pythonpath/tester.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/lu90zdwi.tmp_/test.zip/test/pythonpath/pythonpath.zip
 WARNING: (com.sun.star.uno.RuntimeException) { { Message = "<type 'exceptions.AttributeError'>: 'module' object has no attribute 'writeRegistryInfo', traceback follows\X000a  /usr/lib/libreoffice/basis-link/program/pythonloader.py:138 in function writeRegistryInfo() [return mod.writeRegistryInfo( self.ctx.ServiceManager, regKey )]\X000a", Context = (com.sun.star.uno.XInterface) @0 } }


Can someone point out a way to properly do this..? Confused
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Sun Mar 18, 2012 10:32 am    Post subject: Reply with quote

Hi,
It seems you have wrong entries in the META-INF/manifest.xml.
You have to specify the file which has g_ImplementationHelper variable or writeRegistryInfo method to register in the entry of the manifest.

And you do not need to put __init__.py into pythonpath directory. Create another directory in it as module and create __init__.py into it, to reduce name conflict.
Back to top
View user's profile Send private message
ishanthilina
General User
General User


Joined: 17 Mar 2012
Posts: 46

PostPosted: Mon Mar 19, 2012 4:05 am    Post subject: Reply with quote

hanya wrote:
Hi,
It seems you have wrong entries in the META-INF/manifest.xml.
You have to specify the file which has g_ImplementationHelper variable or writeRegistryInfo method to register in the entry of the manifest.

And you do not need to put __init__.py into pythonpath directory. Create another directory in it as module and create __init__.py into it, to reduce name conflict.




Hi hanya,

Thank you very much for the reply. I'm a newbie to python scripting under pyUno. I'm not sure what I did was correct or not. I deleted the __init__.py file as you suggested.

Then I added a META-INF/manifest.xml file with the following content.

Code:
<manifest:manifest>
<manifest:file-entry manifest:media-type="application/vnd.sun.star.uno-component;type=Python"
                    manifest:full-path="pythonpath/tester.py"/>
<manifest:file-entry manifest:media-type="application/vnd.sun.star.uno-component;type=Python"
                    manifest:full-path="Import.py"/>
</manifest:manifest>


Also I changed the Import.py files following part,

Code:

g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation( \
   run,"tester.tt",
        ("vnd.sun.star.uno-component",),)


as follows.

Code:
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation( \
   run,"tester.tt",
       ("vnd.sun.star.uno-component",),)



But I still get the above mentioned error. Confused. Can you point me to anything more that I should try..?

Also, if you have time, can you please add a sample code which demonstrates multiple python source code importing in pyUno (Hope I'm not being a burden to you Sad )
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Mon Mar 19, 2012 8:19 am    Post subject: Reply with quote

All right, I have some extensions written in Python but small one is good to understand what should be do. I made the example extension in the following thread of the another forum, you can get attached OXT package and unpack to read its contents.
http://user.services.openoffice.org/en/forum/viewtopic.php?f=6&t=48986#p224826

In the attached file, META-INF/manifest.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest>
   <manifest:file-entry manifest:full-path="registration.py"
    manifest:media-type="application/vnd.sun.star.uno-component;type=Python"/>
</manifest:manifest>


registration.py has following contents:
Code:
from pythoncommonscripts import IMPLE_NAME, SERVICE_NAMES

def create(ctx, *args):
    try:
        import pythoncommonscripts.provider
        return pythoncommonscripts.provider.create(ctx, *args)
    except Exception, e:
        print(e)
        import traceback
        traceback.print_exc()

import unohelper
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
    create, IMPLE_NAME, SERVICE_NAMES,)

So, you have to specify the file which has g_ImplementationHelper variable to register in the manifest.xml file.
pythoncommonscripts module is there under pythonpath directory and I can import its contents from register.py file. You have to put pythonpath directory (or pythonpath.zip) in the same directory which includes registration script.
We can call addImplementation method some times to register some components in a registaration file.
Back to top
View user's profile Send private message
ishanthilina
General User
General User


Joined: 17 Mar 2012
Posts: 46

PostPosted: Mon Mar 19, 2012 10:48 am    Post subject: Weird Reply with quote

Hi,

I really appreciate the support you are providing Smile . I installed the extension you gave me and it got installed fine. I studied its source code and uninstalled it and added a new python source code file to the directory pythoncommonscripts. Then I created a package and re installed the package.

But it didn't work as I expected. The package got installed, but when I validated the installed extension, it gave me an error.

Then I removed that modified extension and again try to install the extension you provided. Then a error message was displayed.

Code:
 ERROR: Error binding package: vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luc9i344.tmp_/PythonCommonScripts-1.0.0.zip
       Cause: an error occurred during file opening

unopkg failed.


Then I renamed the zip file to an other name and installed it. Following is the output.

Code:
unopkg add ~/ee.zip
pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/pythonpath.zip
pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/__init__.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/__init__.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/pythonpath.zip
pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/registration.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/registration.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath.zip
adding /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath to sys.path
pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/registration.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/registration.py
pythonloader.Loader.activate
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/registration.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/registration.py


And I ran unopkg list t see the details. It's output is as follows.

Code:
 All deployed user extensions:

Identifier: org.openoffice.legacy.ee.zip
  URL: vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip
  is registered: unknown
  Media-Type: application/vnd.sun.star.legacy-package-bundle
  Description:
  bundled Packages: {
      URL: vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
      is registered: no
      Media-Type: application/vnd.sun.star.uno-component;type=Python
      Description:

      URL: vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/__init__.py
      is registered: no
      Media-Type: application/vnd.sun.star.uno-component;type=Python
      Description:

      URL: vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/registration.py
      is registered: yes
      Media-Type: application/vnd.sun.star.uno-component;type=Python
      Description:

  }
 


When I try to validate the extension, an error message is displayed,

Code:
 pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/pythonpath.zip
 WARNING: (com.sun.star.uno.RuntimeException) { { Message = "Couldn't load file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py for reason No module named pythonscript", Context = (com.sun.star.uno.XInterface) @0 } }
pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/__init__.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/__init__.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/pythonpath.zip
 WARNING: (com.sun.star.uno.RuntimeException) { { Message = "<type 'exceptions.AttributeError'>: 'module' object has no attribute 'writeRegistryInfo', traceback follows\X000a  /usr/lib/libreoffice/basis-link/program/pythonloader.py:138 in function writeRegistryInfo() [return mod.writeRegistryInfo( self.ctx.ServiceManager, regKey )]\X000a", Context = (com.sun.star.uno.XInterface) @0 } }
 


I'm really confused on what is happening. I know that I'm being a burden now Crying or Very sad But please help me if you can
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Mon Mar 19, 2012 10:58 am    Post subject: Reply with quote

Quote:
Code:
 pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/pythonpath.zip
 WARNING: (com.sun.star.uno.RuntimeException) { { Message = "Couldn't load file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py for reason No module named pythonscript", Context = (com.sun.star.uno.XInterface) @0 } }
On LibreOffice, pythonscript.py is moved into its own extension package and it can not be found until python script (I mean python macro) is loaded. Remove original provider.py.

Quote:
Code:
 All deployed user extensions:

Identifier: org.openoffice.legacy.ee.zip
I suppose to use "oxt" as file extension for extension package. Recent office recognize it and others would be categorized as legacy. This is not a problem in this case.
Back to top
View user's profile Send private message
ishanthilina
General User
General User


Joined: 17 Mar 2012
Posts: 46

PostPosted: Mon Mar 19, 2012 11:17 am    Post subject: Reply with quote

Thank you for the real quick reply Hanya Smile.

hanya wrote:
Quote:
Code:
 pythonloader.Loader ctor
pythonloader.Loader.writeRegistryInfo
pythonloader: interpreting url vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
pythonloader: after expansion file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py
checking for existence of /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/pythonpath.zip
 WARNING: (com.sun.star.uno.RuntimeException) { { Message = "Couldn't load file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py for reason No module named pythonscript", Context = (com.sun.star.uno.XInterface) @0 } }
On LibreOffice, pythonscript.py is moved into its own extension package and it can not be found until python script (I mean python macro) is loaded. Remove original provider.py.



Are you asking me to delete the original provider.py file created by the initial extension installation...? (Which is located in folder in /home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/ ). I have already deleted that. I got the error even after deleting them. Is there any other copy that I should delete...? Confused

Quote:



Code:
 All deployed user extensions:

Identifier: org.openoffice.legacy.ee.zip
I suppose to use "oxt" as file extension for extension package. Recent office recognize it and others would be categorized as legacy. This is not a problem in this case.


Yes, I tried it using the oxt extension, but it didn't solve the problem.

Thank you again for your help. It is very much appreciated.
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Tue Mar 20, 2012 12:24 am    Post subject: Reply with quote

Quote:
I have already deleted that. I got the error even after deleting them. Is there any other copy that I should delete...
Hmm, why do you still have such kind of error message:
"Couldn't load file:///home/ishan/.libreoffice/3/user/uno_packages/cache/uno_packages/luca5gpn.tmp_/ee.zip/ee/pythonpath/pythoncommonscripts/provider.py for reason No module named pythonscript

This means, maybe your extension registry has older information. Have you try to modify enabled extension? If your extension working with new user's profile, older registry of extensions has problem. Otherwise, your extension still have something wrong in pythonpath directory.

Recent extension mechanism is robuster than older one but still it has problem with illegal handling. I always modifying enabled extension to develop but I do not modify any registered files (described in manifest.xml) until it is disabled.

Recent LibreOffice also has an example extension written in Python in sdk examples, if you want to see other simple example:
http://api.libreoffice.org/examples/python/toolpanel/
Back to top
View user's profile Send private message
ishanthilina
General User
General User


Joined: 17 Mar 2012
Posts: 46

PostPosted: Tue Mar 20, 2012 6:45 pm    Post subject: Reply with quote

Hi,

Is there any interface that the classes in pythonpath should implement...?

Thanks for the help you are providing Smile.
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Tue Mar 20, 2012 9:58 pm    Post subject: Reply with quote

Hi,
Code:
Is there any interface that the classes in pythonpath should implement...?
You can put something you want in pythonpath dir. But if you want to pass an instance of Python class to the office, it have to implement http://api.libreoffice.org/docs/common/ref/com/sun/star/lang/XTypeProvider.html interface. But there is helper class for it, inherit unohelper.Base class to make your own UNO component and required interfaces written in Python.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group