pythonscript.py 42 KB

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