2 Modulo di debug per LeenO
3 permette il debug attraverso l' IDE Eric6 (o simili)
14 from os
import listdir
15 from os.path
import isfile, join
18 from com.sun.star.connection
import NoConnectException
22 if sys.platform ==
'linux' or sys.platform ==
'darwin':
23 _sofficePath =
'/usr/lib/libreoffice/program'
26 _sofficePath =
'C:\\Program Files\\LibreOffice\\program'
29 OPENOFFICE_PORT = 8100
30 OPENOFFICE_PATH = _sofficePath
31 OPENOFFICE_BIN = os.path.join(OPENOFFICE_PATH, calc)
32 OPENOFFICE_LIBPATH = OPENOFFICE_PATH
36 Start, stop, and connect to OpenOffice.
39 """ Create OORunner that connects on the specified port. """
45 Connect to OpenOffice.
46 If a connection cannot be established try to start OpenOffice.
48 localContext = uno.getComponentContext()
49 resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext)
56 context = resolver.resolve(
"uno:socket,host=localhost,port=%d;urp;StarOffice.ComponentContext" % self.
port)
58 except NoConnectException:
74 raise Exception(
"Failed to connect to OpenOffice on port %d" % self.
port)
76 desktop = context.ServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", context)
79 raise Exception(
"Failed to create OpenOffice desktop on port %d" % self.
port)
82 _started_desktops[self.
port] = desktop
84 return {
'context': context,
'desktop': desktop}
89 Start a headless instance of OpenOffice.
91 args = [OPENOFFICE_BIN,
92 '--accept=socket,host=localhost,port=%d;urp;StarOffice.ServiceManager' % self.
port,
94 '--nofirststartwizard',
98 env = os.environ.copy()
103 child = subprocess.Popen(args=args, env=env, start_new_session=
False)
104 except Exception
as e:
105 raise Exception(
"Failed to start OpenOffice on port %d: %s" % (self.
port, e))
109 raise Exception(
"Failed to start OpenOffice on port %d" % self.
port)
118 if _started_desktops.get(self.
port):
119 _started_desktops[self.
port].terminate()
120 del _started_desktops[self.
port]
127 _started_desktops = {}
129 def _shutdown_desktops():
130 """ Shutdown all OpenOffice desktops that were started by the program. """
131 for port, desktop
in _started_desktops.items():
138 atexit.register(_shutdown_desktops)
141 if type(__builtins__) == type(sys):
142 bDict = __builtins__.__dict__
149 This function reload all Leeno modules found in pythonpath
152 myPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
153 myPath = os.path.join(myPath,
"pythonpath")
156 pythonFiles = [f[: -3]
for f
in listdir(myPath)
if isfile(join(myPath, f))
and f.endswith(
".py")]
157 for f
in pythonFiles:
158 print(
"Loading module:", f)
159 module = importlib.import_module(f)
165 importlib.reload(module)
173 lo = runner.connect()
176 leenoPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
177 sys.path.append(leenoPath)
178 leenoPath = os.path.join(leenoPath,
"pythonpath")
179 sys.path.append(leenoPath)
182 frames = lo[
'desktop'].getFrames()
184 frames[0]. activate()
187 Poi sembra strano quando dico che il python è stato studiato con i piedi...
189 By default, when in the __main__ module, __builtins__ is the built-in module __builtin__ (note: no 's'); when in any other module,
190 __builtins__ is an alias for the dictionary of the __builtin__ module itself.
191 Note that in Python3, the module __builtin__ has been renamed to builtins to avoid some of this confusion.
195 bDict[
'__global_context__'] = lo[
'context']
200 desktop = lo[
'desktop']
205 url = uno.systemPathToFileUrl(filename)
206 oDoc = desktop.loadComponentFromURL(url,
"_blank", 0, tuple())
210 from io
import StringIO
211 import xml.etree.ElementTree
as ET
216 filename =
"/storage/Scaricati/COMPUTI_METRICI/LEENO/TESTS/TestPdfExport.ods"
219 sheet = oDoc.Sheets[0]
220 pageStyleName = sheet.PageStyle
221 pageStyles = oDoc.StyleFamilies.getByName(
'PageStyles')
222 pageStyle = pageStyles.getByName(pageStyleName)
223 footer = pageStyle.RightPageFooterContent
224 rightText = footer.RightText
227 pos = rightText.String.find(pattern)
228 cursor = rightText.createTextCursor()
229 cursor.collapseToStart()
230 cursor.goRight(pos,
False)
231 cursor.goRight(len(pattern),
True)
233 oField = oDoc.createInstance(
"com.sun.star.text.TextField.PageCount")
234 cursor.collapseToStart()
235 rightText.insertTextContent(cursor, oField,
False)
246 pageStyle.RightPageFooterContent = footer