| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # vim: ts=4:sw=4 |
|---|
| 3 | # This file is part of LOME. |
|---|
| 4 | # |
|---|
| 5 | # LOME is free software: you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation, either version 3 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # LOME is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with LOME. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | # |
|---|
| 18 | import sys |
|---|
| 19 | import os |
|---|
| 20 | import threading |
|---|
| 21 | from threading import Event |
|---|
| 22 | |
|---|
| 23 | sys.path.append( os.path.join( sys.path[0], 'common')) |
|---|
| 24 | sys.path.append( os.path.join( sys.path[0], 'common', 'szobjects')) |
|---|
| 25 | sys.path.append( os.path.join( sys.path[0], 'common', 'drawobjects')) |
|---|
| 26 | sys.path.append( os.path.join( sys.path[0], 'common', 'lodb')) |
|---|
| 27 | sys.path.append( os.path.join( sys.path[0], 'common', 'mesh')) |
|---|
| 28 | sys.path.append( os.path.join( sys.path[0], 'lotatc_server')) |
|---|
| 29 | sys.path.append( os.path.join( sys.path[0], 'lotatc_client')) |
|---|
| 30 | sys.path.append( os.path.join( sys.path[0], 'loplug', 'server')) |
|---|
| 31 | sys.path.append( os.path.join( sys.path[0], 'loplug', 'plugins')) |
|---|
| 32 | sys.path.append( os.path.join( sys.path[0], 'loplug', 'plugins', 'common')) |
|---|
| 33 | |
|---|
| 34 | from os.path import join, dirname |
|---|
| 35 | from PyQt4 import uic |
|---|
| 36 | from PyQt4.QtGui import * |
|---|
| 37 | from PyQt4.QtCore import * |
|---|
| 38 | |
|---|
| 39 | try: |
|---|
| 40 | import logging.config |
|---|
| 41 | # Set logging config |
|---|
| 42 | logging.config.fileConfig('logging.conf') |
|---|
| 43 | except: |
|---|
| 44 | # Set logging config |
|---|
| 45 | logging.basicConfig(level=logging.DEBUG, format='%(name)s: %(message)s' |
|---|
| 46 | ,filename='loplug.log', filemode='w' |
|---|
| 47 | ) |
|---|
| 48 | |
|---|
| 49 | from loplugRouter import PlugRouter |
|---|
| 50 | from pagesmanager import * |
|---|
| 51 | from lotatc_ts import * |
|---|
| 52 | from szobjects import * |
|---|
| 53 | |
|---|
| 54 | # from lua_server import LuaServer |
|---|
| 55 | # from item import Item |
|---|
| 56 | # from pgdatas import * |
|---|
| 57 | |
|---|
| 58 | from constants import * |
|---|
| 59 | from plugins_configuration import * |
|---|
| 60 | from configuration import * |
|---|
| 61 | from about import * |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | CONFIG_FILE="loplug.cfg" |
|---|
| 65 | #- Main window |
|---|
| 66 | g_window = None |
|---|
| 67 | g_pathname = None |
|---|
| 68 | |
|---|
| 69 | #---------------------------------------------------------------- |
|---|
| 70 | ## About callback |
|---|
| 71 | def on_about(): |
|---|
| 72 | about = AboutDlg("LoPlug", g_pathname ) |
|---|
| 73 | # about.setWindowIcon(QIcon(os.path.join(g_pathname,"lotatc_client","resources", "lotatc_icon_server.png"))) |
|---|
| 74 | about.setWindowIcon(QIcon(os.path.join(g_pathname,"loplug","resources", "loplug.png"))) |
|---|
| 75 | about.exec_() |
|---|
| 76 | |
|---|
| 77 | def configure(): |
|---|
| 78 | window = PluginsConfiguration(plugins=g_window.plugins) |
|---|
| 79 | window.setWindowIcon(QIcon(os.path.join(g_pathname,"loplug","resources", "loplug.png"))) |
|---|
| 80 | window.exec_() |
|---|
| 81 | g_window.config.save_xml(CONFIG_FILE) |
|---|
| 82 | check_status() |
|---|
| 83 | |
|---|
| 84 | def check_status(): |
|---|
| 85 | g_window.statusList.clear() |
|---|
| 86 | for plug in g_window.plugins: |
|---|
| 87 | if plug.is_enable(): |
|---|
| 88 | is_working, error = plug.get_current_status() |
|---|
| 89 | item = QListWidgetItem() |
|---|
| 90 | if is_working: |
|---|
| 91 | item.setText( "%s: OK"%( plug.get_name() ) ) |
|---|
| 92 | else: |
|---|
| 93 | item.setText( "%s : %s"%( plug.get_name(), error ) ) |
|---|
| 94 | item.setBackground( QBrush( QColor(255, 0, 0) ) ) |
|---|
| 95 | g_window.statusList.addItem(item) |
|---|
| 96 | |
|---|
| 97 | def make_default_config_file(file, plug_path): |
|---|
| 98 | f = open( file, 'w' ) |
|---|
| 99 | |
|---|
| 100 | all_txt = """ <?xml version="1.0" encoding="utf-8"?> |
|---|
| 101 | <loplug> |
|---|
| 102 | %s |
|---|
| 103 | </loplug>""" |
|---|
| 104 | txt = "" |
|---|
| 105 | |
|---|
| 106 | import glob |
|---|
| 107 | raw_plugins = glob.glob( plug_path ) |
|---|
| 108 | for p in raw_plugins: |
|---|
| 109 | full_path = os.path.join( g_pathname, p ) |
|---|
| 110 | sys.path.append( os.path.dirname( full_path ) ) |
|---|
| 111 | if not "common" in p: |
|---|
| 112 | dir = os.path.basename( full_path ).replace(".pyc", "").replace(".py", "") |
|---|
| 113 | txt += """<section name="%s"> |
|---|
| 114 | </section> |
|---|
| 115 | """%(dir[0:3].upper() + dir[3:]) |
|---|
| 116 | f.write( all_txt%txt ) |
|---|
| 117 | f.close() |
|---|
| 118 | def on_about_to_quit(): |
|---|
| 119 | # Shutdown plugs |
|---|
| 120 | for plug in g_window.plugins: |
|---|
| 121 | if not plug.isRenderer(): |
|---|
| 122 | debug_logger.debug( "[LOPLUG] shutdown" + str(plug) ) |
|---|
| 123 | try: |
|---|
| 124 | plug.shutDown() |
|---|
| 125 | except: |
|---|
| 126 | import traceback |
|---|
| 127 | exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() |
|---|
| 128 | msg_a = traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback) |
|---|
| 129 | msg = "" |
|---|
| 130 | for m in msg_a: |
|---|
| 131 | msg += m |
|---|
| 132 | debug_logger.error("Cannot shutDown %s, error is:\n%s"%( str(plug), msg ) ) |
|---|
| 133 | |
|---|
| 134 | for plug in g_window.plugins: |
|---|
| 135 | if plug.isRenderer(): |
|---|
| 136 | debug_logger.debug( "[LOPLUG] shutdown " + str(plug) ) |
|---|
| 137 | try: |
|---|
| 138 | plug.shutDown() |
|---|
| 139 | except: |
|---|
| 140 | import traceback |
|---|
| 141 | exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() |
|---|
| 142 | msg_a = traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback) |
|---|
| 143 | msg = "" |
|---|
| 144 | for m in msg_a: |
|---|
| 145 | msg += m |
|---|
| 146 | debug_logger.error("Cannot shutDown %s, error is:\n%s"%( str(plug), msg ) ) |
|---|
| 147 | |
|---|
| 148 | # Wait for plug shutdown complete |
|---|
| 149 | for plug in g_window.plugins: |
|---|
| 150 | debug_logger.debug( "[LOPLUG] wait for shutdown complete " + str(plug) ) |
|---|
| 151 | try: |
|---|
| 152 | plug.waitForShutDownComplete() |
|---|
| 153 | del plug |
|---|
| 154 | except: |
|---|
| 155 | import traceback |
|---|
| 156 | exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() |
|---|
| 157 | msg_a = traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback) |
|---|
| 158 | msg = "" |
|---|
| 159 | for m in msg_a: |
|---|
| 160 | msg += m |
|---|
| 161 | debug_logger.error("Cannot waitforshutDown %s, error is:\n%s"%( str(plug), msg ) ) |
|---|
| 162 | |
|---|
| 163 | debug_logger.debug("[LOPLUG] shutdown PlugRouter") |
|---|
| 164 | # Shutdown PlugRouter |
|---|
| 165 | plugRouter.shutDown() |
|---|
| 166 | plugRouterThread.join() |
|---|
| 167 | debug_logger.debug( "[LOPLUG] shutdown PlugRouter done" ) |
|---|
| 168 | |
|---|
| 169 | g_window.plugins = None |
|---|
| 170 | |
|---|
| 171 | g_window.config.save_xml(CONFIG_FILE) |
|---|
| 172 | app.closeAllWindows() |
|---|
| 173 | # ================================================= |
|---|
| 174 | if __name__ == "__main__": |
|---|
| 175 | if hasattr(sys, 'frozen'): |
|---|
| 176 | g_pathname = os.path.dirname(sys.executable) |
|---|
| 177 | elif __file__: |
|---|
| 178 | g_pathname = os.path.dirname(__file__) |
|---|
| 179 | |
|---|
| 180 | g_pathname = os.path.abspath(g_pathname ) |
|---|
| 181 | debug_logger = logging.getLogger('[LoPlug]') |
|---|
| 182 | |
|---|
| 183 | import traceback |
|---|
| 184 | app = QApplication(sys.argv) |
|---|
| 185 | |
|---|
| 186 | #- Load traduction |
|---|
| 187 | locale = QLocale.system().name(); |
|---|
| 188 | |
|---|
| 189 | #-Debug |
|---|
| 190 | #locale = "fr_FR" |
|---|
| 191 | #locale = "en_US" |
|---|
| 192 | |
|---|
| 193 | translator = QTranslator() |
|---|
| 194 | translator.load(QString("lome_"+locale), QString(os.path.join(g_pathname, "i18n"))) |
|---|
| 195 | app.installTranslator(translator); |
|---|
| 196 | |
|---|
| 197 | cst_init(app) |
|---|
| 198 | from unitloader import * |
|---|
| 199 | UnitLoader() |
|---|
| 200 | |
|---|
| 201 | g_window = uic.loadUi(join(g_pathname,"loplug","server","loplugServerDlg.ui")) |
|---|
| 202 | g_window.setWindowIcon(QIcon(os.path.join(g_pathname,"loplug","resources", "loplug.png"))) |
|---|
| 203 | g_window.connect( g_window.configBtn, SIGNAL("clicked()"), configure ) |
|---|
| 204 | g_window.connect(g_window.aboutBtn, SIGNAL("clicked()"), on_about) |
|---|
| 205 | g_window.connect(app, SIGNAL("aboutToQuit()"), on_about_to_quit) |
|---|
| 206 | |
|---|
| 207 | g_window.config = Configuration( name="loplug") |
|---|
| 208 | ret = g_window.config.load_xml("loplug.cfg") |
|---|
| 209 | if ret: |
|---|
| 210 | on_about() |
|---|
| 211 | |
|---|
| 212 | import glob |
|---|
| 213 | g_window.plugins = [] |
|---|
| 214 | plug_path = "loplug/plugins/*/pg*.py" |
|---|
| 215 | raw_plugins = glob.glob( plug_path ) |
|---|
| 216 | zipfile = False |
|---|
| 217 | if not raw_plugins: |
|---|
| 218 | plug_path = "loplug/plugins/*/*.pyo" |
|---|
| 219 | raw_plugins = glob.glob( plug_path ) |
|---|
| 220 | zipfile = True |
|---|
| 221 | |
|---|
| 222 | if ret: |
|---|
| 223 | make_default_config_file("loplug.cfg", plug_path) |
|---|
| 224 | g_window.config.load_xml("loplug.cfg") |
|---|
| 225 | |
|---|
| 226 | options = { |
|---|
| 227 | "pathname":g_pathname, |
|---|
| 228 | "parent_window":g_window |
|---|
| 229 | } |
|---|
| 230 | plug_vbox = QVBoxLayout() |
|---|
| 231 | g_window.pluginsBox.setLayout( plug_vbox ) |
|---|
| 232 | for p in raw_plugins: |
|---|
| 233 | full_path = os.path.normpath(os.path.join( g_pathname, p )) |
|---|
| 234 | if zipfile: |
|---|
| 235 | sys.path.append( full_path ) |
|---|
| 236 | else: |
|---|
| 237 | if os.path.dirname( full_path ) not in sys.path: |
|---|
| 238 | sys.path.append( os.path.dirname( full_path ) ) |
|---|
| 239 | |
|---|
| 240 | if (not "common" in p |
|---|
| 241 | and not "pgpluginclient" in p |
|---|
| 242 | and not "pgproxyclient" in p |
|---|
| 243 | and not "pgdatabase" in p |
|---|
| 244 | and not "pgservice" in p): |
|---|
| 245 | try: |
|---|
| 246 | dir = os.path.basename( full_path ).replace(".pyc", "").replace(".pyo", "").replace(".py", "") |
|---|
| 247 | debug_logger.debug( "Load plug: %s" % ( dir ) ) |
|---|
| 248 | exec("from " + dir + " import *") |
|---|
| 249 | obj = eval(dir[0:3].upper() + dir[3:] ) |
|---|
| 250 | obj_ins = obj(g_window.config, options) |
|---|
| 251 | g_window.plugins.append(obj_ins) |
|---|
| 252 | w = obj_ins.get_main_widget() |
|---|
| 253 | if w: |
|---|
| 254 | plug_vbox.addWidget( w ) |
|---|
| 255 | except: |
|---|
| 256 | import traceback |
|---|
| 257 | exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() |
|---|
| 258 | msg_a = traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback) |
|---|
| 259 | msg = "" |
|---|
| 260 | for m in msg_a: |
|---|
| 261 | msg += m |
|---|
| 262 | debug_logger.error("Cannot instanciate %s, error is:\n%s"%( str(p), msg ) ) |
|---|
| 263 | |
|---|
| 264 | options = {} |
|---|
| 265 | # Start PlugRouter which is responsible for the plug communication means |
|---|
| 266 | # It will open a well konwn port used by plug to communicate |
|---|
| 267 | plugRouter = PlugRouter( ( CST_LOPLUG_ROUTER_ADDRESS, CST_LOPLUG_ROUTER_PORT)) |
|---|
| 268 | plugRouterThread = threading.Thread( target = plugRouter.serve_forever) |
|---|
| 269 | plugRouterThread.setDaemon( True) |
|---|
| 270 | plugRouterThread.start() |
|---|
| 271 | # As it is a thread, keep it time to execute |
|---|
| 272 | sleep(1) |
|---|
| 273 | |
|---|
| 274 | # Ask plugs to identify themselves to the plugRouter |
|---|
| 275 | for plug in g_window.plugins: |
|---|
| 276 | plug.connect_to_plugRouter() |
|---|
| 277 | |
|---|
| 278 | # Start plugs |
|---|
| 279 | for plug in g_window.plugins: |
|---|
| 280 | if not plug.isRenderer(): |
|---|
| 281 | try: |
|---|
| 282 | plug.start() |
|---|
| 283 | except: |
|---|
| 284 | import traceback |
|---|
| 285 | exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() |
|---|
| 286 | msg_a = traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback) |
|---|
| 287 | msg = "" |
|---|
| 288 | for m in msg_a: |
|---|
| 289 | msg += m |
|---|
| 290 | debug_logger.error("Cannot start %s, error is:\n%s"%( str(plug), msg ) ) |
|---|
| 291 | for plug in g_window.plugins: |
|---|
| 292 | if plug.isRenderer(): |
|---|
| 293 | try: |
|---|
| 294 | plug.start() |
|---|
| 295 | except: |
|---|
| 296 | import traceback |
|---|
| 297 | exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() |
|---|
| 298 | msg_a = traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback) |
|---|
| 299 | msg = "" |
|---|
| 300 | for m in msg_a: |
|---|
| 301 | msg += m |
|---|
| 302 | debug_logger.error("Cannot start %s, error is:\n%s"%( str(plug), msg ) ) |
|---|
| 303 | |
|---|
| 304 | check_status() |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | # Start graphical interface |
|---|
| 308 | g_window.show() |
|---|
| 309 | status = app.exec_() |
|---|
| 310 | |
|---|
| 311 | debug_logger.debug( "[LOPLUG] EXIT QT LOOP" ) |
|---|
| 312 | |
|---|
| 313 | g_window = None |
|---|
| 314 | QApplication.exit() |
|---|
| 315 | # sys.exit( status) |
|---|