24from urllib.parse
import unquote
26from com.sun.star.loader
import XImplementationLoader
29MODULE_PROTOCOL =
"vnd.openoffice.pymodule:"
32g_supportedServices =
"com.sun.star.loader.Python",
33g_implementationName =
"org.openoffice.comp.pyuno.Loader"
36 nColon = url.find(
":" )
38 raise RuntimeException(
"PythonLoader: No protocol in url " + url,
None )
39 return url[0:nColon], url[nColon+1:len(url)]
41g_loadedComponents = {}
45 print(b
"checking for existence of " +
encfile( path ))
46 if 1 == os.access(
encfile( path ), os.F_OK)
and not path
in sys.path:
48 print(b
"adding " +
encfile( path ) + b
" to sys.path")
49 sys.path.append( path )
52 if 1 == os.access(
encfile( path ), os.F_OK)
and not path
in sys.path:
54 print(b
"adding " +
encfile( path ) + b
" to sys.path")
55 sys.path.append( path )
58 return uni.encode( sys.getfilesystemencoding())
63 print(
"pythonloader.Loader ctor")
68 print(
"pythonloader: interpreting url " + url)
69 protocol, dependent =
splitUrl( url )
70 if "vnd.sun.star.expand" == protocol:
71 exp = self.
ctx.getValueByName(
"/singletons/com.sun.star.util.theMacroExpander" )
72 url = exp.expandMacros(unquote(dependent))
76 print(
"pythonloader: after expansion " + protocol +
":" + dependent)
79 if "file" == protocol:
84 mod = g_loadedComponents.get( url )
86 mod = types.ModuleType(
"uno_component")
94 with open( filename, encoding=
'utf_8' )
as fileHandle:
95 src = fileHandle.read().replace(
"\r",
"")
96 if not src.endswith(
"\n" ):
100 codeobject = compile( src,
encfile(filename),
"exec" )
101 mod.__file__ = filename
102 exec(codeobject, mod.__dict__)
103 g_loadedComponents[url] = mod
105 elif "vnd.openoffice.pymodule" == protocol:
106 nSlash = dependent.rfind(
'/')
109 dependent = dependent[nSlash+1:len(dependent)]
110 if not path
in sys.path:
111 sys.path.append( path )
112 mod = __import__( dependent )
113 path_component, dot, rest = dependent.partition(
'.')
115 path_component, dot, rest = rest.partition(
'.')
116 mod = getattr(mod, path_component)
120 print(
"Unknown protocol '" + protocol +
"'");
121 raise RuntimeException(
"PythonLoader: Unknown protocol " +
122 protocol +
" in url " +url, self )
123 except Exception
as e:
125 print (
"Python import exception " + str(type(e)) +
126 " message " + str(e) +
" args " + str(e.args));
127 raise RuntimeException(
"Couldn't load " + url +
" for reason " + str(e),
None )
130 def activate( self, implementationName, dummy, locationUrl, regKey ):
132 print(
"pythonloader.Loader.activate")
135 implHelper = mod.__dict__.get(
"g_ImplementationHelper" ,
None )
137 print (
"Fetched ImplHelper as " + str(implHelper))
138 if implHelper
is None:
139 return mod.getComponentFactory( implementationName, self.
ctx.ServiceManager, regKey )
141 return implHelper.getComponentFactory( implementationName,regKey,self.
ctx.ServiceManager)
145 print(
"pythonloader.Loader.writeRegistryInfo" )
148 implHelper = mod.__dict__.get(
"g_ImplementationHelper" ,
None )
149 if implHelper
is None:
150 return mod.writeRegistryInfo( self.
ctx.ServiceManager, regKey )
152 return implHelper.writeRegistryInfo( regKey, self.
ctx.ServiceManager )
155 return g_implementationName
161 return g_supportedServices
def activate(self, implementationName, dummy, locationUrl, regKey)
def getSupportedServiceNames(self)
def supportsService(self, ServiceName)
def writeRegistryInfo(self, regKey, dummy, locationUrl)
def getImplementationName(self)
def getModuleFromUrl(self, url)
def checkForPythonPathBesideComponent(url)
def fileUrlToSystemPath(url)
def absolutize(path, relativeUrl)