Skip to content

Commit

Permalink
Replace IECore.Enum with standard types from enum module
Browse files Browse the repository at this point in the history
Now we're Python 3 only we can use the standard `enum` module, which provides a more comprehensive framework than `IECore.Enum`. For the most part its a drop-in replacement, but there are a few gotchas to be aware of, which I have hopefully dealt with everywhere necessary :

- `str( e )` returns only `<name>` for `IECore.Enum` but returns `<className>.<name>` for `enum.Enum`.
- `int( e )` is provided by default for `IECore.Enum`, but not by `enum.Enum`, so we must use `enum.IntEnum` when that is needed.
- `IECore.Enum` values start at 0, but `enum.Enum` values start at 1 by default. Where this is important (see Wedge), we must specify `start = 0`.
  • Loading branch information
johnhaddon committed Nov 23, 2023
1 parent 14c785e commit d275461
Show file tree
Hide file tree
Showing 34 changed files with 92 additions and 75 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Breaking Changes
- StringPlug : Removed deprecated `precomputedHash` argument from `getValue()` method.
- OpenColorIOContext : Removed `configEnabledPlug()`, `configValuePlug()`, `workingSpaceEnabledPlug()` and `workingSpaceValuePlug()` methods. Use the OptionalValuePlug child accessors instead.
- Windows launch script : Removed the hardcoded `/debugexe` switch used when `GAFFER_DEBUG` is enabled, making it possible to use debuggers other than Visual Studio. Debug switches can be added to the `GAFFER_DEBUGGER` environment variable instead.
- Enums : Replaced `IECore.Enum` types with standard Python types from the `enum` module.

1.3.x.x (relative to 1.3.7.0)
=======
Expand Down
7 changes: 4 additions & 3 deletions python/GafferCortexUI/OpDialogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#
##########################################################################

import enum
import sys
import threading
import traceback
Expand Down Expand Up @@ -66,7 +67,7 @@ class OpDialogue( GafferUI.Dialogue ) :
#
# NoneByDefault : deprecated - the same as DisplayResult
# CloseByDefault : deprecated - the same as DisplayResult
PostExecuteBehaviour = IECore.Enum.create( "FromUserData", "None_", "Close", "DisplayResult", "DisplayResultAndClose", "NoneByDefault", "CloseByDefault" )
PostExecuteBehaviour = enum.Enum( "PostExecuteBehaviour", [ "FromUserData", "None_", "Close", "DisplayResult", "DisplayResultAndClose", "NoneByDefault", "CloseByDefault" ] )

## Defines which button has the focus when the op is displayed for editing.
#
Expand All @@ -79,7 +80,7 @@ class OpDialogue( GafferUI.Dialogue ) :
# OK : The OK button has the focus.
#
# Cancel : The cancel button has the focus.
DefaultButton = IECore.Enum.create( "FromUserData", "None_", "OK", "Cancel" )
DefaultButton = enum.Enum( "DefaultButton", [ "FromUserData", "None_", "OK", "Cancel" ] )

# If executeInBackground is True, then the Op will be executed on another
# thread, allowing the UI to remain responsive during execution. This is
Expand Down Expand Up @@ -254,7 +255,7 @@ def _acceptsClose( self ) :
# the op is running in the background.
return self.__state != self.__State.Execution

__State = IECore.Enum.create( "ParameterEditing", "Execution", "ErrorDisplay", "ResultDisplay" )
__State = enum.Enum( "__State", [ "ParameterEditing", "Execution", "ErrorDisplay", "ResultDisplay" ] )

def __initiateParameterEditing( self, *unused ) :

Expand Down
3 changes: 2 additions & 1 deletion python/GafferDispatch/LocalDispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#
##########################################################################

import enum
import os
import errno
import signal
Expand Down Expand Up @@ -62,7 +63,7 @@ def __init__( self, name = "LocalDispatcher", jobPool = None ) :

class Job( object ) :

Status = IECore.Enum.create( "Waiting", "Running", "Complete", "Failed", "Killed" )
Status = enum.IntEnum( "Status", [ "Waiting", "Running", "Complete", "Failed", "Killed" ] )

