LeenO computo metrico con LibreOffice  3.22.0
Il software libero per la gestione di computi metrici e contabilità lavori.
LeenoDispatcher.py
Vai alla documentazione di questo file.
1 '''
2  LeenO menu and basic function dispatcher
3 '''
4 
5 import sys
6 import os
7 import inspect
8 import importlib
9 
10 from os import listdir
11 from os.path import isfile, join
12 
13 import unohelper
14 from com.sun.star.task import XJobExecutor
15 import Dialogs
16 
17 import uno
18 import traceback
19 from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
20 def msgbox(*, Title='Errore interno', Message=''):
21  """ Create message box
22  type_msg: infobox, warningbox, errorbox, querybox, messbox
23  http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1awt_1_1XMessageBoxFactory.html
24  """
25  ctx = uno.getComponentContext()
26  sm = ctx.getServiceManager()
27  toolkit = sm.createInstance('com.sun.star.awt.Toolkit')
28  parent = toolkit.getDesktopWindow()
29  buttons=MSG_BUTTONS.BUTTONS_OK
30  type_msg='errorbox'
31  mb = toolkit.createMessageBox(parent, type_msg, buttons, Title, str(Message))
32  return mb.execute()
33 
34 # set this to 1 to enable debugging
35 # set to 0 before deploying
36 ENABLE_DEBUG = 1
37 
38 # set this one to 0 for deploy mode
39 # leave to 1 if you want to disable python cache
40 # to be able to modify and run installed extension
41 DISABLE_CACHE = 1
42 
43 if ENABLE_DEBUG == 1:
44  pass
45 
46 
47 def loVersion():
48  '''
49  Legge il numero di versione di LibreOffice.
50  '''
51  aConfigProvider = uno.getComponentContext().ServiceManager.createInstance("com.sun.star.configuration.ConfigurationProvider")
52  arg = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
53  arg.Name = "nodepath"
54  arg.Value = '/org.openoffice.Setup/Product'
55  return aConfigProvider.createInstanceWithArguments(
56  "com.sun.star.configuration.ConfigurationAccess",
57  (arg, )).ooSetupVersionAboutBox
58 
60  '''
61  This function should fix python path adding it to current sys path
62  if not already there
63  Useless here, just kept for reference
64  '''
65  # dirty trick to have pythonpath added if missing
66  myPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
67  myPath = os.path.join(myPath, "pythonpath")
68  if myPath not in sys.path:
69  sys.path.append(myPath)
70 
71 
73  '''
74  This function reload all Leeno modules found in pythonpath
75  '''
76  # get our pythonpath
77  myPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
78  myPath = os.path.join(myPath, "pythonpath")
79 
80  # we need a listing of modules. We look at pythonpath ones
81  pythonFiles = [f[: -3] for f in listdir(myPath) if isfile(join(myPath, f)) and f.endswith(".py")]
82  for f in pythonFiles:
83  #~ print("Loading module:", f)
84  module = importlib.import_module(f)
85 
86  # reload the module
87  importlib.reload(module)
88 
89 
90 class Dispatcher(unohelper.Base, XJobExecutor):
91  '''
92  LeenO menu and basic function dispatcher
93  '''
94 
95  def __init__(self, ctx, *args):
96 
97  # CTX is XComponentContext - we store it
98  self.ComponentContext = ctx
99 
100  # store any passed arg
101  self.args = args
102 
103  # just in case...
104  fixPythonPath()
105 
106  def trigger(self, arg):
107  '''
108  This function gets called when a menu item is selected
109  or when a basic function calls PyScript()
110  '''
111  try:
112  # reload all Leeno Modules
113  if DISABLE_CACHE != 0:
115 
116  # menu items are passed as module.function
117  # so split them in 2 strings
118  ModFunc = arg.split('.')
119 
120  # locate the module from its name and check it
121  module = importlib.import_module(ModFunc[0])
122  if module is None:
123  print("Module '", ModFunc[0], "' not found")
124  return
125 
126  # reload the module if we don't want the cache
127  # if DISABLE_CACHE != 0:
128  # importlib.reload(module)
129 
130  # locate the function from its name and check it
131  func = getattr(module, ModFunc[1])
132  if func is None:
133  print("Function '", ModFunc[1], "' not found in Module '", ModFunc[0], "'")
135  Title="Errore interno",
136  Text=f"Funzione '{ModFunc[1]}' non trovata nel modulo '{ModFunc[0]}'")
137  return
138 
139  # call the handler, depending of number of arguments
140  if len(self.args) == 0:
141  func()
142  else:
143  func(self.args)
144 
145  except Exception as e:
146  # msg = traceback.format_exc()
147 
148 # Aggiunge info generiche su SO, LO e LeenO
149  pir = uno.getComponentContext().getValueByName(
150  '/singletons/com.sun.star.deployment.PackageInformationProvider')
151  expath = pir.getPackageLocation('org.giuseppe-vizziello.leeno')
152  if os.altsep:
153  code_file = uno.fileUrlToSystemPath(expath + os.altsep +
154  'leeno_version_code')
155  else:
156  code_file = uno.fileUrlToSystemPath(expath + os.sep +
157  'leeno_version_code')
158  f = open(code_file, 'r')
159  msg = "OS: " + sys.platform + ' / LibreOffice-' + loVersion() +' / '+ f.readline() + "\n\n"
160 #
161 
162  print("sys.exc_info:", sys.exc_info())
163  sysinfo = sys.exc_info()
164  exceptionClass = sysinfo[0].__name__
165  msg += str(sysinfo[1])
166  if msg == '-1' or msg == '':
167  msg += str(sysinfo[0])
168  if msg != '':
169  msg += '\n\n'
170  tb = sysinfo[2]
171  tbInfo = traceback.extract_tb(tb)[-1]
172  function = tbInfo.name
173  line = tbInfo.lineno
174  file = os.path.split(tbInfo.filename)[1]
175  msg = (
176  msg +
177  "File: '" + file + "'\n" +
178  "Line: '" + str(line) + "'\n" +
179  "Function: '" + function + "'\n")
180  msg += "-" * 30 + "\n"
181  msg += "BACKTRACE:\n"
182  for bkInfo in traceback.extract_tb(tb):
183  function = bkInfo.name
184  line = str(bkInfo.lineno)
185  file = os.path.split(bkInfo.filename)[1]
186  msg += f"File:{file}, Line:{line}, Function:{function}\n"
187  msg += "\n\n"
188 
189  Dialogs.Exclamation(Title="Errore interno", Text=msg)
190 
191 
192 g_ImplementationHelper = unohelper.ImplementationHelper()
193 g_ImplementationHelper.addImplementation(
194  Dispatcher,
195  "org.giuseppe-vizziello.leeno.dispatcher",
196  ("com.sun.star.task.Job",),)
python.LeenoDispatcher.Dispatcher.__init__
def __init__(self, ctx, *args)
Definition: LeenoDispatcher.py:95
python.LeenoDispatcher.Dispatcher.args
args
Definition: LeenoDispatcher.py:101
python.LeenoDispatcher.Dispatcher.ComponentContext
ComponentContext
Definition: LeenoDispatcher.py:98
python.LeenoDispatcher.fixPythonPath
def fixPythonPath()
Definition: LeenoDispatcher.py:59
python.LeenoDispatcher.reloadLeenoModules
def reloadLeenoModules()
Definition: LeenoDispatcher.py:72
python.LeenoDispatcher.Dispatcher
Definition: LeenoDispatcher.py:90
Dialogs.Exclamation
def Exclamation(*Title='', Text='')
Definition: Dialogs.py:2536
python.LeenoDispatcher.msgbox
def msgbox(*Title='Errore interno', Message='')
Definition: LeenoDispatcher.py:20
python.LeenoDispatcher.loVersion
def loVersion()
Definition: LeenoDispatcher.py:47
python.LeenoDispatcher.Dispatcher.trigger
def trigger(self, arg)
Definition: LeenoDispatcher.py:106