Skip to content

Commit

Permalink
<Release prep>[]: <Clean up wxID>
Browse files Browse the repository at this point in the history
[Use smart type to wrap basic type; Invocation code needs updating]

[]
  • Loading branch information
Humberto Sanchez II committed Oct 24, 2022
1 parent 4c5e42b commit c68f30d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __pycache__/
/html/
build/
dist/
pyutplugincore.egg-info
pyutplugincore.egg-info/
/pyenv-3.10.6/
/pyenv-3.10.0/
.envrc
Expand Down
66 changes: 19 additions & 47 deletions core/PluginManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

from wx import Yield as wxYield

from core.ToolPluginInterface import ToolPluginInterface
from core.IOPluginInterface import IOPluginInterface
from core.IPluginAdapter import IPluginAdapter
from core.Singleton import Singleton

from core.ToolPluginInterface import ToolPluginInterface
from core.types.PluginDataTypes import IOPluginMap
from core.types.PluginDataTypes import IOPluginMapType
from core.types.PluginDataTypes import ToolsPluginMap
from core.types.PluginDataTypes import InputPluginMap
from core.types.PluginDataTypes import OutputPluginMap
from core.types.PluginDataTypes import PluginList
from core.types.PluginDataTypes import PluginIDMap
from core.types.PluginDataTypes import PluginType
Expand Down Expand Up @@ -68,12 +69,10 @@ def init(self, *args, **kwargs):

self.logger: Logger = getLogger(__name__)

# These are built later on
self._toolPluginsIDMap: PluginIDMap = cast(PluginIDMap, None)
self._inputPluginsIDMap: PluginIDMap = cast(PluginIDMap, None)
self._outputPluginsIDMap: PluginIDMap = cast(PluginIDMap, None)
self._inputPluginsMap: IOPluginMap = cast(IOPluginMap, None)
self._outputPluginsMap: IOPluginMap = cast(IOPluginMap, None)
# These are lazily built
self._toolPluginsMap: ToolsPluginMap = ToolsPluginMap()
self._inputPluginsMap: InputPluginMap = InputPluginMap()
self._outputPluginsMap: OutputPluginMap = OutputPluginMap()

self._ioPluginClasses: PluginList = PluginList([])
self._toolPluginClasses: PluginList = PluginList([])
Expand Down Expand Up @@ -124,53 +123,29 @@ def toolPlugins(self) -> PluginList:
return self._toolPluginClasses

@property
def toolPluginsIDMap(self) -> PluginIDMap:
if self._toolPluginsIDMap is None:
self._toolPluginsIDMap = self.__mapWxIdsToPlugins(self.toolPlugins)
return self._toolPluginsIDMap

@property
def inputPluginsIDMap(self) -> PluginIDMap:
if self._inputPluginsIDMap is None:
self._inputPluginsIDMap = self.__mapWxIdsToPlugins(self.inputPlugins)
return self._inputPluginsIDMap
def toolPluginsMap(self) -> ToolsPluginMap:
if len(self._toolPluginsMap.pluginIdMap) == 0:
self._toolPluginsMap.pluginIdMap = self.__mapWxIdsToPlugins(self.toolPlugins)
return self._toolPluginsMap

@property
def outputPluginsIDMap(self) -> PluginIDMap:
if self._outputPluginsIDMap is None:
self._outputPluginsIDMap = self.__mapWxIdsToPlugins(self.outputPlugins)
return self._outputPluginsIDMap

@property
def inputPluginsMap(self) -> IOPluginMap:

if self._inputPluginsMap is None:
self._inputPluginsMap = IOPluginMap()

self._inputPluginsMap.mapType = IOPluginMapType.INPUT_MAP
def inputPluginsMap(self) -> InputPluginMap:
if len(self._inputPluginsMap.pluginIdMap) == 0:
self._inputPluginsMap.pluginIdMap = self.__mapWxIdsToPlugins(self.inputPlugins)

return self._inputPluginsMap

@property
def outputPluginsMap(self) -> IOPluginMap:

if self._outputPluginsMap is None:
self._outputPluginsMap = IOPluginMap()

self._outputPluginsMap.mapType = IOPluginMapType.OUTPUT_MAP
def outputPluginsMap(self) -> OutputPluginMap:
if len(self._outputPluginsMap.pluginIdMap) == 0:
self._outputPluginsMap.pluginIdMap = self.__mapWxIdsToPlugins(self.outputPlugins)

return self._outputPluginsMap

def doToolAction(self, wxId: int):
"""
Args:
wxId: The ID ref of the menu item
"""

