diff options
author | Andrea Gelmini <andrea.gelmini@gelma.net> | 2015-09-22 11:35:52 +0200 |
---|---|---|
committer | Noel Grandin <noelgrandin@gmail.com> | 2015-09-30 06:56:42 +0000 |
commit | 0e5318aa75b615b35a3d07172bdeb26eb5acfdd9 (patch) | |
tree | c45ec64c50fc982a0d515c5d2c18cc147dcf680c /scripting | |
parent | b78a2bc3d427e083fe309d0d8ee5e9e74f7f5119 (diff) |
Script: better way to detect Windows
Change-Id: I0e99443f3715cde4414d14b2fa507ecab0995c6d
Reviewed-on: https://gerrit.libreoffice.org/18937
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Tested-by: Noel Grandin <noelgrandin@gmail.com>
Diffstat (limited to 'scripting')
-rw-r--r-- | scripting/source/pyprov/pythonscript.py | 143 |
1 files changed, 72 insertions, 71 deletions
diff --git a/scripting/source/pyprov/pythonscript.py b/scripting/source/pyprov/pythonscript.py index 03f0cdeae7c8..c62c22872f9c 100644 --- a/scripting/source/pyprov/pythonscript.py +++ b/scripting/source/pyprov/pythonscript.py @@ -23,6 +23,7 @@ import os import imp import time import ast +import platform try: unicode @@ -82,18 +83,18 @@ def getLogTarget(): except: print("Exception during creation of pythonscript logfile: "+ lastException2String() + "\n, delagating log to stdout\n") return ret - + class Logger(LogLevel): def __init__(self , target ): self.target = target def isDebugLevel( self ): return self.use >= self.DEBUG - + def debug( self, msg ): if self.isDebugLevel(): self.log( self.DEBUG, msg ) - + def isErrorLevel( self ): return self.use >= self.ERROR @@ -159,12 +160,12 @@ def readTextFromStream( inputStream ): if read < BLOCK_SIZE: break return code.value - + def toIniName( str ): - # TODO: what is the official way to get to know whether i am on the windows platform ? - if( hasattr(sys , "dllhandle") ): + if platform.system() == "Windows": return str + ".ini" - return str + "rc" + else: + return str + "rc" """ definition: storageURI is the system dependent, absolute file url, where the script is stored on disk @@ -177,7 +178,7 @@ class MyUriHelper: { "share" : "vnd.sun.star.expand:$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/Scripts/python" , \ "share:uno_packages" : "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE/uno_packages", \ "user" : "vnd.sun.star.expand:${$BRAND_INI_DIR/" + toIniName( "bootstrap") + "::UserInstallation}/user/Scripts/python" , \ - "user:uno_packages" : "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages" } + "user:uno_packages" : "vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages" } self.m_uriRefFac = ctx.ServiceManager.createInstanceWithContext("com.sun.star.uri.UriReferenceFactory",ctx) if location.startswith( "vnd.sun.star.tdoc" ): self.m_baseUri = location + "/Scripts/python" @@ -186,10 +187,10 @@ class MyUriHelper: self.m_baseUri = expandUri( self.s_UriMap[location] ) self.m_scriptUriLocation = location log.debug( "initialized urihelper with baseUri="+self.m_baseUri + ",m_scriptUriLocation="+self.m_scriptUriLocation ) - + def getRootStorageURI( self ): return self.m_baseUri - + def getStorageURI( self, scriptURI ): return self.scriptURI2StorageUri(scriptURI) @@ -207,7 +208,7 @@ class MyUriHelper: "?language=" + LANGUAGENAME + "&location=" + self.m_scriptUriLocation log.debug( "converting storageURI="+storageURI + " to scriptURI=" + ret ) return ret - + def scriptURI2StorageUri( self, scriptURI ): try: myUri = self.m_uriRefFac.parse(scriptURI) @@ -220,7 +221,7 @@ class MyUriHelper: except Exception as e: log.error( "error during converting scriptURI="+scriptURI + ": " + str(e)) raise RuntimeException( "pythonscript:scriptURI2StorageUri: " + str(e), None ) - + class ModuleEntry: def __init__( self, lastRead, module ): @@ -256,14 +257,14 @@ def checkForPythonPathBesideScript( url ): if 1 == os.access( encfile(path), os.F_OK) and not path in sys.path: log.log( LogLevel.DEBUG, "adding " + path + " to sys.path" ) sys.path.append( path ) - - + + class ScriptContext(unohelper.Base): def __init__( self, ctx, doc, inv ): self.ctx = ctx self.doc = doc self.inv = inv - + # XScriptContext def getDocument(self): if self.doc: @@ -296,12 +297,12 @@ class ScriptContext(unohelper.Base): # log.debug("file " + url + " has changed, reloading") # else: # load = False -# +# # if load: # log.debug( "opening >" + url + "<" ) # # code = readTextFromStream( sfa.openFileRead( url ) ) - + # execute the module # entry = ModuleEntry( lastRead, imp.new_module("ooo_script_framework") ) # entry.module.__dict__[GLOBAL_SCRIPTCONTEXT_NAME] = g_scriptContext @@ -324,13 +325,13 @@ class ProviderContext: def getTransientPartFromUrl( self, url ): rest = url.replace( self.rootUrl , "",1 ).replace( "/","",1) return rest[0:rest.find("/")] - + def getPackageNameFromUrl( self, url ): rest = url.replace( self.rootUrl , "",1 ).replace( "/","",1) start = rest.find("/") +1 return rest[start:rest.find("/",start)] - - + + def removePackageByUrl( self, url ): items = self.mapPackageName2Path.items() for i in items: @@ -348,20 +349,20 @@ class ProviderContext: else: package = Package( (url,), transientPart) self.mapPackageName2Path[ packageName ] = package - + def isUrlInPackage( self, url ): values = self.mapPackageName2Path.values() for i in values: -# print ("checking " + url + " in " + str(i.paths)) +# print ("checking " + url + " in " + str(i.paths)) if url in i.paths: return True # print ("false") return False - + def setPackageAttributes( self, mapPackageName2Path, rootUrl ): self.mapPackageName2Path = mapPackageName2Path self.rootUrl = rootUrl - + def getPersistentUrlFromStorageUrl( self, url ): # package name is the second directory ret = url @@ -431,14 +432,14 @@ class ProviderContext: log.debug( "file " + url + " has changed, reloading" ) else: load = False - + if load: log.debug( "opening >" + url + "<" ) - + src = readTextFromStream( self.sfa.openFileRead( url ) ) checkForPythonPathBesideScript( url[0:url.rfind('/')] ) - src = ensureSourceState( src ) - + src = ensureSourceState( src ) + # execute the module entry = ModuleEntry( lastRead, imp.new_module("ooo_script_framework") ) entry.module.__dict__[GLOBAL_SCRIPTCONTEXT_NAME] = self.scriptContext @@ -453,14 +454,14 @@ class ProviderContext: self.modules[ url ] = entry log.debug( "mapped " + url + " to " + str( entry.module ) ) return entry.module - + #-------------------------------------------------- def isScript( candidate ): ret = False if isinstance( candidate, type(isScript) ): ret = True return ret - + #------------------------------------------------------- class ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, XActionListener ): def __init__( self, provCtx, uri, fileName, funcName ): @@ -468,7 +469,7 @@ class ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, self.funcName = funcName self.provCtx = provCtx self.uri = uri - + def getName( self ): return self.funcName @@ -477,7 +478,7 @@ class ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, def hasChildNodes(self): return False - + def getType( self): return SCRIPT @@ -489,19 +490,19 @@ class ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, self.provCtx.getPersistentUrlFromStorageUrl( self.uri + "$" + self.funcName ) ) elif name == "Editable" and ENABLE_EDIT_DIALOG: ret = not self.provCtx.sfa.isReadOnly( self.uri ) - + log.debug( "ScriptBrowseNode.getPropertyValue called for " + name + ", returning " + str(ret) ) except: log.error( "ScriptBrowseNode.getPropertyValue error " + lastException2String()) raise - + return ret def setPropertyValue( self, name, value ): log.debug( "ScriptBrowseNode.setPropertyValue called " + name + "=" +str(value ) ) def getPropertySetInfo( self ): log.debug( "ScriptBrowseNode.getPropertySetInfo called " ) return None - + def getIntrospection( self ): return None @@ -540,12 +541,12 @@ class ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, values = mod.__dict__.get( CALLABLE_CONTAINER_NAME , None ) if not values: values = mod.__dict__.values() - + for i in values: if isScript( i ): i() break - + elif event.ActionCommand == "Save": toWrite = uno.ByteSequence( str( @@ -561,9 +562,9 @@ class ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, # text = self.editor.getControl("EditorTextField").getText() # log.debug("Would save: " + text) except: - # TODO: add an error box here ! + # TODO: add an error box here! log.error( lastException2String() ) - + def setValue( self, name, value ): return None @@ -577,7 +578,7 @@ class ScriptBrowseNode( unohelper.Base, XBrowseNode , XPropertySet, XInvocation, def hasProperty( self, name ): return False - + #------------------------------------------------------- class FileBrowseNode( unohelper.Base, XBrowseNode ): def __init__( self, provCtx, uri , name ): @@ -585,15 +586,15 @@ class FileBrowseNode( unohelper.Base, XBrowseNode ): self.uri = uri self.name = name self.funcnames = None - + def getName( self ): return self.name - + def getChildNodes(self): ret = () try: self.funcnames = self.provCtx.getFuncsByUrl( self.uri ) - + scriptNodeList = [] for i in self.funcnames: scriptNodeList.append( @@ -612,11 +613,11 @@ class FileBrowseNode( unohelper.Base, XBrowseNode ): return len(self.getChildNodes()) > 0 except: return False - + def getType( self): return CONTAINER - + class DirBrowseNode( unohelper.Base, XBrowseNode ): def __init__( self, provCtx, name, rootUrl ): @@ -661,13 +662,13 @@ class DirBrowseNode( unohelper.Base, XBrowseNode ): class ManifestHandler( XDocumentHandler, unohelper.Base ): def __init__( self, rootUrl ): self.rootUrl = rootUrl - + def startDocument( self ): self.urlList = [] - + def endDocument( self ): pass - + def startElement( self , name, attlist): if name == "manifest:file-entry": if attlist.getValueByName( "manifest:media-type" ) == "application/vnd.sun.star.framework-script": @@ -699,11 +700,11 @@ def isPyFileInPath( sfa, path ): break return ret -# extracts META-INF directory from +# extracts META-INF directory from def getPathsFromPackage( rootUrl, sfa ): ret = () try: - fileUrl = rootUrl + "/META-INF/manifest.xml" + fileUrl = rootUrl + "/META-INF/manifest.xml" inputStream = sfa.openFileRead( fileUrl ) parser = uno.getComponentContext().ServiceManager.createInstance( "com.sun.star.xml.sax.Parser" ) handler = ManifestHandler( rootUrl ) @@ -718,7 +719,7 @@ def getPathsFromPackage( rootUrl, sfa ): log.debug( "getPathsFromPackage " + fileUrl + " Exception: " +text ) pass return ret - + class Package: def __init__( self, paths, transientPathElement ): @@ -734,8 +735,8 @@ class DummyInteractionHandler( unohelper.Base, XInteractionHandler ): class DummyProgressHandler( unohelper.Base, XProgressHandler ): def __init__( self ): pass - - def push( self,status ): + + def push( self,status ): log.debug( "pythonscript: DummyProgressHandler.push " + str( status ) ) def update( self,status ): log.debug( "pythonscript: DummyProgressHandler.update " + str( status ) ) @@ -771,12 +772,12 @@ def getModelFromDocUrl(ctx, url): p = Property() p.Name = "DocumentModel" p.Handle = -1 - + c = Command() c.Handle = -1 c.Name = "getPropertyValues" c.Argument = uno.Any("[]com.sun.star.beans.Property", (p,)) - + env = CommandEnvironment() try: ret = content.execute(c, 0, env) @@ -899,7 +900,7 @@ def expandUri( uri ): if uri.startswith( "file:" ): uri = uno.absolutize("",uri) # necessary to get rid of .. in uri return uri - + #-------------------------------------------------------------- class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameContainer): def __init__( self, ctx, *args ): @@ -938,7 +939,7 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC # "com.sun.star.script.provider.ScriptURIHelper", (LANGUAGENAME, storageType), ctx) urlHelper = MyUriHelper( ctx, storageType ) log.debug( "got urlHelper " + str( urlHelper ) ) - + rootUrl = expandUri( urlHelper.getRootStorageURI() ) log.debug( storageType + " transformed to " + rootUrl ) @@ -956,7 +957,7 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC self.dirBrowseNode = PackageBrowseNode( self.provCtx, LANGUAGENAME, rootUrl ) else: self.dirBrowseNode = DirBrowseNode( self.provCtx, LANGUAGENAME, rootUrl ) - + except Exception as e: text = lastException2String() log.debug( "PythonScriptProvider could not be instantiated because of : " + text ) @@ -966,7 +967,7 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC return self.dirBrowseNode.getName() def getChildNodes( self ): - return self.dirBrowseNode.getChildNodes() + return self.dirBrowseNode.getChildNodes() def hasChildNodes( self ): return self.dirBrowseNode.hasChildNodes() @@ -976,22 +977,22 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC def getScript( self, uri ): log.debug( "DirBrowseNode getScript " + uri + " invoked" ) - + raise IllegalArgumentException( "DirBrowseNode couldn't instantiate script " + uri , self , 0 ) def getScript( self, scriptUri ): try: log.debug( "getScript " + scriptUri + " invoked") - + storageUri = self.provCtx.getStorageUrlFromPersistentUrl( self.provCtx.uriHelper.getStorageURI(scriptUri) ); log.debug( "getScript: storageUri = " + storageUri) fileUri = storageUri[0:storageUri.find( "$" )] - funcName = storageUri[storageUri.find( "$" )+1:len(storageUri)] - + funcName = storageUri[storageUri.find( "$" )+1:len(storageUri)] + mod = self.provCtx.getModuleByUrl( fileUri ) log.debug( " got mod " + str(mod) ) - + func = mod.__dict__[ funcName ] log.debug( "got func " + str( func ) ) @@ -1000,7 +1001,7 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC text = lastException2String() log.error( text ) raise ScriptFrameworkErrorException( text, self, scriptUri, LANGUAGENAME, 0 ) - + # XServiceInfo def getSupportedServices( self ): @@ -1016,11 +1017,11 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC log.debug( "getByName called" + str( name )) return None - + def getElementNames( self ): log.debug( "getElementNames called") return () - + def hasByName( self, name ): try: log.debug( "hasByName called " + str( name )) @@ -1042,7 +1043,7 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC log.debug( "removeByName unknown uri " + str( name ) + ", ignoring" ) raise NoSuchElementException( uri + "is not in package" , self ) log.debug( "removeByName called" + str( uri ) + " successful" ) - + def insertByName( self, name, value ): log.debug( "insertByName called " + str( name ) + " " + str( value )) uri = expandUri( name ) @@ -1064,13 +1065,13 @@ class PythonScriptProvider( unohelper.Base, XBrowseNode, XScriptProvider, XNameC def getElementType( self ): log.debug( "getElementType called" ) return uno.getTypeByName( "void" ) - + def hasElements( self ): log.debug( "hasElements got called") return False - + g_ImplementationHelper.addImplementation( \ - PythonScriptProvider,g_implName, \ + PythonScriptProvider,g_implName, \ ("com.sun.star.script.provider.LanguageScriptProvider", "com.sun.star.script.provider.ScriptProviderFor"+ LANGUAGENAME,),) |