19f22d7c2SAndrew Rist# ************************************************************* 29f22d7c2SAndrew Rist# 39f22d7c2SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 49f22d7c2SAndrew Rist# or more contributor license agreements. See the NOTICE file 59f22d7c2SAndrew Rist# distributed with this work for additional information 69f22d7c2SAndrew Rist# regarding copyright ownership. The ASF licenses this file 79f22d7c2SAndrew Rist# to you under the Apache License, Version 2.0 (the 89f22d7c2SAndrew Rist# "License"); you may not use this file except in compliance 99f22d7c2SAndrew Rist# with the License. You may obtain a copy of the License at 109f22d7c2SAndrew Rist# 119f22d7c2SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 129f22d7c2SAndrew Rist# 139f22d7c2SAndrew Rist# Unless required by applicable law or agreed to in writing, 149f22d7c2SAndrew Rist# software distributed under the License is distributed on an 159f22d7c2SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 169f22d7c2SAndrew Rist# KIND, either express or implied. See the License for the 179f22d7c2SAndrew Rist# specific language governing permissions and limitations 189f22d7c2SAndrew Rist# under the License. 199f22d7c2SAndrew Rist# 209f22d7c2SAndrew Rist# ************************************************************* 219f22d7c2SAndrew Rist 22cdf0e10cSrcweir# XScript implementation for python 23cdf0e10cSrcweirimport uno 24cdf0e10cSrcweirimport unohelper 25cdf0e10cSrcweirimport sys 26cdf0e10cSrcweirimport os 27cdf0e10cSrcweirimport imp 28cdf0e10cSrcweirimport time 29cdf0e10cSrcweirimport compiler 30cdf0e10cSrcweir 31cdf0e10cSrcweirclass LogLevel: 32cdf0e10cSrcweir NONE = 0 33cdf0e10cSrcweir ERROR = 1 34cdf0e10cSrcweir DEBUG = 2 35cdf0e10cSrcweir 36cdf0e10cSrcweir# Configuration ---------------------------------------------------- 37cdf0e10cSrcweirLogLevel.use = LogLevel.NONE # production level 38cdf0e10cSrcweir#LogLevel.use = LogLevel.ERROR # for script developers 39cdf0e10cSrcweir#LogLevel.use = LogLevel.DEBUG # for script framework developers 40cdf0e10cSrcweirLOG_STDOUT = True # True, writes to stdout (difficult on windows) 41cdf0e10cSrcweir # False, writes to user/Scripts/python/log.txt 42cdf0e10cSrcweirENABLE_EDIT_DIALOG=False # offers a minimal editor for editing. 43cdf0e10cSrcweir#------------------------------------------------------------------- 44cdf0e10cSrcweir 45cdf0e10cSrcweirdef encfile(uni): 46cdf0e10cSrcweir return uni.encode( sys.getfilesystemencoding()) 47cdf0e10cSrcweir 48cdf0e10cSrcweirdef lastException2String(): 49cdf0e10cSrcweir (excType,excInstance,excTraceback) = sys.exc_info() 50cdf0e10cSrcweir ret = str(excType) + ": "+str(excInstance) + "\n" + \ 51cdf0e10cSrcweir uno._uno_extract_printable_stacktrace( excTraceback ) 52cdf0e10cSrcweir return ret 53cdf0e10cSrcweir 54cdf0e10cSrcweirdef logLevel2String( level ): 55cdf0e10cSrcweir ret = " NONE" 56cdf0e10cSrcweir if level == LogLevel.ERROR: 57cdf0e10cSrcweir ret = "ERROR" 58cdf0e10cSrcweir elif level >= LogLevel.DEBUG: 59cdf0e10cSrcweir ret = "DEBUG" 60cdf0e10cSrcweir return ret 61cdf0e10cSrcweir 62cdf0e10cSrcweirdef getLogTarget(): 63cdf0e10cSrcweir ret = sys.stdout 64cdf0e10cSrcweir if not LOG_STDOUT: 65cdf0e10cSrcweir try: 66cdf0e10cSrcweir pathSubst = uno.getComponentContext().ServiceManager.createInstance( 67cdf0e10cSrcweir "com.sun.star.util.PathSubstitution" ) 68cdf0e10cSrcweir userInstallation = pathSubst.getSubstituteVariableValue( "user" ) 69cdf0e10cSrcweir if len( userInstallation ) > 0: 70cdf0e10cSrcweir systemPath = uno.fileUrlToSystemPath( userInstallation + "/Scripts/python/log.txt" ) 71cdf0e10cSrcweir ret = file( systemPath , "a" ) 72cdf0e10cSrcweir except Exception,e: 73cdf0e10cSrcweir print "Exception during creation of pythonscript logfile: "+ lastException2String() + "\n, delagating log to stdout\n" 74cdf0e10cSrcweir return ret 75cdf0e10cSrcweir 76cdf0e10cSrcweirclass Logger(LogLevel): 77cdf0e10cSrcweir def __init__(self , target ): 78cdf0e10cSrcweir self.target = target 79cdf0e10cSrcweir 80cdf0e10cSrcweir def isDebugLevel( self ): 81cdf0e10cSrcweir return self.use >= self.DEBUG 82cdf0e10cSrcweir 83cdf0e10cSrcweir def debug( self, msg ): 84cdf0e10cSrcweir if self.isDebugLevel(): 85cdf0e10cSrcweir self.log( self.DEBUG, msg ) 86cdf0e10cSrcweir 87cdf0e10cSrcweir def isErrorLevel( self ): 88cdf0e10cSrcweir return self.use >= self.ERROR 89cdf0e10cSrcweir 90cdf0e10cSrcweir def error( self, msg ): 91cdf0e10cSrcweir if self.isErrorLevel(): 92cdf0e10cSrcweir self.log( self.ERROR, msg ) 93cdf0e10cSrcweir 94cdf0e10cSrcweir def log( self, level, msg ): 95cdf0e10cSrcweir if self.use >= level: 96cdf0e10cSrcweir try: 97cdf0e10cSrcweir self.target.write( 98cdf0e10cSrcweir time.asctime() + 99cdf0e10cSrcweir " [" + 100cdf0e10cSrcweir logLevel2String( level ) + 101cdf0e10cSrcweir "] " + 102cdf0e10cSrcweir encfile(msg) + 103cdf0e10cSrcweir "\n" ) 104cdf0e10cSrcweir self.target.flush() 105cdf0e10cSrcweir except Exception,e: 106cdf0e10cSrcweir print "Error during writing to stdout: " +lastException2String() + "\n" 107cdf0e10cSrcweir 108cdf0e10cSrcweirlog = Logger( getLogTarget() ) 109cdf0e10cSrcweir 110cdf0e10cSrcweirlog.debug( "pythonscript loading" ) 111cdf0e10cSrcweir 112cdf0e10cSrcweir#from com.sun.star.lang import typeOfXServiceInfo, typeOfXTypeProvider 113cdf0e10cSrcweirfrom com.sun.star.uno import RuntimeException 114cdf0e10cSrcweirfrom com.sun.star.lang import XServiceInfo 115cdf0e10cSrcweirfrom com.sun.star.io import IOException 116*61c9e2f8SAriel Constenla-Hailefrom com.sun.star.ucb import CommandAbortedException, XCommandEnvironment, XProgressHandler, Command 117cdf0e10cSrcweirfrom com.sun.star.task import XInteractionHandler 118*61c9e2f8SAriel Constenla-Hailefrom com.sun.star.beans import XPropertySet, Property 119cdf0e10cSrcweirfrom com.sun.star.container import XNameContainer 120cdf0e10cSrcweirfrom com.sun.star.xml.sax import XDocumentHandler, InputSource 121cdf0e10cSrcweirfrom com.sun.star.uno import Exception as UnoException 122cdf0e10cSrcweirfrom com.sun.star.script import XInvocation 123cdf0e10cSrcweirfrom com.sun.star.awt import XActionListener 124cdf0e10cSrcweir 125cdf0e10cSrcweirfrom com.sun.star.script.provider import XScriptProvider, XScript, XScriptContext, ScriptFrameworkErrorException 126cdf0e10cSrcweirfrom com.sun.star.script.browse import XBrowseNode 127cdf0e10cSrcweirfrom com.sun.star.script.browse.BrowseNodeTypes import SCRIPT, CONTAINER, ROOT 128cdf0e10cSrcweirfrom com.sun.star.util import XModifyListener 129cdf0e10cSrcweir 130cdf0e10cSrcweirLANGUAGENAME = "Python" 131cdf0e10cSrcweirGLOBAL_SCRIPTCONTEXT_NAME = "XSCRIPTCONTEXT" 132cdf0e10cSrcweirCALLABLE_CONTAINER_NAME = "g_exportedScripts" 133cdf0e10cSrcweir 134cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable 135cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper() 136cdf0e10cSrcweirg_implName = "org.openoffice.pyuno.LanguageScriptProviderFor"+LANGUAGENAME 137cdf0e10cSrcweir 138cdf0e10cSrcweir 139cdf0e10cSrcweir 140cdf0e10cSrcweirBLOCK_SIZE = 65536 141cdf0e10cSrcweirdef readTextFromStream( inputStream ): 142cdf0e10cSrcweir # read the file 143cdf0e10cSrcweir code = uno.ByteSequence( "" ) 144cdf0e10cSrcweir while True: 145cdf0e10cSrcweir read,out = inputStream.readBytes( None , BLOCK_SIZE ) 146cdf0e10cSrcweir code = code + out 147cdf0e10cSrcweir if read < BLOCK_SIZE: 148cdf0e10cSrcweir break 149cdf0e10cSrcweir return code.value 150cdf0e10cSrcweir 151cdf0e10cSrcweirdef toIniName( str ): 152cdf0e10cSrcweir # TODO: what is the official way to get to know whether i am on the windows platform ? 153cdf0e10cSrcweir if( hasattr(sys , "dllhandle") ): 154cdf0e10cSrcweir return str + ".ini" 155cdf0e10cSrcweir return str + "rc" 156cdf0e10cSrcweir 157cdf0e10cSrcweir 158cdf0e10cSrcweir""" definition: storageURI is the system dependent, absolute file url, where the script is stored on disk 159cdf0e10cSrcweir scriptURI is the system independent uri 160cdf0e10cSrcweir""" 161cdf0e10cSrcweirclass MyUriHelper: 162cdf0e10cSrcweir 163cdf0e10cSrcweir def __init__( self, ctx, location ): 164cdf0e10cSrcweir self.s_UriMap = \ 165cdf0e10cSrcweir { "share" : "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" + toIniName( "bootstrap") + "::BaseInstallation}/share/Scripts/python" , \ 166cdf0e10cSrcweir "share:uno_packages" : "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE/uno_packages", \ 167cdf0e10cSrcweir "user" : "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" + toIniName( "bootstrap") + "::UserInstallation}/user/Scripts/python" , \ 168cdf0e10cSrcweir "user:uno_packages" : "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages" } 169cdf0e10cSrcweir self.m_uriRefFac = ctx.ServiceManager.createInstanceWithContext("com.sun.star.uri.UriReferenceFactory",ctx) 170cdf0e10cSrcweir if location.startswith( "vnd.sun.star.tdoc" ): 171cdf0e10cSrcweir self.m_baseUri = location + "/Scripts/python" 172cdf0e10cSrcweir self.m_scriptUriLocation = "document" 173cdf0e10cSrcweir else: 174cdf0e10cSrcweir self.m_baseUri = expandUri( self.s_UriMap[location] ) 175cdf0e10cSrcweir self.m_scriptUriLocation = location 176cdf0e10cSrcweir log.isDebugLevel() and log.debug( "initialized urihelper with baseUri="+self.m_baseUri + ",m_scriptUriLocation="+self.m_scriptUriLocation ) 177cdf0e10cSrcweir 178cdf0e10cSrcweir def getRootStorageURI( self ): 179cdf0e10cSrcweir return self.m_baseUri 180cdf0e10cSrcweir 181cdf0e10cSrcweir def getStorageURI( self, scriptURI ): 182cdf0e10cSrcweir return self.scriptURI2StorageUri(scriptURI) 183cdf0e10cSrcweir 184cdf0e10cSrcweir def getScriptURI( self, storageURI ): 185cdf0e10cSrcweir return self.storageURI2ScriptUri(storageURI) 186cdf0e10cSrcweir 187cdf0e10cSrcweir def storageURI2ScriptUri( self, storageURI ): 188cdf0e10cSrcweir if not storageURI.startswith( self.m_baseUri ): 189cdf0e10cSrcweir message = "pythonscript: storage uri '" + storageURI + "' not in base uri '" + self.m_baseUri + "'" 190cdf0e10cSrcweir log.isDebugLevel() and log.debug( message ) 191cdf0e10cSrcweir raise RuntimeException( message ) 192cdf0e10cSrcweir 193cdf0e10cSrcweir ret = "vnd.sun.star.script:" + \ 194cdf0e10cSrcweir storageURI[len(self.m_baseUri)+1:].replace("/","|") + \ 195cdf0e10cSrcweir "?language=" + LANGUAGENAME + "&location=" + self.m_scriptUriLocation 196cdf0e10cSrcweir log.isDebugLevel() and log.debug( "converting storageURI="+storageURI + " to scriptURI=" + ret ) 197cdf0e10cSrcweir return ret 198cdf0e10cSrcweir 199cdf0e10cSrcweir def scriptURI2StorageUri( self, scriptURI ): 200cdf0e10cSrcweir try: 201cdf0e10cSrcweir myUri = self.m_uriRefFac.parse(scriptURI) 202cdf0e10cSrcweir ret = self.m_baseUri + "/" + myUri.getName().replace( "|", "/" ) 203cdf0e10cSrcweir log.isDebugLevel() and log.debug( "converting scriptURI="+scriptURI + " to storageURI=" + ret ) 204cdf0e10cSrcweir return ret 205cdf0e10cSrcweir except UnoException, e: 206cdf0e10cSrcweir log.error( "error during converting scriptURI="+scriptURI + ": " + e.Message) 207cdf0e10cSrcweir raise RuntimeException( "pythonscript:scriptURI2StorageUri: " +e.getMessage(), None ) 208cdf0e10cSrcweir except Exception, e: 209cdf0e10cSrcweir log.error( "error during converting scriptURI="+scriptURI + ": " + str(e)) 210cdf0e10cSrcweir raise RuntimeException( "pythonscript:scriptURI2StorageUri: " + str(e), None ) 211cdf0e10cSrcweir 212cdf0e10cSrcweir 213cdf0e10cSrcweirclass ModuleEntry: 214cdf0e10cSrcweir def __init__( self, lastRead, module ): 215cdf0e10cSrcweir self.lastRead = lastRead 216cdf0e10cSrcweir self.module = module 217cdf0e10cSrcweir 218cdf0e10cSrcweirdef hasChanged( oldDate, newDate ): 219cdf0e10cSrcweir return newDate.Year > oldDate.Year or \ 220cdf0e10cSrcweir newDate.Month > oldDate.Month or \ 221cdf0e10cSrcweir newDate.Day > oldDate.Day or \ 222cdf0e10cSrcweir newDate.Hours > oldDate.Hours or \ 223cdf0e10cSrcweir newDate.Minutes > oldDate.Minutes or \ 224cdf0e10cSrcweir newDate.Seconds > oldDate.Seconds or \ 225cdf0e10cSrcweir newDate.HundredthSeconds > oldDate.HundredthSeconds 226cdf0e10cSrcweir 227cdf0e10cSrcweirdef ensureSourceState( code ): 228cdf0e10cSrcweir if not code.endswith( "\n" ): 229cdf0e10cSrcweir code = code + "\n" 230cdf0e10cSrcweir code = code.replace( "\r", "" ) 231cdf0e10cSrcweir return code 232cdf0e10cSrcweir 233cdf0e10cSrcweir 234cdf0e10cSrcweirdef checkForPythonPathBesideScript( url ): 235cdf0e10cSrcweir if url.startswith( "file:" ): 236cdf0e10cSrcweir path = unohelper.fileUrlToSystemPath( url+"/pythonpath.zip" ); 237cdf0e10cSrcweir log.log( LogLevel.DEBUG, "checking for existence of " + path ) 238cdf0e10cSrcweir if 1 == os.access( encfile(path), os.F_OK) and not path in sys.path: 239cdf0e10cSrcweir log.log( LogLevel.DEBUG, "adding " + path + " to sys.path" ) 240cdf0e10cSrcweir sys.path.append( path ) 241cdf0e10cSrcweir 242cdf0e10cSrcweir path = unohelper.fileUrlToSystemPath( url+"/pythonpath" ); 243cdf0e10cSrcweir log.log( LogLevel.DEBUG, "checking for existence of " + path ) 244cdf0e10cSrcweir if 1 == os.access( encfile(path), os.F_OK) and not path in sys.path: 245cdf0e10cSrcweir log.log( LogLevel.DEBUG, "adding " + path + " to sys.path" ) 246cdf0e10cSrcweir sys.path.append( path ) 247cdf0e10cSrcweir 248cdf0e10cSrcweir 249cdf0e10cSrcweirclass ScriptContext(unohelper.Base): 250*61c9e2f8SAriel Constenla-Haile def __init__( self, ctx, doc, inv ): 251cdf0e10cSrcweir self.ctx = ctx 252cdf0e10cSrcweir self.doc = doc 253*61c9e2f8SAriel Constenla-Haile self.inv = inv 254cdf0e10cSrcweir 255cdf0e10cSrcweir # XScriptContext 256cdf0e10cSrcweir def getDocument(self): 257*61c9e2f8SAriel Constenla-Haile if self.doc: 258*61c9e2f8SAriel Constenla-Haile return self.doc 259cdf0e10cSrcweir return self.getDesktop().getCurrentComponent() 260cdf0e10cSrcweir 261cdf0e10cSrcweir def getDesktop(self): 262cdf0e10cSrcweir return self.ctx.ServiceManager.createInstanceWithContext( 263cdf0e10cSrcweir "com.sun.star.frame.Desktop", self.ctx ) 264cdf0e10cSrcweir 265cdf0e10cSrcweir def getComponentContext(self): 266cdf0e10cSrcweir return self.ctx 267cdf0e10cSrcweir 268*61c9e2f8SAriel Constenla-Haile def getInvocationContext(self): 269*61c9e2f8SAriel Constenla-Haile return self.inv 270*61c9e2f8SAriel Constenla-Haile 271cdf0e10cSrcweir#---------------------------------- 272cdf0e10cSrcweir# Global Module Administration 273cdf0e10cSrcweir# does not fit together with script 274cdf0e10cSrcweir# engine lifetime management 275cdf0e10cSrcweir#---------------------------------- 276cdf0e10cSrcweir#g_scriptContext = ScriptContext( uno.getComponentContext(), None ) 277cdf0e10cSrcweir#g_modules = {} 278cdf0e10cSrcweir#def getModuleByUrl( url, sfa ): 279cdf0e10cSrcweir# entry = g_modules.get(url) 280cdf0e10cSrcweir# load = True 281cdf0e10cSrcweir# lastRead = sfa.getDateTimeModified( url ) 282cdf0e10cSrcweir# if entry: 283cdf0e10cSrcweir# if hasChanged( entry.lastRead, lastRead ): 284cdf0e10cSrcweir# log.isDebugLevel() and log.debug("file " + url + " has changed, reloading") 285cdf0e10cSrcweir# else: 286cdf0e10cSrcweir# load = False 287cdf0e10cSrcweir# 288cdf0e10cSrcweir# if load: 289cdf0e10cSrcweir# log.isDebugLevel() and log.debug( "opening >" + url + "<" ) 290cdf0e10cSrcweir# 291cdf0e10cSrcweir# code = readTextFromStream( sfa.openFileRead( url ) ) 292cdf0e10cSrcweir 293cdf0e10cSrcweir # execute the module 294cdf0e10cSrcweir# entry = ModuleEntry( lastRead, imp.new_module("ooo_script_framework") ) 295cdf0e10cSrcweir# entry.module.__dict__[GLOBAL_SCRIPTCONTEXT_NAME] = g_scriptContext 296cdf0e10cSrcweir# entry.module.__file__ = url 297cdf0e10cSrcweir# exec code in entry.module.__dict__ 298cdf0e10cSrcweir# g_modules[ url ] = entry 299cdf0e10cSrcweir# log.isDebugLevel() and log.debug( "mapped " + url + " to " + str( entry.module ) ) 300cdf0e10cSrcweir# return entry.module 301cdf0e10cSrcweir 302cdf0e10cSrcweirclass ProviderContext: 303cdf0e10cSrcweir def __init__( self, storageType, sfa, uriHelper, scriptContext ): 304cdf0e10cSrcweir self.storageType = storageType 305cdf0e10cSrcweir self.sfa = sfa 306cdf0e10cSrcweir self.uriHelper = uriHelper 307cdf0e10cSrcweir self.scriptContext = scriptContext 308cdf0e10cSrcweir self.modules = {} 309cdf0e10cSrcweir self.rootUrl = None 310cdf0e10cSrcweir self.mapPackageName2Path = None 311cdf0e10cSrcweir 312cdf0e10cSrcweir def getTransientPartFromUrl( self, url ): 313cdf0e10cSrcweir rest = url.replace( self.rootUrl , "",1 ).replace( "/","",1) 314cdf0e10cSrcweir return rest[0:rest.find("/")] 315cdf0e10cSrcweir 316cdf0e10cSrcweir def getPackageNameFromUrl( self, url ): 317cdf0e10cSrcweir rest = url.replace( self.rootUrl , "",1 ).replace( "/","",1) 318cdf0e10cSrcweir start = rest.find("/") +1 319cdf0e10cSrcweir return rest[start:rest.find("/",start)] 320cdf0e10cSrcweir 321cdf0e10cSrcweir 322cdf0e10cSrcweir def removePackageByUrl( self, url ): 323cdf0e10cSrcweir items = self.mapPackageName2Path.items() 324cdf0e10cSrcweir for i in items: 325cdf0e10cSrcweir if url in i[1].pathes: 326cdf0e10cSrcweir self.mapPackageName2Path.pop(i[0]) 327cdf0e10cSrcweir break 328cdf0e10cSrcweir 329cdf0e10cSrcweir def addPackageByUrl( self, url ): 330cdf0e10cSrcweir packageName = self.getPackageNameFromUrl( url ) 331cdf0e10cSrcweir transientPart = self.getTransientPartFromUrl( url ) 332cdf0e10cSrcweir log.isDebugLevel() and log.debug( "addPackageByUrl : " + packageName + ", " + transientPart + "("+url+")" + ", rootUrl="+self.rootUrl ) 333cdf0e10cSrcweir if self.mapPackageName2Path.has_key( packageName ): 334cdf0e10cSrcweir package = self.mapPackageName2Path[ packageName ] 335cdf0e10cSrcweir package.pathes = package.pathes + (url, ) 336cdf0e10cSrcweir else: 337cdf0e10cSrcweir package = Package( (url,), transientPart) 338cdf0e10cSrcweir self.mapPackageName2Path[ packageName ] = package 339cdf0e10cSrcweir 340cdf0e10cSrcweir def isUrlInPackage( self, url ): 341cdf0e10cSrcweir values = self.mapPackageName2Path.values() 342cdf0e10cSrcweir for i in values: 343cdf0e10cSrcweir# print "checking " + url + " in " + str(i.pathes) 344cdf0e10cSrcweir if url in i.pathes: 345cdf0e10cSrcweir return True 346cdf0e10cSrcweir# print "false" 347cdf0e10cSrcweir return False 348cdf0e10cSrcweir 349cdf0e10cSrcweir def setPackageAttributes( self, mapPackageName2Path, rootUrl ): 350cdf0e10cSrcweir self.mapPackageName2Path = mapPackageName2Path 351cdf0e10cSrcweir self.rootUrl = rootUrl 352cdf0e10cSrcweir 353cdf0e10cSrcweir def getPersistentUrlFromStorageUrl( self, url ): 354cdf0e10cSrcweir # package name is the second directory 355cdf0e10cSrcweir ret = url 356cdf0e10cSrcweir if self.rootUrl: 357cdf0e10cSrcweir pos = len( self.rootUrl) +1 358cdf0e10cSrcweir ret = url[0:pos]+url[url.find("/",pos)+1:len(url)] 359cdf0e10cSrcweir log.isDebugLevel() and log.debug( "getPersistentUrlFromStorageUrl " + url + " -> "+ ret) 360cdf0e10cSrcweir return ret 361cdf0e10cSrcweir 362cdf0e10cSrcweir def getStorageUrlFromPersistentUrl( self, url): 363cdf0e10cSrcweir ret = url 364cdf0e10cSrcweir if self.rootUrl: 365cdf0e10cSrcweir pos = len(self.rootUrl)+1 366cdf0e10cSrcweir packageName = url[pos:url.find("/",pos+1)] 367cdf0e10cSrcweir package = self.mapPackageName2Path[ packageName ] 368cdf0e10cSrcweir ret = url[0:pos]+ package.transientPathElement + "/" + url[pos:len(url)] 369cdf0e10cSrcweir log.isDebugLevel() and log.debug( "getStorageUrlFromPersistentUrl " + url + " -> "+ ret) 370cdf0e10cSrcweir return ret 371cdf0e10cSrcweir 372cdf0e10cSrcweir def getFuncsByUrl( self, url ): 373cdf0e10cSrcweir src = readTextFromStream( self.sfa.openFileRead( url ) ) 374cdf0e10cSrcweir checkForPythonPathBesideScript( url[0:url.rfind('/')] ) 375cdf0e10cSrcweir src = ensureSourceState( src ) 376cdf0e10cSrcweir 377cdf0e10cSrcweir code = compiler.parse( src ) 378cdf0e10cSrcweir 379cdf0e10cSrcweir allFuncs = [] 380cdf0e10cSrcweir 381cdf0e10cSrcweir if code == None: 382cdf0e10cSrcweir return allFuncs 383cdf0e10cSrcweir 384cdf0e10cSrcweir g_exportedScripts = [] 385cdf0e10cSrcweir for node in code.node.nodes: 386cdf0e10cSrcweir if node.__class__.__name__ == 'Function': 387cdf0e10cSrcweir allFuncs.append(node.name) 388cdf0e10cSrcweir elif node.__class__.__name__ == 'Assign': 389cdf0e10cSrcweir for assignee in node.nodes: 390cdf0e10cSrcweir if assignee.name == 'g_exportedScripts': 391cdf0e10cSrcweir for item in node.expr: 392cdf0e10cSrcweir if item.__class__.__name__ == 'Name': 393cdf0e10cSrcweir g_exportedScripts.append(item.name) 394cdf0e10cSrcweir return g_exportedScripts 395cdf0e10cSrcweir 396cdf0e10cSrcweir return allFuncs 397cdf0e10cSrcweir 398cdf0e10cSrcweir def getModuleByUrl( self, url ): 399cdf0e10cSrcweir entry = self.modules.get(url) 400cdf0e10cSrcweir load = True 401cdf0e10cSrcweir lastRead = self.sfa.getDateTimeModified( url ) 402cdf0e10cSrcweir if entry: 403cdf0e10cSrcweir if hasChanged( entry.lastRead, lastRead ): 404cdf0e10cSrcweir log.isDebugLevel() and log.debug( "file " + url + " has changed, reloading" ) 405cdf0e10cSrcweir else: 406cdf0e10cSrcweir load = False 407cdf0e10cSrcweir 408cdf0e10cSrcweir if load: 409cdf0e10cSrcweir log.isDebugLevel() and log.debug( "opening >" + url + "<" ) 410cdf0e10cSrcweir 411cdf0e10cSrcweir src = readTextFromStream( self.sfa.openFileRead( url ) ) 412cdf0e10cSrcweir checkForPythonPathBesideScript( url[0:url.rfind('/')] ) 413cdf0e10cSrcweir src = ensureSourceState( src ) 414cdf0e10cSrcweir 415cdf0e10cSrcweir # execute the module 416cdf0e10cSrcweir entry = ModuleEntry( lastRead, imp.new_module("ooo_script_framework") ) 417cdf0e10cSrcweir entry.module.__dict__[GLOBAL_SCRIPTCONTEXT_NAME] = self.scriptContext 418cdf0e10cSrcweir 419cdf0e10cSrcweir code = None 420cdf0e10cSrcweir if url.startswith( "file:" ): 421cdf0e10cSrcweir code = compile( src, encfile(uno.fileUrlToSystemPath( url ) ), "exec" ) 422cdf0e10cSrcweir else: 423cdf0e10cSrcweir code = compile( src, url, "exec" ) 424cdf0e10cSrcweir exec code in entry.module.__dict__ 425cdf0e10cSrcweir entry.module.__file__ = url 426cdf0e10cSrcweir self.modules[ url ] = entry 427cdf0e10cSrcweir log.isDebugLevel() and log.debug( "mapped " + url + " to " + str( entry.module ) ) 428cdf0e10cSrcweir return entry.module 429cdf0e10cSrcweir 430cdf0e10cSrcweir#-------------------------------------------------- 431cdf0e10cSrcweirdef isScript( candidate ): 432cdf0e10cSrcweir ret = False 433cdf0e10cSrcweir if isinstance( candidate, type(isScript) ): 434cdf0e10cSrcweir ret = True 435cdf0e10cSrcweir return ret 436cdf0e10cSrcweir 437cdf0e10cSrcweir#------------------------------------------------------- 438cdf0e10cSrcweirclass ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, XActionListener ): 439cdf0e10cSrcweir def __init__( self, provCtx, uri, fileName, funcName ): 440cdf0e10cSrcweir self.fileName = fileName 441cdf0e10cSrcweir self.funcName = funcName 442cdf0e10cSrcweir self.provCtx = provCtx 443cdf0e10cSrcweir self.uri = uri 444cdf0e10cSrcweir 445cdf0e10cSrcweir def getName( self ): 446cdf0e10cSrcweir return self.funcName 447cdf0e10cSrcweir 448cdf0e10cSrcweir def getChildNodes(self): 449cdf0e10cSrcweir return () 450cdf0e10cSrcweir 451cdf0e10cSrcweir def hasChildNodes(self): 452cdf0e10cSrcweir return False 453cdf0e10cSrcweir 454cdf0e10cSrcweir def getType( self): 455cdf0e10cSrcweir return SCRIPT 456cdf0e10cSrcweir 457cdf0e10cSrcweir def getPropertyValue( self, name ): 458cdf0e10cSrcweir ret = None 459cdf0e10cSrcweir try: 460cdf0e10cSrcweir if name == "URI": 461cdf0e10cSrcweir ret = self.provCtx.uriHelper.getScriptURI( 462cdf0e10cSrcweir self.provCtx.getPersistentUrlFromStorageUrl( self.uri + "$" + self.funcName ) ) 463cdf0e10cSrcweir elif name == "Editable" and ENABLE_EDIT_DIALOG: 464cdf0e10cSrcweir ret = not self.provCtx.sfa.isReadOnly( self.uri ) 465cdf0e10cSrcweir 466cdf0e10cSrcweir log.isDebugLevel() and log.debug( "ScriptBrowseNode.getPropertyValue called for " + name + ", returning " + str(ret) ) 467cdf0e10cSrcweir except Exception,e: 468cdf0e10cSrcweir log.error( "ScriptBrowseNode.getPropertyValue error " + lastException2String()) 469cdf0e10cSrcweir raise 470cdf0e10cSrcweir 471cdf0e10cSrcweir return ret 472cdf0e10cSrcweir def setPropertyValue( self, name, value ): 473cdf0e10cSrcweir log.isDebugLevel() and log.debug( "ScriptBrowseNode.setPropertyValue called " + name + "=" +str(value ) ) 474cdf0e10cSrcweir def getPropertySetInfo( self ): 475cdf0e10cSrcweir log.isDebugLevel() and log.debug( "ScriptBrowseNode.getPropertySetInfo called " ) 476cdf0e10cSrcweir return None 477cdf0e10cSrcweir 478cdf0e10cSrcweir def getIntrospection( self ): 479cdf0e10cSrcweir return None 480cdf0e10cSrcweir 481cdf0e10cSrcweir def invoke( self, name, params, outparamindex, outparams ): 482cdf0e10cSrcweir if name == "Editable": 483cdf0e10cSrcweir servicename = "com.sun.star.awt.DialogProvider" 484cdf0e10cSrcweir ctx = self.provCtx.scriptContext.getComponentContext() 485cdf0e10cSrcweir dlgprov = ctx.ServiceManager.createInstanceWithContext( 486cdf0e10cSrcweir servicename, ctx ) 487cdf0e10cSrcweir 488cdf0e10cSrcweir self.editor = dlgprov.createDialog( 489cdf0e10cSrcweir "vnd.sun.star.script:" + 490cdf0e10cSrcweir "ScriptBindingLibrary.MacroEditor?location=application") 491cdf0e10cSrcweir 492cdf0e10cSrcweir code = readTextFromStream(self.provCtx.sfa.openFileRead(self.uri)) 493cdf0e10cSrcweir code = ensureSourceState( code ) 494cdf0e10cSrcweir self.editor.getControl("EditorTextField").setText(code) 495cdf0e10cSrcweir 496cdf0e10cSrcweir self.editor.getControl("RunButton").setActionCommand("Run") 497cdf0e10cSrcweir self.editor.getControl("RunButton").addActionListener(self) 498cdf0e10cSrcweir self.editor.getControl("SaveButton").setActionCommand("Save") 499cdf0e10cSrcweir self.editor.getControl("SaveButton").addActionListener(self) 500cdf0e10cSrcweir 501cdf0e10cSrcweir self.editor.execute() 502cdf0e10cSrcweir 503cdf0e10cSrcweir return None 504cdf0e10cSrcweir 505cdf0e10cSrcweir def actionPerformed( self, event ): 506cdf0e10cSrcweir try: 507cdf0e10cSrcweir if event.ActionCommand == "Run": 508cdf0e10cSrcweir code = self.editor.getControl("EditorTextField").getText() 509cdf0e10cSrcweir code = ensureSourceState( code ) 510cdf0e10cSrcweir mod = imp.new_module("ooo_script_framework") 511cdf0e10cSrcweir mod.__dict__[GLOBAL_SCRIPTCONTEXT_NAME] = self.provCtx.scriptContext 512cdf0e10cSrcweir exec code in mod.__dict__ 513cdf0e10cSrcweir values = mod.__dict__.get( CALLABLE_CONTAINER_NAME , None ) 514cdf0e10cSrcweir if not values: 515cdf0e10cSrcweir values = mod.__dict__.values() 516cdf0e10cSrcweir 517cdf0e10cSrcweir for i in values: 518cdf0e10cSrcweir if isScript( i ): 519cdf0e10cSrcweir i() 520cdf0e10cSrcweir break 521cdf0e10cSrcweir 522cdf0e10cSrcweir elif event.ActionCommand == "Save": 523cdf0e10cSrcweir toWrite = uno.ByteSequence( 524cdf0e10cSrcweir str( 525cdf0e10cSrcweir self.editor.getControl("EditorTextField").getText().encode( 526cdf0e10cSrcweir sys.getdefaultencoding())) ) 527cdf0e10cSrcweir copyUrl = self.uri + ".orig" 528cdf0e10cSrcweir self.provCtx.sfa.move( self.uri, copyUrl ) 529cdf0e10cSrcweir out = self.provCtx.sfa.openFileWrite( self.uri ) 530cdf0e10cSrcweir out.writeBytes( toWrite ) 531cdf0e10cSrcweir out.close() 532cdf0e10cSrcweir self.provCtx.sfa.kill( copyUrl ) 533cdf0e10cSrcweir# log.isDebugLevel() and log.debug("Save is not implemented yet") 534cdf0e10cSrcweir# text = self.editor.getControl("EditorTextField").getText() 535cdf0e10cSrcweir# log.isDebugLevel() and log.debug("Would save: " + text) 536cdf0e10cSrcweir except Exception,e: 537cdf0e10cSrcweir # TODO: add an error box here ! 538cdf0e10cSrcweir log.error( lastException2String() ) 539cdf0e10cSrcweir 540cdf0e10cSrcweir 541cdf0e10cSrcweir def setValue( self, name, value ): 542cdf0e10cSrcweir return None 543cdf0e10cSrcweir 544cdf0e10cSrcweir def getValue( self, name ): 545cdf0e10cSrcweir return None 546cdf0e10cSrcweir 547cdf0e10cSrcweir def hasMethod( self, name ): 548cdf0e10cSrcweir return False 549cdf0e10cSrcweir 550cdf0e10cSrcweir def hasProperty( self, name ): 551cdf0e10cSrcweir return False 552cdf0e10cSrcweir 553cdf0e10cSrcweir 554cdf0e10cSrcweir#------------------------------------------------------- 555cdf0e10cSrcweirclass FileBrowseNode( unohelper.Base, XBrowseNode ): 556cdf0e10cSrcweir def __init__( self, provCtx, uri , name ): 557cdf0e10cSrcweir self.provCtx = provCtx 558cdf0e10cSrcweir self.uri = uri 559cdf0e10cSrcweir self.name = name 560cdf0e10cSrcweir self.funcnames = None 561cdf0e10cSrcweir 562cdf0e10cSrcweir def getName( self ): 563cdf0e10cSrcweir return self.name 564cdf0e10cSrcweir 565cdf0e10cSrcweir def getChildNodes(self): 566cdf0e10cSrcweir ret = () 567cdf0e10cSrcweir try: 568cdf0e10cSrcweir self.funcnames = self.provCtx.getFuncsByUrl( self.uri ) 569cdf0e10cSrcweir 570cdf0e10cSrcweir scriptNodeList = [] 571cdf0e10cSrcweir for i in self.funcnames: 572cdf0e10cSrcweir scriptNodeList.append( 573cdf0e10cSrcweir ScriptBrowseNode( 574cdf0e10cSrcweir self.provCtx, self.uri, self.name, i )) 575cdf0e10cSrcweir ret = tuple( scriptNodeList ) 576cdf0e10cSrcweir log.isDebugLevel() and log.debug( "returning " +str(len(ret)) + " ScriptChildNodes on " + self.uri ) 577cdf0e10cSrcweir except Exception, e: 578cdf0e10cSrcweir text = lastException2String() 579cdf0e10cSrcweir log.error( "Error while evaluating " + self.uri + ":" + text ) 580cdf0e10cSrcweir raise 581cdf0e10cSrcweir return ret 582cdf0e10cSrcweir 583cdf0e10cSrcweir def hasChildNodes(self): 584cdf0e10cSrcweir try: 585cdf0e10cSrcweir return len(self.getChildNodes()) > 0 586cdf0e10cSrcweir except Exception, e: 587cdf0e10cSrcweir return False 588cdf0e10cSrcweir 589cdf0e10cSrcweir def getType( self): 590cdf0e10cSrcweir return CONTAINER 591cdf0e10cSrcweir 592cdf0e10cSrcweir 593cdf0e10cSrcweir 594cdf0e10cSrcweirclass DirBrowseNode( unohelper.Base, XBrowseNode ): 595cdf0e10cSrcweir def __init__( self, provCtx, name, rootUrl ): 596cdf0e10cSrcweir self.provCtx = provCtx 597cdf0e10cSrcweir self.name = name 598cdf0e10cSrcweir self.rootUrl = rootUrl 599cdf0e10cSrcweir 600cdf0e10cSrcweir def getName( self ): 601cdf0e10cSrcweir return self.name 602cdf0e10cSrcweir 603cdf0e10cSrcweir def getChildNodes( self ): 604cdf0e10cSrcweir try: 605cdf0e10cSrcweir log.isDebugLevel() and log.debug( "DirBrowseNode.getChildNodes called for " + self.rootUrl ) 606cdf0e10cSrcweir contents = self.provCtx.sfa.getFolderContents( self.rootUrl, True ) 607cdf0e10cSrcweir browseNodeList = [] 608cdf0e10cSrcweir for i in contents: 609cdf0e10cSrcweir if i.endswith( ".py" ): 610cdf0e10cSrcweir log.isDebugLevel() and log.debug( "adding filenode " + i ) 611cdf0e10cSrcweir browseNodeList.append( 612cdf0e10cSrcweir FileBrowseNode( self.provCtx, i, i[i.rfind("/")+1:len(i)-3] ) ) 613cdf0e10cSrcweir elif self.provCtx.sfa.isFolder( i ) and not i.endswith("/pythonpath"): 614cdf0e10cSrcweir log.isDebugLevel() and log.debug( "adding DirBrowseNode " + i ) 615cdf0e10cSrcweir browseNodeList.append( DirBrowseNode( self.provCtx, i[i.rfind("/")+1:len(i)],i)) 616cdf0e10cSrcweir return tuple( browseNodeList ) 617cdf0e10cSrcweir except Exception, e: 618cdf0e10cSrcweir text = lastException2String() 619cdf0e10cSrcweir log.error( "DirBrowseNode error: " + str(e) + " while evaluating " + self.rootUrl) 620cdf0e10cSrcweir log.error( text) 621cdf0e10cSrcweir return () 622cdf0e10cSrcweir 623cdf0e10cSrcweir def hasChildNodes( self ): 624cdf0e10cSrcweir return True 625cdf0e10cSrcweir 626cdf0e10cSrcweir def getType( self ): 627cdf0e10cSrcweir return CONTAINER 628cdf0e10cSrcweir 629cdf0e10cSrcweir def getScript( self, uri ): 630cdf0e10cSrcweir log.debug( "DirBrowseNode getScript " + uri + " invoked" ) 631cdf0e10cSrcweir raise IllegalArgumentException( "DirBrowseNode couldn't instantiate script " + uri , self , 0 ) 632cdf0e10cSrcweir 633cdf0e10cSrcweir 634cdf0e10cSrcweirclass ManifestHandler( XDocumentHandler, unohelper.Base ): 635cdf0e10cSrcweir def __init__( self, rootUrl ): 636cdf0e10cSrcweir self.rootUrl = rootUrl 637cdf0e10cSrcweir 638cdf0e10cSrcweir def startDocument( self ): 639cdf0e10cSrcweir self.urlList = [] 640cdf0e10cSrcweir 641cdf0e10cSrcweir def endDocument( self ): 642cdf0e10cSrcweir pass 643cdf0e10cSrcweir 644cdf0e10cSrcweir def startElement( self , name, attlist): 645cdf0e10cSrcweir if name == "manifest:file-entry": 646cdf0e10cSrcweir if attlist.getValueByName( "manifest:media-type" ) == "application/vnd.sun.star.framework-script": 647cdf0e10cSrcweir self.urlList.append( 648cdf0e10cSrcweir self.rootUrl + "/" + attlist.getValueByName( "manifest:full-path" ) ) 649cdf0e10cSrcweir 650cdf0e10cSrcweir def endElement( self, name ): 651cdf0e10cSrcweir pass 652cdf0e10cSrcweir 653cdf0e10cSrcweir def characters ( self, chars ): 654cdf0e10cSrcweir pass 655cdf0e10cSrcweir 656cdf0e10cSrcweir def ignoreableWhitespace( self, chars ): 657cdf0e10cSrcweir pass 658cdf0e10cSrcweir 659cdf0e10cSrcweir def setDocumentLocator( self, locator ): 660cdf0e10cSrcweir pass 661cdf0e10cSrcweir 662cdf0e10cSrcweirdef isPyFileInPath( sfa, path ): 663cdf0e10cSrcweir ret = False 664cdf0e10cSrcweir contents = sfa.getFolderContents( path, True ) 665cdf0e10cSrcweir for i in contents: 666cdf0e10cSrcweir if sfa.isFolder(i): 667cdf0e10cSrcweir ret = isPyFileInPath(sfa,i) 668cdf0e10cSrcweir else: 669cdf0e10cSrcweir if i.endswith(".py"): 670cdf0e10cSrcweir ret = True 671cdf0e10cSrcweir if ret: 672cdf0e10cSrcweir break 673cdf0e10cSrcweir return ret 674cdf0e10cSrcweir 675cdf0e10cSrcweir# extracts META-INF directory from 676cdf0e10cSrcweirdef getPathesFromPackage( rootUrl, sfa ): 677cdf0e10cSrcweir ret = () 678cdf0e10cSrcweir try: 679cdf0e10cSrcweir fileUrl = rootUrl + "/META-INF/manifest.xml" 680cdf0e10cSrcweir inputStream = sfa.openFileRead( fileUrl ) 681cdf0e10cSrcweir parser = uno.getComponentContext().ServiceManager.createInstance( "com.sun.star.xml.sax.Parser" ) 682cdf0e10cSrcweir handler = ManifestHandler( rootUrl ) 683cdf0e10cSrcweir parser.setDocumentHandler( handler ) 684cdf0e10cSrcweir parser.parseStream( InputSource( inputStream , "", fileUrl, fileUrl ) ) 685cdf0e10cSrcweir for i in tuple(handler.urlList): 686cdf0e10cSrcweir if not isPyFileInPath( sfa, i ): 687cdf0e10cSrcweir handler.urlList.remove(i) 688cdf0e10cSrcweir ret = tuple( handler.urlList ) 689cdf0e10cSrcweir except UnoException, e: 690cdf0e10cSrcweir text = lastException2String() 691cdf0e10cSrcweir log.debug( "getPathesFromPackage " + fileUrl + " Exception: " +text ) 692cdf0e10cSrcweir pass 693cdf0e10cSrcweir return ret 694cdf0e10cSrcweir 695cdf0e10cSrcweir 696cdf0e10cSrcweirclass Package: 697cdf0e10cSrcweir def __init__( self, pathes, transientPathElement ): 698cdf0e10cSrcweir self.pathes = pathes 699cdf0e10cSrcweir self.transientPathElement = transientPathElement 700cdf0e10cSrcweir 701cdf0e10cSrcweirclass DummyInteractionHandler( unohelper.Base, XInteractionHandler ): 702cdf0e10cSrcweir def __init__( self ): 703cdf0e10cSrcweir pass 704cdf0e10cSrcweir def handle( self, event): 705cdf0e10cSrcweir log.isDebugLevel() and log.debug( "pythonscript: DummyInteractionHandler.handle " + str( event ) ) 706cdf0e10cSrcweir 707cdf0e10cSrcweirclass DummyProgressHandler( unohelper.Base, XProgressHandler ): 708cdf0e10cSrcweir def __init__( self ): 709cdf0e10cSrcweir pass 710cdf0e10cSrcweir 711cdf0e10cSrcweir def push( self,status ): 712cdf0e10cSrcweir log.isDebugLevel() and log.debug( "pythonscript: DummyProgressHandler.push " + str( status ) ) 713cdf0e10cSrcweir def update( self,status ): 714cdf0e10cSrcweir log.isDebugLevel() and log.debug( "pythonscript: DummyProgressHandler.update " + str( status ) ) 715cdf0e10cSrcweir def pop( self ): 716cdf0e10cSrcweir log.isDebugLevel() and log.debug( "pythonscript: DummyProgressHandler.push " + str( event ) ) 717cdf0e10cSrcweir 718cdf0e10cSrcweirclass CommandEnvironment(unohelper.Base, XCommandEnvironment): 719cdf0e10cSrcweir def __init__( self ): 720cdf0e10cSrcweir self.progressHandler = DummyProgressHandler() 721cdf0e10cSrcweir self.interactionHandler = DummyInteractionHandler() 722cdf0e10cSrcweir def getInteractionHandler( self ): 723cdf0e10cSrcweir return self.interactionHandler 724cdf0e10cSrcweir def getProgressHandler( self ): 725cdf0e10cSrcweir return self.progressHandler 726cdf0e10cSrcweir 727cdf0e10cSrcweir#maybe useful for debugging purposes 728cdf0e10cSrcweir#class ModifyListener( unohelper.Base, XModifyListener ): 729cdf0e10cSrcweir# def __init__( self ): 730cdf0e10cSrcweir# pass 731cdf0e10cSrcweir# def modified( self, event ): 732cdf0e10cSrcweir# log.isDebugLevel() and log.debug( "pythonscript: ModifyListener.modified " + str( event ) ) 733cdf0e10cSrcweir# def disposing( self, event ): 734cdf0e10cSrcweir# log.isDebugLevel() and log.debug( "pythonscript: ModifyListener.disposing " + str( event ) ) 735cdf0e10cSrcweir 736*61c9e2f8SAriel Constenla-Hailedef getModelFromDocUrl(ctx, url): 737*61c9e2f8SAriel Constenla-Haile """Get document model from document url.""" 738*61c9e2f8SAriel Constenla-Haile doc = None 739*61c9e2f8SAriel Constenla-Haile args = ("Local", "Office") 740*61c9e2f8SAriel Constenla-Haile ucb = ctx.getServiceManager().createInstanceWithArgumentsAndContext( 741*61c9e2f8SAriel Constenla-Haile "com.sun.star.ucb.UniversalContentBroker", args, ctx) 742*61c9e2f8SAriel Constenla-Haile identifier = ucb.createContentIdentifier(url) 743*61c9e2f8SAriel Constenla-Haile content = ucb.queryContent(identifier) 744*61c9e2f8SAriel Constenla-Haile p = Property() 745*61c9e2f8SAriel Constenla-Haile p.Name = "DocumentModel" 746*61c9e2f8SAriel Constenla-Haile p.Handle = -1 747*61c9e2f8SAriel Constenla-Haile 748*61c9e2f8SAriel Constenla-Haile c = Command() 749*61c9e2f8SAriel Constenla-Haile c.Handle = -1 750*61c9e2f8SAriel Constenla-Haile c.Name = "getPropertyValues" 751*61c9e2f8SAriel Constenla-Haile c.Argument = uno.Any("[]com.sun.star.beans.Property", (p,)) 752*61c9e2f8SAriel Constenla-Haile 753*61c9e2f8SAriel Constenla-Haile env = CommandEnvironment() 754*61c9e2f8SAriel Constenla-Haile try: 755*61c9e2f8SAriel Constenla-Haile ret = content.execute(c, 0, env) 756*61c9e2f8SAriel Constenla-Haile doc = ret.getObject(1, None) 757*61c9e2f8SAriel Constenla-Haile except Exception, e: 758*61c9e2f8SAriel Constenla-Haile log.isErrorLevel() and log.error("getModelFromDocUrl: %s" % url) 759*61c9e2f8SAriel Constenla-Haile return doc 760*61c9e2f8SAriel Constenla-Haile 761cdf0e10cSrcweirdef mapStorageType2PackageContext( storageType ): 762cdf0e10cSrcweir ret = storageType 763cdf0e10cSrcweir if( storageType == "share:uno_packages" ): 764cdf0e10cSrcweir ret = "shared" 765cdf0e10cSrcweir if( storageType == "user:uno_packages" ): 766cdf0e10cSrcweir ret = "user" 767cdf0e10cSrcweir return ret 768cdf0e10cSrcweir 769cdf0e10cSrcweirdef getPackageName2PathMap( sfa, storageType ): 770cdf0e10cSrcweir ret = {} 771cdf0e10cSrcweir packageManagerFactory = uno.getComponentContext().getValueByName( 772cdf0e10cSrcweir "/singletons/com.sun.star.deployment.thePackageManagerFactory" ) 773cdf0e10cSrcweir packageManager = packageManagerFactory.getPackageManager( 774cdf0e10cSrcweir mapStorageType2PackageContext(storageType)) 775cdf0e10cSrcweir# packageManager.addModifyListener( ModifyListener() ) 776cdf0e10cSrcweir log.isDebugLevel() and log.debug( "pythonscript: getPackageName2PathMap start getDeployedPackages" ) 777cdf0e10cSrcweir packages = packageManager.getDeployedPackages( 778cdf0e10cSrcweir packageManager.createAbortChannel(), CommandEnvironment( ) ) 779cdf0e10cSrcweir log.isDebugLevel() and log.debug( "pythonscript: getPackageName2PathMap end getDeployedPackages (" + str(len(packages))+")" ) 780cdf0e10cSrcweir 781cdf0e10cSrcweir for i in packages: 782cdf0e10cSrcweir log.isDebugLevel() and log.debug( "inspecting package " + i.Name + "("+i.Identifier.Value+")" ) 783cdf0e10cSrcweir transientPathElement = penultimateElement( i.URL ) 784cdf0e10cSrcweir j = expandUri( i.URL ) 785cdf0e10cSrcweir pathes = getPathesFromPackage( j, sfa ) 786cdf0e10cSrcweir if len( pathes ) > 0: 787cdf0e10cSrcweir # map package name to url, we need this later 788cdf0e10cSrcweir log.isErrorLevel() and log.error( "adding Package " + transientPathElement + " " + str( pathes ) ) 789cdf0e10cSrcweir ret[ lastElement( j ) ] = Package( pathes, transientPathElement ) 790cdf0e10cSrcweir return ret 791cdf0e10cSrcweir 792cdf0e10cSrcweirdef penultimateElement( aStr ): 793cdf0e10cSrcweir lastSlash = aStr.rindex("/") 794cdf0e10cSrcweir penultimateSlash = aStr.rindex("/",0,lastSlash-1) 795cdf0e10cSrcweir return aStr[ penultimateSlash+1:lastSlash ] 796cdf0e10cSrcweir 797cdf0e10cSrcweirdef lastElement( aStr): 798cdf0e10cSrcweir return aStr[ aStr.rfind( "/" )+1:len(aStr)] 799cdf0e10cSrcweir 800cdf0e10cSrcweirclass PackageBrowseNode( unohelper.Base, XBrowseNode ): 801cdf0e10cSrcweir def __init__( self, provCtx, name, rootUrl ): 802cdf0e10cSrcweir self.provCtx = provCtx 803cdf0e10cSrcweir self.name = name 804cdf0e10cSrcweir self.rootUrl = rootUrl 805cdf0e10cSrcweir 806cdf0e10cSrcweir def getName( self ): 807cdf0e10cSrcweir return self.name 808cdf0e10cSrcweir 809cdf0e10cSrcweir def getChildNodes( self ): 810cdf0e10cSrcweir items = self.provCtx.mapPackageName2Path.items() 811cdf0e10cSrcweir browseNodeList = [] 812cdf0e10cSrcweir for i in items: 813cdf0e10cSrcweir if len( i[1].pathes ) == 1: 814cdf0e10cSrcweir browseNodeList.append( 815cdf0e10cSrcweir DirBrowseNode( self.provCtx, i[0], i[1].pathes[0] )) 816cdf0e10cSrcweir else: 817cdf0e10cSrcweir for j in i[1].pathes: 818cdf0e10cSrcweir browseNodeList.append( 819cdf0e10cSrcweir DirBrowseNode( self.provCtx, i[0]+"."+lastElement(j), j ) ) 820cdf0e10cSrcweir return tuple( browseNodeList ) 821cdf0e10cSrcweir 822cdf0e10cSrcweir def hasChildNodes( self ): 823cdf0e10cSrcweir return len( self.mapPackageName2Path ) > 0 824cdf0e10cSrcweir 825cdf0e10cSrcweir def getType( self ): 826cdf0e10cSrcweir return CONTAINER 827cdf0e10cSrcweir 828cdf0e10cSrcweir def getScript( self, uri ): 829cdf0e10cSrcweir log.debug( "DirBrowseNode getScript " + uri + " invoked" ) 830cdf0e10cSrcweir raise IllegalArgumentException( "PackageBrowseNode couldn't instantiate script " + uri , self , 0 ) 831cdf0e10cSrcweir 832cdf0e10cSrcweir 833cdf0e10cSrcweir 834cdf0e10cSrcweir 835cdf0e10cSrcweirclass PythonScript( unohelper.Base, XScript ): 836cdf0e10cSrcweir def __init__( self, func, mod ): 837cdf0e10cSrcweir self.func = func 838cdf0e10cSrcweir self.mod = mod 839cdf0e10cSrcweir def invoke(self, args, out, outindex ): 840cdf0e10cSrcweir log.isDebugLevel() and log.debug( "PythonScript.invoke " + str( args ) ) 841cdf0e10cSrcweir try: 842cdf0e10cSrcweir ret = self.func( *args ) 843cdf0e10cSrcweir except UnoException,e: 844cdf0e10cSrcweir # UNO Exception continue to fly ... 845cdf0e10cSrcweir text = lastException2String() 846cdf0e10cSrcweir complete = "Error during invoking function " + \ 847cdf0e10cSrcweir str(self.func.__name__) + " in module " + \ 848cdf0e10cSrcweir self.mod.__file__ + " (" + text + ")" 849cdf0e10cSrcweir log.isDebugLevel() and log.debug( complete ) 850cdf0e10cSrcweir # some people may beat me up for modifying the exception text, 851cdf0e10cSrcweir # but otherwise office just shows 852cdf0e10cSrcweir # the type name and message text with no more information, 853cdf0e10cSrcweir # this is really bad for most users. 854cdf0e10cSrcweir e.Message = e.Message + " (" + complete + ")" 855cdf0e10cSrcweir raise 856cdf0e10cSrcweir except Exception,e: 857cdf0e10cSrcweir # General python exception are converted to uno RuntimeException 858cdf0e10cSrcweir text = lastException2String() 859cdf0e10cSrcweir complete = "Error during invoking function " + \ 860cdf0e10cSrcweir str(self.func.__name__) + " in module " + \ 861cdf0e10cSrcweir self.mod.__file__ + " (" + text + ")" 862cdf0e10cSrcweir log.isDebugLevel() and log.debug( complete ) 863cdf0e10cSrcweir raise RuntimeException( complete , self ) 864cdf0e10cSrcweir log.isDebugLevel() and log.debug( "PythonScript.invoke ret = " + str( ret ) ) 865cdf0e10cSrcweir return ret, (), () 866cdf0e10cSrcweir 867cdf0e10cSrcweirdef expandUri( uri ): 868cdf0e10cSrcweir if uri.startswith( "vnd.sun.star.expand:" ): 869cdf0e10cSrcweir uri = uri.replace( "vnd.sun.star.expand:", "",1) 870cdf0e10cSrcweir uri = uno.getComponentContext().getByName( 871cdf0e10cSrcweir "/singletons/com.sun.star.util.theMacroExpander" ).expandMacros( uri ) 872cdf0e10cSrcweir if uri.startswith( "file:" ): 873cdf0e10cSrcweir uri = uno.absolutize("",uri) # necessary to get rid of .. in uri 874cdf0e10cSrcweir return uri 875cdf0e10cSrcweir 876cdf0e10cSrcweir#-------------------------------------------------------------- 877cdf0e10cSrcweirclass PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameContainer): 878cdf0e10cSrcweir def __init__( self, ctx, *args ): 879cdf0e10cSrcweir if log.isDebugLevel(): 880cdf0e10cSrcweir mystr = "" 881cdf0e10cSrcweir for i in args: 882cdf0e10cSrcweir if len(mystr) > 0: 883cdf0e10cSrcweir mystr = mystr +"," 884cdf0e10cSrcweir mystr = mystr + str(i) 885cdf0e10cSrcweir log.debug( "Entering PythonScriptProvider.ctor" + mystr ) 886cdf0e10cSrcweir 887*61c9e2f8SAriel Constenla-Haile doc = None 888*61c9e2f8SAriel Constenla-Haile inv = None 889cdf0e10cSrcweir storageType = "" 890*61c9e2f8SAriel Constenla-Haile 891cdf0e10cSrcweir if isinstance(args[0],unicode ): 892cdf0e10cSrcweir storageType = args[0] 893*61c9e2f8SAriel Constenla-Haile if storageType.startswith( "vnd.sun.star.tdoc" ): 894*61c9e2f8SAriel Constenla-Haile doc = getModelFromDocUrl(ctx, storageType) 895cdf0e10cSrcweir else: 896*61c9e2f8SAriel Constenla-Haile inv = args[0] 897*61c9e2f8SAriel Constenla-Haile try: 898*61c9e2f8SAriel Constenla-Haile doc = inv.ScriptContainer 899*61c9e2f8SAriel Constenla-Haile content = ctx.getServiceManager().createInstanceWithContext( 900*61c9e2f8SAriel Constenla-Haile "com.sun.star.frame.TransientDocumentsDocumentContentFactory", 901*61c9e2f8SAriel Constenla-Haile ctx).createDocumentContent(doc) 902*61c9e2f8SAriel Constenla-Haile storageType = content.getIdentifier().getContentIdentifier() 903*61c9e2f8SAriel Constenla-Haile except Exception, e: 904*61c9e2f8SAriel Constenla-Haile text = lastException2String() 905*61c9e2f8SAriel Constenla-Haile log.error( text ) 906*61c9e2f8SAriel Constenla-Haile 907cdf0e10cSrcweir isPackage = storageType.endswith( ":uno_packages" ) 908cdf0e10cSrcweir 909cdf0e10cSrcweir try: 910cdf0e10cSrcweir# urlHelper = ctx.ServiceManager.createInstanceWithArgumentsAndContext( 911cdf0e10cSrcweir# "com.sun.star.script.provider.ScriptURIHelper", (LANGUAGENAME, storageType), ctx) 912cdf0e10cSrcweir urlHelper = MyUriHelper( ctx, storageType ) 913cdf0e10cSrcweir log.isDebugLevel() and log.debug( "got urlHelper " + str( urlHelper ) ) 914cdf0e10cSrcweir 915cdf0e10cSrcweir rootUrl = expandUri( urlHelper.getRootStorageURI() ) 916cdf0e10cSrcweir log.isDebugLevel() and log.debug( storageType + " transformed to " + rootUrl ) 917cdf0e10cSrcweir 918cdf0e10cSrcweir ucbService = "com.sun.star.ucb.SimpleFileAccess" 919cdf0e10cSrcweir sfa = ctx.ServiceManager.createInstanceWithContext( ucbService, ctx ) 920cdf0e10cSrcweir if not sfa: 921cdf0e10cSrcweir log.debug("PythonScriptProvider couldn't instantiate " +ucbService) 922cdf0e10cSrcweir raise RuntimeException( 923cdf0e10cSrcweir "PythonScriptProvider couldn't instantiate " +ucbService, self) 924cdf0e10cSrcweir self.provCtx = ProviderContext( 925*61c9e2f8SAriel Constenla-Haile storageType, sfa, urlHelper, ScriptContext( uno.getComponentContext(), doc, inv ) ) 926cdf0e10cSrcweir if isPackage: 927cdf0e10cSrcweir mapPackageName2Path = getPackageName2PathMap( sfa, storageType ) 928cdf0e10cSrcweir self.provCtx.setPackageAttributes( mapPackageName2Path , rootUrl ) 929cdf0e10cSrcweir self.dirBrowseNode = PackageBrowseNode( self.provCtx, LANGUAGENAME, rootUrl ) 930cdf0e10cSrcweir else: 931cdf0e10cSrcweir self.dirBrowseNode = DirBrowseNode( self.provCtx, LANGUAGENAME, rootUrl ) 932cdf0e10cSrcweir 933cdf0e10cSrcweir except Exception, e: 934cdf0e10cSrcweir text = lastException2String() 935cdf0e10cSrcweir log.debug( "PythonScriptProvider could not be instantiated because of : " + text ) 936cdf0e10cSrcweir raise e 937cdf0e10cSrcweir 938cdf0e10cSrcweir def getName( self ): 939cdf0e10cSrcweir return self.dirBrowseNode.getName() 940cdf0e10cSrcweir 941cdf0e10cSrcweir def getChildNodes( self ): 942cdf0e10cSrcweir return self.dirBrowseNode.getChildNodes() 943cdf0e10cSrcweir 944cdf0e10cSrcweir def hasChildNodes( self ): 945cdf0e10cSrcweir return self.dirBrowseNode.hasChildNodes() 946cdf0e10cSrcweir 947cdf0e10cSrcweir def getType( self ): 948cdf0e10cSrcweir return self.dirBrowseNode.getType() 949cdf0e10cSrcweir 950cdf0e10cSrcweir def getScript( self, uri ): 951cdf0e10cSrcweir log.debug( "DirBrowseNode getScript " + uri + " invoked" ) 952cdf0e10cSrcweir 953cdf0e10cSrcweir raise IllegalArgumentException( "DirBrowseNode couldn't instantiate script " + uri , self , 0 ) 954cdf0e10cSrcweir 955cdf0e10cSrcweir def getScript( self, scriptUri ): 956cdf0e10cSrcweir try: 957cdf0e10cSrcweir log.isDebugLevel() and log.debug( "getScript " + scriptUri + " invoked") 958cdf0e10cSrcweir 959cdf0e10cSrcweir storageUri = self.provCtx.getStorageUrlFromPersistentUrl( 960cdf0e10cSrcweir self.provCtx.uriHelper.getStorageURI(scriptUri) ); 961cdf0e10cSrcweir log.isDebugLevel() and log.debug( "getScript: storageUri = " + storageUri) 962cdf0e10cSrcweir fileUri = storageUri[0:storageUri.find( "$" )] 963cdf0e10cSrcweir funcName = storageUri[storageUri.find( "$" )+1:len(storageUri)] 964cdf0e10cSrcweir 965cdf0e10cSrcweir mod = self.provCtx.getModuleByUrl( fileUri ) 966cdf0e10cSrcweir log.isDebugLevel() and log.debug( " got mod " + str(mod) ) 967cdf0e10cSrcweir 968cdf0e10cSrcweir func = mod.__dict__[ funcName ] 969cdf0e10cSrcweir 970cdf0e10cSrcweir log.isDebugLevel() and log.debug( "got func " + str( func ) ) 971cdf0e10cSrcweir return PythonScript( func, mod ) 972cdf0e10cSrcweir except Exception, e: 973cdf0e10cSrcweir text = lastException2String() 974cdf0e10cSrcweir log.error( text ) 975cdf0e10cSrcweir raise ScriptFrameworkErrorException( text, self, scriptUri, LANGUAGENAME, 0 ) 976cdf0e10cSrcweir 977cdf0e10cSrcweir 978cdf0e10cSrcweir # XServiceInfo 979cdf0e10cSrcweir def getSupportedServices( self ): 980cdf0e10cSrcweir return g_ImplementationHelper.getSupportedServices(g_implName) 981cdf0e10cSrcweir 982cdf0e10cSrcweir def supportsService( self, ServiceName ): 983cdf0e10cSrcweir return g_ImplementationHelper.supportsService( g_implName, ServiceName ) 984cdf0e10cSrcweir 985cdf0e10cSrcweir def getImplementationName(self): 986cdf0e10cSrcweir return g_implName 987cdf0e10cSrcweir 988cdf0e10cSrcweir def getByName( self, name ): 989cdf0e10cSrcweir log.debug( "getByName called" + str( name )) 990cdf0e10cSrcweir return None 991cdf0e10cSrcweir 992cdf0e10cSrcweir 993cdf0e10cSrcweir def getElementNames( self ): 994cdf0e10cSrcweir log.debug( "getElementNames called") 995cdf0e10cSrcweir return () 996cdf0e10cSrcweir 997cdf0e10cSrcweir def hasByName( self, name ): 998cdf0e10cSrcweir try: 999cdf0e10cSrcweir log.debug( "hasByName called " + str( name )) 1000cdf0e10cSrcweir uri = expandUri(name) 1001cdf0e10cSrcweir ret = self.provCtx.isUrlInPackage( uri ) 1002cdf0e10cSrcweir log.debug( "hasByName " + uri + " " +str( ret ) ) 1003cdf0e10cSrcweir return ret 1004cdf0e10cSrcweir except Exception, e: 1005cdf0e10cSrcweir text = lastException2String() 1006cdf0e10cSrcweir log.debug( "Error in hasByName:" + text ) 1007cdf0e10cSrcweir return False 1008cdf0e10cSrcweir 1009cdf0e10cSrcweir def removeByName( self, name ): 1010cdf0e10cSrcweir log.debug( "removeByName called" + str( name )) 1011cdf0e10cSrcweir uri = expandUri( name ) 1012cdf0e10cSrcweir if self.provCtx.isUrlInPackage( uri ): 1013cdf0e10cSrcweir self.provCtx.removePackageByUrl( uri ) 1014cdf0e10cSrcweir else: 1015cdf0e10cSrcweir log.debug( "removeByName unknown uri " + str( name ) + ", ignoring" ) 1016cdf0e10cSrcweir raise NoSuchElementException( uri + "is not in package" , self ) 1017cdf0e10cSrcweir log.debug( "removeByName called" + str( uri ) + " successful" ) 1018cdf0e10cSrcweir 1019cdf0e10cSrcweir def insertByName( self, name, value ): 1020cdf0e10cSrcweir log.debug( "insertByName called " + str( name ) + " " + str( value )) 1021cdf0e10cSrcweir uri = expandUri( name ) 1022cdf0e10cSrcweir if isPyFileInPath( self.provCtx.sfa, uri ): 1023cdf0e10cSrcweir self.provCtx.addPackageByUrl( uri ) 1024cdf0e10cSrcweir else: 1025cdf0e10cSrcweir # package is no python package ... 1026cdf0e10cSrcweir log.debug( "insertByName: no python files in " + str( uri ) + ", ignoring" ) 1027cdf0e10cSrcweir raise IllegalArgumentException( uri + " does not contain .py files", self, 1 ) 1028cdf0e10cSrcweir log.debug( "insertByName called " + str( uri ) + " successful" ) 1029cdf0e10cSrcweir 1030cdf0e10cSrcweir def replaceByName( self, name, value ): 1031cdf0e10cSrcweir log.debug( "replaceByName called " + str( name ) + " " + str( value )) 1032cdf0e10cSrcweir removeByName( name ) 1033cdf0e10cSrcweir insertByName( name ) 1034cdf0e10cSrcweir log.debug( "replaceByName called" + str( uri ) + " successful" ) 1035cdf0e10cSrcweir 1036cdf0e10cSrcweir def getElementType( self ): 1037cdf0e10cSrcweir log.debug( "getElementType called" ) 1038cdf0e10cSrcweir return uno.getTypeByName( "void" ) 1039cdf0e10cSrcweir 1040cdf0e10cSrcweir def hasElements( self ): 1041cdf0e10cSrcweir log.debug( "hasElements got called") 1042cdf0e10cSrcweir return False 1043cdf0e10cSrcweir 1044cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \ 1045cdf0e10cSrcweir PythonScriptProvider,g_implName, \ 1046cdf0e10cSrcweir ("com.sun.star.script.provider.LanguageScriptProvider", 1047cdf0e10cSrcweir "com.sun.star.script.provider.ScriptProviderFor"+ LANGUAGENAME,),) 1048cdf0e10cSrcweir 1049cdf0e10cSrcweir 1050cdf0e10cSrcweirlog.debug( "pythonscript finished intializing" ) 1051cdf0e10cSrcweir 1052