pluginMap: PluginIDMap = self.toolPluginsIDMap
pluginMap: PluginIDMap = self.toolPluginsMap.pluginIdMap

# TODO: Fix this later for mypy
clazz: type = pluginMap[wxId] # type: ignore
Expand All @@ -189,23 +164,20 @@ def doToolAction(self, wxId: int):

def doImport(self, wxId: int):
"""
Args:
wxId: The ID ref of the menu item
"""
idMap: PluginIDMap = self.inputPluginsMap.pluginIdMap
idMap: PluginIDMap = self.inputPluginsMap.pluginIdMap
clazz: type = idMap[wxId] # type: ignore
plugInstance: IOPluginInterface = clazz(pluginAdapter=self._pluginAdapter)
self._doIOAction(methodToCall=plugInstance.executeImport)

def doExport(self, wxId: int):
pass
"""
Args:
wxId: The ID ref of the menu item
"""
idMap: PluginIDMap = self.outputPluginsMap.pluginIdMap
idMap: PluginIDMap = self.outputPluginsMap.pluginIdMap
clazz: type = idMap[wxId] # type: ignore
plugInstance: IOPluginInterface = clazz(pluginAdapter=self._pluginAdapter)
self._doIOAction(methodToCall=plugInstance.executeExport)
Expand Down
48 changes: 44 additions & 4 deletions core/types/PluginDataTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,52 @@ def createPlugIdMapFactory() -> PluginIDMap:
PluginDescription = NewType('PluginDescription', str)


class IOPluginMapType(Enum):
class PluginMapType(Enum):
INPUT_MAP = 'InputMap'
OUTPUT_MAP = 'OutputMap'
TOOL_MAP = 'ToolMap'


#
# Some nice syntactic sugar
#
@dataclass
class BasePluginMap:
mapType: PluginMapType = PluginMapType.INPUT_MAP
pluginIdMap: PluginIDMap = field(default_factory=createPlugIdMapFactory)

def __init__(self):
self.pluginIdMap = PluginIDMap({})

def __str__(self) -> str:
return self.__repr__()

def __repr__(self) -> str:
return f'{self.mapType} plugin count: {len(self.pluginIdMap)}'


@dataclass
class InputPluginMap(BasePluginMap):
def __init__(self):
super().__init__()
self.mapType = PluginMapType.INPUT_MAP

def __str__(self) -> str:
return self.__repr__()

def __repr__(self) -> str:
return f'{self.mapType} plugin count: {len(self.pluginIdMap)}'


@dataclass
class OutputPluginMap(BasePluginMap):
def __init__(self):
super().__init__()
self.mapType = PluginMapType.OUTPUT_MAP


@dataclass
class IOPluginMap:
mapType: IOPluginMapType = IOPluginMapType.INPUT_MAP
pluginIdMap: PluginIDMap = field(default_factory=createPlugIdMapFactory)
class ToolsPluginMap(BasePluginMap):
def __init__(self):
super().__init__()
self.mapType = PluginMapType.TOOL_MAP
2 changes: 1 addition & 1 deletion scripts/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ rm -rf dist build
find . -type d -name '*'.egg-info -delete

rm -rf .eggs
rm -rfv core.egg-info
rm -rfv pyutplugincore.egg-info
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="pyutplugincore",
version="0.5.6",
version="0.5.7",
author='Humberto A. Sanchez II',
author_email='[email protected]',
maintainer='Humberto A. Sanchez II',
Expand Down
5 changes: 0 additions & 5 deletions tests/core/TestPluginManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ def testInputPluginsProperty(self):
inputPlugins = self.pluginManager.inputPlugins
self.assertIsNotNone(inputPlugins, 'Oh no !!')

def testToolPluginsWxIdGenerated(self):

pluginMap: PluginIDMap = self.pluginManager.toolPluginsIDMap
self.assertIsNotNone(pluginMap, 'Where is my map')


def suite() -> TestSuite:
"""You need to change the name of the test class here also."""
Expand Down
34 changes: 17 additions & 17 deletions tests/scaffoldv2/ScaffoldFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from os import getcwd

from dataclasses import dataclass
from typing import Union

from wx import ACCEL_CTRL
from wx import FD_FILE_MUST_EXIST
Expand All @@ -31,14 +32,14 @@
from wx import MessageDialog
from wx import NewIdRef

from wx import Yield as wxYield

from core.IOPluginInterface import IOPluginInterface
from core.PluginManager import PluginManager
from core.ToolPluginInterface import ToolPluginInterface
from core.types.PluginDataTypes import InputPluginMap
from core.types.PluginDataTypes import OutputPluginMap

