From: rnv Date: Thu, 22 Jun 2017 16:17:35 +0000 (+0300) Subject: Merge Python 3 porting. X-Git-Tag: V9_0_0~2 X-Git-Url: http://git.salome-platform.org/gitweb/?p=samples%2Fpyhello.git;a=commitdiff_plain;h=07c0897d109f195fb52301a591e59dad594bc93c Merge Python 3 porting. --- 07c0897d109f195fb52301a591e59dad594bc93c diff --cc doc/input/index.doc index 6b7409b,612d384..4fd29cd --- a/doc/input/index.doc +++ b/doc/input/index.doc @@@ -480,21 -480,18 +480,18 @@@ def initialize() \code def createPreferences(): - if verbose() : print "PYHELLOGUI.createPreferences()" - gid = sgPyQt.addPreference( "General" ) - gid = sgPyQt.addPreference( "Object creation", gid ) - pid = sgPyQt.addPreference( "Default name", gid, SalomePyQt.PT_String, "PYHELLO", "def_obj_name" ) - pid = sgPyQt.addPreference( "Default creation mode", gid, SalomePyQt.PT_Selector, "PYHELLO", "creation_mode" ) - strings = QStringList() - strings.append( "Default name" ) - strings.append( "Generate name" ) - strings.append( "Ask name" ) - indexes = [] - indexes.append( QVariant(0) ) - indexes.append( QVariant(1) ) - indexes.append( QVariant(2) ) - sgPyQt.setPreferenceProperty( pid, "strings", QVariant( strings ) ) - sgPyQt.setPreferenceProperty( pid, "indexes", QVariant( indexes ) ) + if verbose(): - print("PYHELLOGUI.createPreferences() : study : %d" % _getStudyId()) ++ print("PYHELLOGUI.createPreferences()") + gid = sgPyQt.addPreference("General") + gid = sgPyQt.addPreference("Object creation", gid) + sgPyQt.addPreference("Default name", gid, SalomePyQt.PT_String, "PYHELLO", "def_obj_name") + pid = sgPyQt.addPreference("Default creation mode", gid, SalomePyQt.PT_Selector, "PYHELLO", "creation_mode") + strings = ["Default name", "Generate name", "Ask name"] + indexes = [0, 1, 2] + sgPyQt.setPreferenceProperty(pid, "strings", strings) + sgPyQt.setPreferenceProperty(pid, "indexes", indexes) + pid = sgPyQt.addPreference("Password", gid, SalomePyQt.PT_String, "PYHELLO", "Password") + sgPyQt.setPreferenceProperty(pid, "echo", 2) pass \endcode @@@ -502,7 -499,7 +499,7 @@@ \code def windows(): - if verbose() : print "PYHELLOGUI.windows()" - if verbose() : print("PYHELLOGUI.windows() : study : %d" % _getStudyId()) ++ if verbose() : print("PYHELLOGUI.windows()") wm = {} wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea wm[SalomePyQt.WT_PyConsole] = Qt.BottomDockWidgetArea diff --cc src/PYHELLO/PYHELLO.py index c5540f9,72897c3..3303c8f mode 100755,100644..100755 --- a/src/PYHELLO/PYHELLO.py +++ b/src/PYHELLO/PYHELLO.py @@@ -91,31 -89,30 +90,31 @@@ class PYHELLO(PYHELLO_ORB__POA.PYHELLO_ """ Dump module data to the Python script. """ - def DumpPython( self, study, isPublished, isMultiFile ): + def DumpPython( self, isPublished, isMultiFile ): abuffer = [] names = [] + study = getStudy() father = study.FindComponent( moduleName() ) if father: - iter = study.NewChildIterator( father ) - while iter.More(): - name = iter.Value().GetName() + iterator = study.NewChildIterator(father) + while iterator.More(): + name = iterator.Value().GetName() if name: names.append( name ) - iter.Next() + iterator.Next() pass pass if names: - abuffer += [ "from salome import lcc" ] + abuffer += [ "import salome" ] abuffer += [ "import PYHELLO_ORB" ] abuffer += [ "" ] - abuffer += [ "pyhello = lcc.FindOrLoadComponent('FactoryServerPy', '%s')" % moduleName() ] + abuffer += [ "pyhello = salome.lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ] abuffer += [ "" ] - abuffer += [ "pyhello.createObject( '%s' )" % name for name in names ] - abuffer += [ "pyhello.createObject(theStudy, '%s')" % name for name in names ] ++ abuffer += [ "pyhello.createObject( '%s')" % name for name in names ] abuffer += [ "" ] pass if isMultiFile: - abuffer = [ " " + s for s in abuffer ] - abuffer[0:0] = [ "def RebuildData():" ] - abuffer += [ " pass" ] + abuffer = [ " " + s for s in abuffer ] - abuffer[0:0] = [ "def RebuildData( theStudy ):" ] ++ abuffer[0:0] = [ "def RebuildData():" ] + abuffer += [ " pass" ] abuffer += [ "\0" ] return ("\n".join( abuffer ), 1) diff --cc src/PYHELLO/PYHELLO_utils.py index d53d1d8,cb54ac1..615fde7 mode 100755,100644..100755 --- a/src/PYHELLO/PYHELLO_utils.py +++ b/src/PYHELLO/PYHELLO_utils.py @@@ -130,16 -129,16 +130,16 @@@ def getLCC() return __lcc__ ## -# Get study manager -### -__study_manager__ = None -def getStudyManager(): - global __study_manager__ - if __study_manager__ is None: - obj = getNS().Resolve( '/myStudyManager' ) - __study_manager__ = obj._narrow( SALOMEDS.StudyManager ) +# Get study +### +__study__ = None +def getStudy(): + global __study__ + if __study__ is None: - obj = getNS().Resolve( '/Study' ) ++ obj = getNS().Resolve( '/Study' ) + __study__ = obj._narrow( SALOMEDS.Study ) pass - return __study_manager__ + return __study__ ### # Get PYHELLO engine diff --cc src/PYHELLOGUI/PYHELLOGUI.py index d352ddf,69b1f53..9ded024 --- a/src/PYHELLOGUI/PYHELLOGUI.py +++ b/src/PYHELLOGUI/PYHELLOGUI.py @@@ -26,13 -26,20 +26,22 @@@ # --- # import traceback - import os - from qtsalome import * - from PYHELLO_utils import * + from PYHELLO_utils import (moduleName, getStudyManager, getObjectID, verbose, + moduleID, objectID, getEngineIOR, getEngine) + from SalomePyQt import (SalomePyQt, WT_ObjectBrowser, WT_PyConsole, PT_Selector, # @UnresolvedImport + PT_String) # @UnresolvedImport + from qtsalome import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, # @UnresolvedImport + QPushButton, QMessageBox, QInputDialog, Qt) # @UnresolvedImport + from salome.kernel import termcolor + from salome.kernel.logger import Logger + import libSALOME_Swig + + + logger = Logger(moduleName(), color=termcolor.RED_FG) +import salome + ################################################ # GUI context class # Used to store actions, menus, toolbars, etc... @@@ -169,7 -212,7 +176,7 @@@ def _getSelection() # called when module is initialized # perform initialization actions def initialize(): - if verbose() : print "PYHELLOGUI.initialize()" - if verbose() : print("PYHELLOGUI.initialize() : study : %d" % _getStudyId()) ++ if verbose() : print("PYHELLOGUI.initialize()") # set default preferences values if not sgPyQt.hasSetting( "PYHELLO", "def_obj_name"): sgPyQt.addSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ) @@@ -182,58 -225,62 +189,53 @@@ # called when module is initialized # return map of popup windows to be used by the module def windows(): - if verbose() : print "PYHELLOGUI.windows()" - if verbose() : print("PYHELLOGUI.windows() : study : %d" % _getStudyId()) ++ if verbose() : print("PYHELLOGUI.windows()") wm = {} - wm[SalomePyQt.WT_ObjectBrowser] = Qt.LeftDockWidgetArea - wm[SalomePyQt.WT_PyConsole] = Qt.BottomDockWidgetArea + wm[WT_ObjectBrowser] = Qt.LeftDockWidgetArea + wm[WT_PyConsole] = Qt.BottomDockWidgetArea return wm # called when module is initialized # return list of 2d/3d views to be used ny the module def views(): - if verbose() : print "PYHELLOGUI.views()" - if verbose() : print("PYHELLOGUI.views() : study : %d" % _getStudyId()) ++ if verbose() : print("PYHELLOGUI.views()") return [] # called when module is initialized # export module's preferences def createPreferences(): - if verbose() : print "PYHELLOGUI.createPreferences()" - gid = sgPyQt.addPreference( "General" ) - gid = sgPyQt.addPreference( "Object creation", gid ) - pid = sgPyQt.addPreference( "Default name", gid, SalomePyQt.PT_String, "PYHELLO", "def_obj_name" ) - pid = sgPyQt.addPreference( "Default creation mode", gid, SalomePyQt.PT_Selector, "PYHELLO", "creation_mode" ) - strings = QStringList() - strings.append( "Default name" ) - strings.append( "Generate name" ) - strings.append( "Ask name" ) - indexes = [] - indexes.append( QVariant(0) ) - indexes.append( QVariant(1) ) - indexes.append( QVariant(2) ) - sgPyQt.setPreferenceProperty( pid, "strings", QVariant( strings ) ) - sgPyQt.setPreferenceProperty( pid, "indexes", QVariant( indexes ) ) - pid = sgPyQt.addPreference( "Password", gid, SalomePyQt.PT_String, "PYHELLO", "Password" ) - sgPyQt.setPreferenceProperty( pid, "echo", QVariant( 2 ) ) + if verbose(): - print("PYHELLOGUI.createPreferences() : study : %d" % _getStudyId()) ++ print("PYHELLOGUI.createPreferences()") + gid = sgPyQt.addPreference("General") + gid = sgPyQt.addPreference("Object creation", gid) + sgPyQt.addPreference("Default name", gid, PT_String, "PYHELLO", "def_obj_name") + pid = sgPyQt.addPreference("Default creation mode", gid, PT_Selector, "PYHELLO", "creation_mode") + strings = ["Default name", "Generate name", "Ask name"] + indexes = [0, 1, 2] + sgPyQt.setPreferenceProperty(pid, "strings", strings) + sgPyQt.setPreferenceProperty(pid, "indexes", indexes) + pid = sgPyQt.addPreference("Password", gid, PT_String, "PYHELLO", "Password") + sgPyQt.setPreferenceProperty(pid, "echo", 2) pass # called when module is activated # returns True if activating is successfull and False otherwise def activate(): - if verbose() : print "PYHELLOGUI.activate()" - if verbose() : print("PYHELLOGUI.activate() : study : %d" % _getStudyId()) - ctx = _setContext( _getStudyId() ) ++ if verbose() : print("PYHELLOGUI.activate()") + GUIcontext() return True # called when module is deactivated def deactivate(): - if verbose() : print "PYHELLOGUI.deactivate()" - if verbose() : print("PYHELLOGUI.deactivate() : study : %d" % _getStudyId()) - pass - -# called when active study is changed -# active study ID is passed as parameter -def activeStudyChanged( studyID ): - if verbose() : print("PYHELLOGUI.activeStudyChanged(): study : %d" % studyID) - ctx = _setContext( _getStudyId() ) ++ if verbose() : print("PYHELLOGUI.deactivate()") pass # called when popup menu is invoked # popup menu and menu context are passed as parameters def createPopupMenu( popup, context ): - if verbose() : print "PYHELLOGUI.createPopupMenu(): context = %s" % context + if verbose() : print("PYHELLOGUI.createPopupMenu(): context = %s" % context) - ctx = _setContext( _getStudyId() ) - study = _getStudy() selcount, selected = _getSelection() - if verbose() : print selcount, selected + if verbose() : print(selcount, selected) if selcount == 1: # one object is selected if moduleID() in selected: @@@ -422,14 -470,16 +425,16 @@@ def CreateObject() name = default_name pass pass - except: + except Exception as e: + logger.debug(e) # generate object name - __objectid__ = __objectid__ + 1 - name = "%s %d" % ( default_name, __objectid__ ) + __objectid__ = __objectid__ + 1 + name = "%s %d" % (default_name, __objectid__) pass - if not name: return - getEngine().createObject( name ) + if not name: + return - getEngine().createObject(_getStudy(), name) - sg.updateObjBrowser(True) ++ getEngine().createObject( name) + sg.updateObjBrowser() pass ###