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/?a=commitdiff_plain;h=07c0897d109f195fb52301a591e59dad594bc93c;hp=-c;p=samples%2Fpyhello.git Merge Python 3 porting. --- 07c0897d109f195fb52301a591e59dad594bc93c diff --combined 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 --combined src/PYHELLO/PYHELLO.py index c5540f9,72897c3..3303c8f mode 100755,100644..100755 --- a/src/PYHELLO/PYHELLO.py +++ b/src/PYHELLO/PYHELLO.py @@@ -29,8 -29,7 +29,7 @@@ import PYHELLO_ORB__PO import SALOME_ComponentPy import SALOME_DriverPy import SALOMEDS - - from PYHELLO_utils import * + from PYHELLO_utils import findOrCreateComponent, objectID, moduleName class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen, SALOME_ComponentPy.SALOME_ComponentPy_i, @@@ -77,45 -76,43 +76,45 @@@ """ Create object. """ - def createObject( self, study, name ): + def createObject( self, name ): + study = getStudy() builder = study.NewBuilder() - father = findOrCreateComponent( study ) - object = builder.NewObject( father ) - attr = builder.FindOrCreateAttribute( object, "AttributeName" ) + father = findOrCreateComponent() + obj = builder.NewObject( father ) + attr = builder.FindOrCreateAttribute( obj, "AttributeName" ) attr.SetValue( name ) - attr = builder.FindOrCreateAttribute( object, "AttributeLocalID" ) + attr = builder.FindOrCreateAttribute( obj, "AttributeLocalID" ) attr.SetValue( objectID() ) pass """ 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 --combined 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 @@@ -32,14 -32,13 +32,14 @@@ __all__ = "getORB", "getNS", "getLCC", - "getStudyManager", "getEngine", + "getStudy", "getEngineIOR", "findOrCreateComponent", "getObjectID", ] + from omniORB import CORBA from SALOME_NamingServicePy import SALOME_NamingServicePy_i from LifeCycleCORBA import LifeCycleCORBA @@@ -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 @@@ -165,9 -164,8 +165,9 @@@ def getEngineIOR() ### # Find or create PYHELLO component object in a study ### -def findOrCreateComponent( study ): - father = study.FindComponent( moduleName() ) +def findOrCreateComponent(): + study = getStudy() + father =study.FindComponent( moduleName() ) if father is None: builder = study.NewBuilder() father = builder.NewComponent( moduleName() ) @@@ -188,10 -186,9 +188,10 @@@ ### # Get object's ID ### -def getObjectID( study, entry ): +def getObjectID( entry ): ID = unknownID() - if study and entry: + study = getStudy() + if entry: sobj = study.FindObjectID( entry ) if sobj is not None: test, anAttr = sobj.FindAttribute( "AttributeLocalID" ) diff --combined 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... @@@ -109,17 -116,19 +118,15 @@@ class GUIcontext # Global variables ################################################ -# study-to-context map -__study2context__ = {} -# current context -__current_context__ = None # object counter __objectid__ = 0 ################################################ # Get SALOME PyQt interface - import SalomePyQt - sgPyQt = SalomePyQt.SalomePyQt() + sgPyQt = SalomePyQt() # Get SALOME Swig interface - import libSALOME_Swig sg = libSALOME_Swig.SALOMEGUI_Swig() ################################################ @@@ -128,12 -137,27 +135,12 @@@ # Internal methods ################################################ -### -# get active study ID -### -def _getStudyId(): - return sgPyQt.getStudyId() - -### -# get active study -### -def _getStudy(): - studyId = _getStudyId() - study = getStudyManager().GetStudyByID( studyId ) - return study - ### # returns True if object has children ### def _hasChildren( sobj ): if sobj: - study = _getStudy() - iter = study.NewChildIterator( sobj ) + iter = salome.myStudy.NewChildIterator( sobj ) while iter.More(): name = iter.Value().GetName() if name: @@@ -143,6 -167,25 +150,6 @@@ pass return False -### -# get current GUI context -### -def _getContext(): - global __current_context__ - return __current_context__ - -### -# set and return current GUI context -# study ID is passed as parameter -### -def _setContext( studyID ): - global __study2context__, __current_context__ - if studyID not in __study2context__: - __study2context__[studyID] = GUIcontext() - pass - __current_context__ = __study2context__[studyID] - return __current_context__ - ### # increment object counter in the map ### @@@ -158,7 -201,7 +165,7 @@@ def _getSelection() selcount = sg.SelectedCount() seltypes = {} for i in range( selcount ): - _incObjToMap( seltypes, getObjectID( _getStudy(), sg.getSelected( i ) ) ) + _incObjToMap( seltypes, getObjectID( sg.getSelected( i ) ) ) pass return selcount, seltypes @@@ -169,7 -212,7 +176,7 @@@ # 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: @@@ -264,57 -311,57 +266,57 @@@ # called when GUI action is activated # action ID is passed as parameter def OnGUIEvent( commandID ): - if verbose() : print "PYHELLOGUI.OnGUIEvent(): command = %d" % commandID - if dict_command.has_key( commandID ): + if verbose() : print("PYHELLOGUI.OnGUIEvent(): command = %d" % commandID) + if commandID in dict_command: try: dict_command[commandID]() except: traceback.print_exc() else: - if verbose() : print "The command is not implemented: %d" % commandID + if verbose() : print("The command is not implemented: %d" % commandID) pass # called when module's preferences are changed # preference's resources section and setting name are passed as parameters def preferenceChanged( section, setting ): - if verbose() : print "PYHELLOGUI.preferenceChanged(): %s / %s" % ( section, setting ) + if verbose() : print("PYHELLOGUI.preferenceChanged(): %s / %s" % ( section, setting )) pass # called when active view is changed # view ID is passed as parameter def activeViewChanged( viewID ): - if verbose() : print "PYHELLOGUI.activeViewChanged(): %d" % viewID + if verbose() : print("PYHELLOGUI.activeViewChanged(): %d" % viewID) pass # called when active view is cloned # cloned view ID is passed as parameter def viewCloned( viewID ): - if verbose() : print "PYHELLOGUI.viewCloned(): %d" % viewID + if verbose() : print("PYHELLOGUI.viewCloned(): %d" % viewID) pass # called when active view is viewClosed # view ID is passed as parameter def viewClosed( viewID ): - if verbose() : print "PYHELLOGUI.viewClosed(): %d" % viewID + if verbose() : print("PYHELLOGUI.viewClosed(): %d" % viewID) pass # called when study is opened # returns engine IOR def engineIOR(): - if verbose() : print "PYHELLOGUI.engineIOR()" + if verbose() : print("PYHELLOGUI.engineIOR()") return getEngineIOR() # called to check if object can be dragged # returns True if drag operation is allowed for this object def isDraggable(what): - if verbose() : print "PYHELLOGUI.isDraggable()" + if verbose() : print("PYHELLOGUI.isDraggable()") # return True if object is draggable return False # called to check if object allows dropping on it # returns True if drop operation is allowed for this object def isDropAccepted(where): - if verbose() : print "PYHELLOGUI.isDropAccepted()" + if verbose() : print("PYHELLOGUI.isDropAccepted()") # return True if object accept drops return False @@@ -322,18 -369,18 +324,18 @@@ # performs corresponding data re-arrangement if allowed def dropObjects(what, where, row, action): if verbose() : - print "PYHELLOGUI.dropObjects()" + print("PYHELLOGUI.dropObjects()") # 'what' is a list of entries of objects being dropped - for i in what: print "- dropped:", i + for i in what: print("- dropped:", i) # 'where' is a parent object's entry - print "- dropping on:", where + print("- dropping on:", where) # 'row' is an position in the parent's children list; # -1 if appending to the end of children list is performed - print "- dropping position:", row + print("- dropping position:", row) # 'action' is a dropping action being performed: # - 0x01 (Qt::CopyAction) for copy # - 0x02 (Qt::MoveAction) for move - print "- drop action:", action + print("- drop action:", action) pass pass @@@ -403,49 -450,53 +405,52 @@@ def ShowHELLO() ### def CreateObject(): global __objectid__ - default_name = str( sgPyQt.stringSetting( "PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME ) ).strip() + default_name = sgPyQt.stringSetting("PYHELLO", "def_obj_name", GUIcontext.DEFAULT_NAME).strip() try: - if sgPyQt.action( GUIcontext.OPTION_3_ID ).isChecked(): + if sgPyQt.action(GUIcontext.OPTION_3_ID).isChecked(): # request object name from the user - name, ok = QInputDialog.getText( sgPyQt.getDesktop(), - "Create Object", - "Enter object name:", - QLineEdit.Normal, - default_name ) - if not ok: return - name = str( name ).strip() - elif sgPyQt.action( GUIcontext.OPTION_2_ID ).isChecked(): + name, ok = QInputDialog.getText(sgPyQt.getDesktop(), + "Create Object", + "Enter object name:", + QLineEdit.Normal, + default_name) + if not ok: + return + name = name.strip() + elif sgPyQt.action(GUIcontext.OPTION_2_ID).isChecked(): # generate object name - __objectid__ = __objectid__ + 1 - name = "%s %d" % ( default_name, __objectid__ ) + __objectid__ = __objectid__ + 1 + name = "%s %d" % (default_name, __objectid__) else: 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 ### # Delete all objects ### def DeleteAll(): - study = _getStudy() - father = study.FindComponent( moduleName() ) + father = salome.myStudy.FindComponent( moduleName() ) if father: - iter = study.NewChildIterator( father ) - builder = study.NewBuilder() + iter = salome.myStudy.NewChildIterator( father ) + builder = salome.myStudy.NewBuilder() while iter.More(): sobj = iter.Value() iter.Next() builder.RemoveObjectWithChildren( sobj ) pass - sg.updateObjBrowser( True ) + sg.updateObjBrowser() pass pass @@@ -453,9 -504,10 +458,9 @@@ # Show object's name ### def ShowMe(): - study = _getStudy() entry = sg.getSelected( 0 ) if entry != '': - sobj = study.FindObjectID( entry ) + sobj = salome.myStudy.FindObjectID( entry ) if ( sobj ): test, attr = sobj.FindAttribute( "AttributeName" ) if test: @@@ -469,28 -521,30 +474,28 @@@ # Delete selected object(s) ### def Delete(): - study = _getStudy() - builder = study.NewBuilder() + builder = salome.myStudy.NewBuilder() if sg.SelectedCount() <= 0: return for i in range( sg.SelectedCount() ): entry = sg.getSelected( i ) if entry != '': - sobj = study.FindObjectID( entry ) + sobj = salome.myStudy.FindObjectID( entry ) if ( sobj ): builder.RemoveObject( sobj ) pass pass pass - sg.updateObjBrowser( True ) + sg.updateObjBrowser() pass ### # Rename selected object ### def Rename(): - study = _getStudy() - builder = study.NewBuilder() + builder = salome.myStudy.NewBuilder() entry = sg.getSelected( 0 ) if entry != '': - sobj = study.FindObjectID( entry ) + sobj = salome.myStudy.FindObjectID( entry ) if ( sobj ): name, ok = QInputDialog.getText( sgPyQt.getDesktop(), "Object name", @@@ -501,7 -555,7 +506,7 @@@ if not ok or not name: return attr = builder.FindOrCreateAttribute( sobj, "AttributeName" ) attr.SetValue( name ) - sg.updateObjBrowser( True ) + sg.updateObjBrowser() pass pass pass