from core.types.PluginDataTypes import IOPluginMap
from core.types.PluginDataTypes import IOPluginMapType
from core.types.PluginDataTypes import PluginMapType
from core.types.PluginDataTypes import PluginIDMap

from tests.scaffoldv2.PluginAdapterV2 import PluginAdapterV2
Expand All @@ -56,10 +57,10 @@ class RequestResponse:
class ScaffoldFrame(Frame):

FRAME_ID: int = 0xDeadBeef
WINDOW_WIDTH: int = 900
WINDOW_HEIGHT: int = 500
WINDOW_WIDTH: int = 1200
WINDOW_HEIGHT: int = 600

def __init__(self, parent=None, wxId=FRAME_ID, size=(800, 600), createEmptyProject: bool = True):
def __init__(self, parent=None, wxId=FRAME_ID, size=(WINDOW_WIDTH, WINDOW_HEIGHT), createEmptyProject: bool = True):

super().__init__(parent=parent, id=wxId, size=size, style=DEFAULT_FRAME_STYLE, title='Test Scaffold for Plugins')

Expand Down Expand Up @@ -140,11 +141,11 @@ def _makeToolsMenu(self, toolsMenu: Menu) -> Menu:
"""
Make the Tools submenu.
"""
pluginMap: PluginIDMap = self._pluginManager.toolPluginsIDMap
idMap: PluginIDMap = self._pluginManager.toolPluginsMap.pluginIdMap

for wxId in pluginMap:
for wxId in idMap:

clazz: type = pluginMap[wxId] # type: ignore
clazz: type = idMap[wxId] # type: ignore

pluginInstance: ToolPluginInterface = clazz(None)
toolsMenu.Append(wxId, pluginInstance.menuTitle)
Expand All @@ -157,31 +158,30 @@ def _makeImportSubMenu(self) -> Menu:
"""
Returns: The import submenu.
"""
pluginMap: IOPluginMap = self._pluginManager.inputPluginsMap
pluginMap: InputPluginMap = self._pluginManager.inputPluginsMap

return self._makeIOSubMenu(pluginMap=pluginMap)

def _makeExportSubMenu(self) -> Menu:
"""
Returns: The export submenu
"""
pluginMap: IOPluginMap = self._pluginManager.outputPluginsMap
pluginMap: OutputPluginMap = self._pluginManager.outputPluginsMap

return self._makeIOSubMenu(pluginMap=pluginMap)

def _makeIOSubMenu(self, pluginMap: IOPluginMap) -> Menu:
def _makeIOSubMenu(self, pluginMap: Union[InputPluginMap, OutputPluginMap]) -> Menu:

subMenu: Menu = Menu()

pluginIDMap: PluginIDMap = pluginMap.pluginIdMap
for wxId in pluginIDMap:
clazz: type = pluginIDMap[wxId] # type: ignore
for wxId in pluginMap.pluginIdMap.keys():
clazz: type = pluginMap.pluginIdMap[wxId] # type: ignore
pluginInstance: IOPluginInterface = clazz(None)

if pluginMap.mapType == IOPluginMapType.INPUT_MAP:
if pluginMap.mapType == PluginMapType.INPUT_MAP:
pluginName: str = pluginInstance.inputFormat.formatName
subMenu = self.__makeSubMenuEntry(subMenu=subMenu, wxId=wxId, pluginName=pluginName, callback=self._onImport)
elif pluginMap.mapType == IOPluginMapType.OUTPUT_MAP:
elif pluginMap.mapType == PluginMapType.OUTPUT_MAP:
pluginName = pluginInstance.outputFormat.formatName
subMenu = self.__makeSubMenuEntry(subMenu=subMenu, wxId=wxId, pluginName=pluginName, callback=self._onExport)
else:
Expand Down
2 changes: 0 additions & 2 deletions tests/scaffoldv2/ScaffoldUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

from ogl.OglInterface2 import OglInterface2

from core.IPluginAdapter import IPluginAdapter
from core.types.Types import FrameInformation
from core.types.Types import FrameInformationCallback
from core.types.Types import FrameSize
Expand All @@ -47,7 +46,6 @@
from core.types.Types import OglLinks
from core.types.Types import SelectedOglObjectsCallback

from tests.scaffoldv2.PluginAdapterV2 import PluginAdapterV2
from tests.scaffoldv2.PyutDiagramType import PyutDiagramType
from tests.scaffoldv2.PyutDocument import PyutDocument
from tests.scaffoldv2.PyutProject import PyutProject
Expand Down

0 comments on commit c68f30d

Please sign in to comment.