def __init__( self, batch, dispatcher ) :

Expand Down
4 changes: 2 additions & 2 deletions python/GafferDispatch/Wedge.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#
##########################################################################

import math
import enum
import imath

import IECore
Expand All @@ -44,7 +44,7 @@

class Wedge( GafferDispatch.TaskContextProcessor ) :

Mode = IECore.Enum.create( "FloatRange", "IntRange", "ColorRange", "FloatList", "IntList", "StringList" )
Mode = enum.IntEnum( "Mode", [ "FloatRange", "IntRange", "ColorRange", "FloatList", "IntList", "StringList" ], start = 0 )

def __init__( self, name = "Wedge" ) :

Expand Down
3 changes: 2 additions & 1 deletion python/GafferDispatchUI/DispatchDialogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#
##########################################################################

import enum
import functools
import sys
import threading
Expand All @@ -54,7 +55,7 @@ class DispatchDialogue( GafferUI.Dialogue ) :
# Close : The dialogue is closed immediately.
#
# Confirm : The dialogue remains open confirming success, with a button for returning to the editing state.
PostDispatchBehaviour = IECore.Enum.create( "Close", "Confirm" )
PostDispatchBehaviour = enum.Enum( "PostDispatchBehaviour", [ "Close", "Confirm" ] )

__dispatchDialogueMenuDefinition = None

Expand Down
3 changes: 2 additions & 1 deletion python/GafferSceneTest/SceneNodeTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#
##########################################################################

import enum
import inspect
import unittest
import time
Expand Down Expand Up @@ -218,7 +219,7 @@ def testDerivingInPython( self ) :

class SphereOrCube( GafferScene.SceneNode ) :

Type = IECore.Enum.create( "Sphere", "Cube" )
Type = enum.IntEnum( "Type", [ "Sphere", "Cube" ], start = 0 )

def __init__( self, name = "SphereOrCube" ) :

Expand Down
3 changes: 2 additions & 1 deletion python/GafferSceneUI/CryptomatteUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#
##########################################################################

import enum
import functools
import imath
import re
Expand Down Expand Up @@ -293,7 +294,7 @@ def __layerPresetValues( plug ) :
GafferUI.Pointer.registerPointer( "removeNames", GafferUI.Pointer( "removeObjects.png", imath.V2i( 36, 18 ) ) )
GafferUI.Pointer.registerPointer( "replaceNames", GafferUI.Pointer( "replaceObjects.png", imath.V2i( 36, 18 ) ) )

__DropMode = IECore.Enum.create( "None_", "Add", "Remove", "Replace" )
__DropMode = enum.Enum( "__DropMode", [ "None_", "Add", "Remove", "Replace" ] )

__originalDragPointer = None

Expand Down
4 changes: 2 additions & 2 deletions python/GafferSceneUI/PathFilterUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#
##########################################################################

import types
import enum
import imath
import functools
from collections import deque
Expand Down Expand Up @@ -236,7 +236,7 @@ def __popupMenu( menuDefinition, plugValueWidget ) :
GafferUI.Pointer.registerPointer( "removeObjects", GafferUI.Pointer( "removeObjects.png", imath.V2i( 53, 14 ) ) )
GafferUI.Pointer.registerPointer( "replaceObjects", GafferUI.Pointer( "replaceObjects.png", imath.V2i( 53, 14 ) ) )

__DropMode = IECore.Enum.create( "None_", "Add", "Remove", "Replace" )
__DropMode = enum.Enum( "__DropMode", [ "None_", "Add", "Remove", "Replace" ] )

__originalDragPointer = None

Expand Down
9 changes: 5 additions & 4 deletions python/GafferSceneUI/SceneInspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#
##########################################################################

import enum
import math
import difflib
import html
Expand Down Expand Up @@ -332,7 +333,7 @@ def update( self, values ) :
# with background colours appropriate to the relationship between the two.
class SideBySideDiff( Diff ) :

Background = IECore.Enum.create( "A", "B", "AB", "Other" )
Background = enum.Enum( "Background", [ "A", "B", "AB", "Other" ] )

