Browse Source

DynLoader hinzu fügen

Refs #1
Patrick Baumgartner 4 years ago
parent
commit
e98dc9b867
7 changed files with 177 additions and 2 deletions
  1. 2 0
      .gitignore
  2. BIN
      .vs/Trixy/v17/.suo
  3. 3 0
      DynLoader/__init__.py
  4. 40 0
      DynLoader/main.py
  5. 120 0
      DynLoader/modInfo.py
  6. 10 0
      Trixy.pyproj
  7. 2 2
      main.py

+ 2 - 0
.gitignore

@@ -1 +1,3 @@
 /.vs
+/__pycache__
+/DynLoader/__pycache__

BIN
.vs/Trixy/v17/.suo


+ 3 - 0
DynLoader/__init__.py

@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from DynLoader.main import DynLoaderMod

+ 40 - 0
DynLoader/main.py

@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import os
+from DynLoader.modInfo import modInfo
+
+
+class DynLoaderMod(object):
+
+
+    instance = None
+    @staticmethod
+    def getInstance():
+        if DynLoaderMod.instance==None:
+            DynLoaderMod.instance = DynLoaderMod()
+        return DynLoaderMod.instance
+
+    def __init__(self,dir,method_prefix=""):
+        self._modules = []
+        files = os.listdir(dir)
+        self._prefix = method_prefix
+        _mod = modInfo.modInfo
+        for f in files:
+            if f!='__pycache__' and f.endswith(".py"):
+                self._modules.append({
+                    "file": f,
+                    "modInfo": modInfo(dir+"/"+f)
+                })
+        
+    def __del__(self):
+        for x in range(self._modules):
+            del self._modules[x]
+        del self._modules
+
+    def execute(self, method, arg1 = None, arg2 = None, arg3 = None):
+        for m in self._modules:
+            if m["modInfo"].Loaded:
+                m["modInfo"].Execute(method, arg1, arg2, arg3)
+
+
+    

+ 120 - 0
DynLoader/modInfo.py

@@ -0,0 +1,120 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import os
+import importlib
+import sys
+
+
+class modInfo(object):
+
+    def __init__(self, path):
+        self._loaded = False
+        self._size = 0
+        self._path = ""
+        self._mod = None
+        self.Path = path
+
+
+    def __del__(self):
+        self.Unload()
+        del self._loaded
+        del self._mod
+        del self._path
+        del self._size
+
+
+    def Unload(self):
+        if self._loaded == False:
+            return
+        self._loaded = False
+
+
+
+    def Execute(self, methode, arg1 = None, arg2 = None,arg3 = None):
+        if self._loaded == False:
+            return
+        methode = getattr(self._mod, methode)
+        if methode:
+            if arg1 == None and arg2==None and arg3 == None:
+                methode()
+            elif arg2 == None and arg3 == None:
+                methode(arg1)
+            elif arg3 == None:
+                methode(arg1, arg2)
+            else:
+                methode(arg1, arg2, arg3)
+
+
+
+
+    def _loadModule(self, moduleName):
+        module = None
+        moduleName = moduleName.replace("/",".")
+        try:
+            del sys.modules[moduleName]
+        except BaseException as err:
+            pass
+        try:
+            module = importlib.import_module(moduleName)
+        except BaseException as err:
+            serr = str(err)
+            print("Error loading module '" + moduleName + "': " + serr)
+        return module
+
+    def _reloadModule(self, moduleName):
+        module = loadModule(moduleName)
+        moduleName = moduleName.replace("/",".")
+        moduleName, modulePath = str(module).replace("' from '", "||").replace("<module '", '').replace("'>", '').split("||")
+        if (modulePath.endswith(".pyc")):
+            import os
+            os.remove(modulePath)
+            module = loadModule(moduleName)
+        return module
+
+    def _getInstance(self, moduleName, param1, param2, param3):
+        module = reloadModule(moduleName)
+        instance = eval("module." + moduleName + "(param1, param2, param3)")
+        return instance
+
+
+
+    @property
+    def Path(self):
+        return self._path
+
+    @Path.setter
+    def Path(self,value):
+        if self._path == value: 
+            return
+        print("Set Path to "+value)
+
+        self.Unload()
+        if value == "":
+            return
+        self._path = value
+        if os.path.isfile(value):
+            self._size = os.path.getsize(self._path)
+            try:
+                #self._mod = __import__(self._path)
+                self._mod = self._loadModule(self._path)
+                self._loaded = True
+            except ImportError:
+                self._mod = None
+                self._loaded = False
+        else:
+            self._mod = None
+            self._loaded = False
+            self._size = 0
+
+
+    @property
+    def Size(self):
+        return self._size
+
+    @property
+    def Module(self):
+        return self._mod
+
+    @property
+    def Loaded(self):
+        return self._loaded

+ 10 - 0
Trixy.pyproj

@@ -21,13 +21,23 @@
     <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
   </PropertyGroup>
   <ItemGroup>
+    <Compile Include="DynLoader\main.py" />
+    <Compile Include="DynLoader\modInfo.py" />
+    <Compile Include="DynLoader\__init__.py" />
     <Compile Include="main.py" />
   </ItemGroup>
   <ItemGroup>
     <Content Include=".gitignore" />
+    <Content Include="DynLoader\__pycache__\modInfo.cpython-37.pyc" />
+    <Content Include="DynLoader\__pycache__\__init__.cpython-37.pyc" />
     <Content Include="LICENSE" />
     <Content Include="README.md" />
   </ItemGroup>
+  <ItemGroup>
+    <Folder Include="DynLoader\" />
+    <Folder Include="DynLoader\__pycache__\" />
+    <Folder Include="mods\" />
+  </ItemGroup>
   <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
   <!-- Uncomment the CoreCompile target to enable the Build command in
        Visual Studio and specify your pre- and post-build commands in

+ 2 - 2
main.py

@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-
+import DynLoader as dyn
 
 if __name__ == "__main__":
-    pass
+    mod = dyn.DynLoaderMod("mods/")