def __init__( self, **kw ) :

Expand Down Expand Up @@ -412,8 +413,8 @@ def update( self, values, visibilities = None, backgrounds = None ) :
continue

repolish = False
if str(backgrounds[i]) != frame._qtWidget().property( "gafferDiff" ) :
frame._qtWidget().setProperty( "gafferDiff", str(backgrounds[i]) )
if backgrounds[i].name != frame._qtWidget().property( "gafferDiff" ) :
frame._qtWidget().setProperty( "gafferDiff", backgrounds[i].name )
repolish = True

if i == 0 :
Expand Down Expand Up @@ -1238,7 +1239,7 @@ def __nodeSetMemberRemoved( self, set, node ) :

class _Rail( GafferUI.ListContainer ) :

Type = IECore.Enum.create( "Top", "Middle", "Gap", "Bottom", "Single" )
Type = enum.Enum( "Type", [ "Top", "Middle", "Gap", "Bottom", "Single" ] )

def __init__( self, type, **kw ) :

Expand Down
12 changes: 5 additions & 7 deletions python/GafferUI/BoolWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
#
##########################################################################

import types

import IECore
import enum

import Gaffer
import GafferUI
Expand All @@ -47,10 +45,10 @@

class BoolWidget( GafferUI.Widget ) :

DisplayMode = IECore.Enum.create( "CheckBox", "Switch", "Tool" )
DisplayMode = enum.Enum( "DisplayMode", [ "CheckBox", "Switch", "Tool" ] )
# True/False states are deliberately omitted from this enum;
# For backwards compatibility we use `bool` values instead.
State = IECore.Enum.create( "Indeterminate" )
State = enum.Enum( "State", [ "Indeterminate" ] )

def __init__( self, text="", checked=False, displayMode=DisplayMode.CheckBox, image = None, **kw ) :

Expand Down Expand Up @@ -118,7 +116,7 @@ def getState( self ) :

def setDisplayMode( self, displayMode ) :

self._qtWidget().setProperty( "gafferDisplayMode", str( displayMode ) )
self._qtWidget().setProperty( "gafferDisplayMode", displayMode.name )
self._qtWidget().setHitMode(
_CheckBox.HitMode.Button if displayMode == self.DisplayMode.Tool else _CheckBox.HitMode.CheckBox
)
Expand Down Expand Up @@ -159,7 +157,7 @@ def __stateChanged( self, state ) :

class _CheckBox( QtWidgets.QCheckBox ) :

HitMode = IECore.Enum.create( "Button", "CheckBox" )
HitMode = enum.Enum( "HitMode", [ "Button", "CheckBox" ] )

def __init__( self, text, parent = None ) :

Expand Down
12 changes: 8 additions & 4 deletions python/GafferUI/CodeWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import os
import re
import enum
import functools
import token
import keyword
Expand Down Expand Up @@ -284,10 +285,13 @@ def __textChanged( self, widget ) :

class Highlighter( object ) :

Type = IECore.Enum.create(
"SingleQuotedString", "DoubleQuotedString", "Number",
"Keyword", "ControlFlow", "Braces", "Operator", "Call",
"Comment", "ReservedWord", "Preprocessor"
Type = enum.Enum(
"Type",
[
"SingleQuotedString", "DoubleQuotedString", "Number",
"Keyword", "ControlFlow", "Braces", "Operator", "Call",
"Comment", "ReservedWord", "Preprocessor"
]
)

# Specifies a highlight type to be used for the characters
Expand Down
5 changes: 2 additions & 3 deletions python/GafferUI/ColorChooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
#
##########################################################################

import enum
import sys
import imath

import IECore

import Gaffer
import GafferUI

Expand Down Expand Up @@ -110,7 +109,7 @@ def _displayTransformChanged( self ) :

class ColorChooser( GafferUI.Widget ) :

ColorChangedReason = IECore.Enum.create( "Invalid", "SetColor", "Reset" )
ColorChangedReason = enum.Enum( "ColorChangedReason", [ "Invalid", "SetColor", "Reset" ] )

def __init__( self, color=imath.Color3f( 1 ), **kw ) :

Expand Down
4 changes: 2 additions & 2 deletions python/GafferUI/Divider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
#
##########################################################################

import IECore
import enum

import GafferUI

from Qt import QtWidgets

class Divider( GafferUI.Widget ) :

Orientation = IECore.Enum.create( "Vertical", "Horizontal" )
Orientation = enum.Enum( "Divider", [ "Vertical", "Horizontal" ] )

def __init__( self, orientation = Orientation.Horizontal, **kw ) :

Expand Down
13 changes: 7 additions & 6 deletions python/GafferUI/Enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#
##########################################################################

import IECore
import enum

import GafferUI

from Qt import QtCore
Expand All @@ -44,7 +45,7 @@

# HorizontalAlignment

HorizontalAlignment = IECore.Enum.create( "None_", "Left", "Right", "Center", "Justify" )
HorizontalAlignment = enum.Enum( "HorizontalAlignment", [ "None_", "Left", "Right", "Center", "Justify" ] )

@staticmethod
def __horizontalFromQt( a ) :
Expand Down Expand Up @@ -80,7 +81,7 @@ def __horizontalToQt( a ) :

# VerticalAlignment

VerticalAlignment = IECore.Enum.create( "None_", "Top", "Bottom", "Center" )
VerticalAlignment = enum.Enum( "VerticalAlignment", [ "None_", "Top", "Bottom", "Center" ] )

@staticmethod
def __verticalFromQt( a ) :
Expand Down Expand Up @@ -112,11 +113,11 @@ def __verticalToQt( a ) :

# Edge

Edge = IECore.Enum.create( "Top", "Bottom", "Left", "Right" )
Edge = enum.Enum( "Edge", [ "Top", "Bottom", "Left", "Right" ] )

# Scroll Mode

ScrollMode = IECore.Enum.create( "Never", "Always", "Automatic" )
ScrollMode = enum.Enum( "ScrollMode", [ "Never", "Always", "Automatic" ] )

__modesToPolicies = {
ScrollMode.Never : QtCore.Qt.ScrollBarAlwaysOff,
Expand All @@ -139,4 +140,4 @@ def __scrollModeFromQt ( a ):
return __policiesToModes[a]

ScrollMode._fromQt = __scrollModeFromQt
ScrollMode._toQt = __scrollModeToQt
ScrollMode._toQt = __scrollModeToQt
3 changes: 2 additions & 1 deletion python/GafferUI/EventLoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#
##########################################################################

import enum
import time
import weakref
import threading
Expand All @@ -53,7 +54,7 @@
## This class provides the event loops used to run GafferUI based applications.
class EventLoop( object ) :

__RunStyle = IECore.Enum.create( "Normal", "PumpThread", "AlreadyRunning", "Houdini" )
__RunStyle = enum.Enum( "__RunStyle", [ "Normal", "PumpThread", "AlreadyRunning", "Houdini" ] )

## Creates a new EventLoop. Note that if you are creating the primary
# EventLoop for an application then you should use mainEventLoop() instead.
Expand Down
6 changes: 3 additions & 3 deletions python/GafferUI/Frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#
##########################################################################

import IECore
import enum

import GafferUI

Expand All @@ -44,7 +44,7 @@
class Frame( GafferUI.ContainerWidget ) :

## \todo Raised and Inset?
BorderStyle = IECore.Enum.create( "None_", "Flat" )
BorderStyle = enum.Enum( "BorderStyle", [ "None_", "Flat" ] )

def __init__( self, child=None, borderWidth=8, borderStyle=BorderStyle.Flat, **kw ) :

Expand All @@ -61,7 +61,7 @@ def __init__( self, child=None, borderWidth=8, borderStyle=BorderStyle.Flat, **k

def setBorderStyle( self, borderStyle ) :

self._qtWidget().setProperty( "gafferBorderStyle", str( borderStyle ) )
self._qtWidget().setProperty( "gafferBorderStyle", borderStyle.name )

def getBorderStyle( self ) :

Expand Down
Loading

0 comments on commit d275461

Please sign in to comment.