Added modular directory structure.
[hooke.git] / hooke / hooke.py
old mode 100755 (executable)
new mode 100644 (file)
index 55f23b1..75cc7d9
@@ -10,8 +10,7 @@ Copyright (C) 2008 Massimo Sandal (University of Bologna, Italy).
 This program is released under the GNU General Public License version 2.\r
 '''\r
 \r
-from libhooke import HOOKE_VERSION\r
-from libhooke import WX_GOOD\r
+from .libhooke import HOOKE_VERSION, WX_GOOD\r
 \r
 import os\r
 \r
@@ -24,12 +23,12 @@ from wx.lib.newevent import NewEvent
 import matplotlib.numerix as nx\r
 import scipy as sp\r
 \r
-from threading import *\r
+from threading import Thread\r
 import Queue\r
 \r
-from hooke_cli import HookeCli\r
-from libhooke import *\r
-import libhookecurve as lhc\r
+from .hooke_cli import HookeCli\r
+from .libhooke import HookeConfig, ClickedPoint\r
+from . import libhookecurve as lhc\r
 \r
 #import file versions, just to know with what we're working...\r
 from hooke_cli import __version__ as hookecli_version\r
@@ -64,15 +63,20 @@ plugin_commands_namespaces=[]
 plugin_gui_namespaces=[]\r
 for plugin_name in config['plugins']:\r
     try:\r
-        plugin=__import__(plugin_name)\r
+        hooke_module=__import__('hooke.plugin.'+plugin_name)\r
+        plugin = getattr(hooke_module.plugin, plugin_name)\r
         try:\r
-            eval('CLI_PLUGINS.append(plugin.'+plugin_name+'Commands)') #take Command plugin classes\r
-            plugin_commands_namespaces.append(dir(eval('plugin.'+plugin_name+'Commands')))\r
+            #take Command plugin classes\r
+            commands = getattr(plugin, plugin_name+'Commands')\r
+            CLI_PLUGINS.append(commands)\r
+            plugin_commands_namespaces.append(dir(commands))\r
         except:\r
             pass\r
         try:\r
-            eval('GUI_PLUGINS.append(plugin.'+plugin_name+'Gui)') #take Gui plugin classes\r
-            plugin_gui_namespaces.append(dir(eval('plugin.'+plugin_name+'Gui')))\r
+            #take Gui plugin classes\r
+            gui = getattr(plugin, plugin_name+'Gui')\r
+            GUI_PLUGINS.append(gui)\r
+            plugin_gui_namespaces.append(dir(gui))\r
         except:\r
             pass\r
     except ImportError:\r
@@ -82,8 +86,11 @@ for plugin_name in config['plugins']:
         print 'Imported plugin ',plugin_name\r
 \r
 #eliminate names common to all namespaces\r
-for i in range(len(plugin_commands_namespaces)):\r
-    plugin_commands_namespaces[i]=[item for item in plugin_commands_namespaces[i] if (item != '__doc__' and item != '__module__' and item != '_plug_init')]\r
+for i,namespace in enumerate(plugin_commands_namespaces):\r
+    plugin_commands_namespaces[i] = \\r
+        filter(lambda item : not (item.startswith('__')\r
+                                  or item == '_plug_init'),\r
+               namespace)\r
 #check for conflicts in namespaces between plugins\r
 #FIXME: only in commands now, because I don't have Gui plugins to check\r
 #FIXME: how to check for plugin-defined variables (self.stuff) ??\r
@@ -94,7 +101,8 @@ for namespace,plugin_name in zip(plugin_commands_namespaces, config['plugins']):
     for item in namespace:\r
         if item in plugin_commands_names:\r
             i=plugin_commands_names.index(item) #we exploit the fact index gives the *first* occurrence of a name...\r
-            print 'Error. Plugin ',plugin_name,' defines a function already defined by ',whatplugin_defines[i],'!'\r
+            print 'Error. Plugin %s defines a function %s already defined by %s!' \\r
+                % (plugin_name, item, whatplugin_defines[i])\r
             print 'This should not happen. Please disable one or both plugins and contact the plugin authors to solve the conflict.'\r
             print 'Hooke cannot continue.'\r
             exit()\r
@@ -110,9 +118,10 @@ FILE_DRIVERS=[]
 LOADED_DRIVERS=[]\r
 for driver_name in config['drivers']:\r
     try:\r
-        driver=__import__(driver_name)\r
+        hooke_module=__import__('hooke.driver.'+driver_name)\r
+        driver = getattr(hooke_module.driver, driver_name)\r
         try:\r
-            eval('FILE_DRIVERS.append(driver.'+driver_name+'Driver)')\r
+            FILE_DRIVERS.append(getattr(driver, driver_name+'Driver'))\r
         except:\r
             pass\r
     except ImportError:\r
@@ -353,7 +362,8 @@ class MainWindow(wx.Frame):
         #make sure we execute _plug_init() for every command line plugin we import\r
         for plugin_name in config['plugins']:\r
             try:\r
-                plugin=__import__(plugin_name)\r
+                hooke_module=__import__('hooke.plugin.'+plugin_name)\r
+                plugin = getattr(hooke_module.plugin, plugin_name)\r
                 try:\r
                     eval('plugin.'+plugin_name+'Gui._plug_init(self)')\r
                     pass\r
@@ -760,7 +770,9 @@ class MySplashScreen(wx.SplashScreen):
         # This is a recipe to a the screen.\r
         # Modify the following variables as necessary.\r
         #aBitmap = wx.Image(name = "wxPyWiki.jpg").ConvertToBitmap()\r
-        aBitmap=wx.Image(name='hooke.jpg').ConvertToBitmap()\r
+        aBitmap=wx.Image(name=os.path.join(\r
+                config['install']['docpath'],\r
+                'hooke.jpg')).ConvertToBitmap()\r
         splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT\r
         splashDuration = 2000 # milliseconds\r
         splashCallback = None\r
@@ -783,16 +795,6 @@ class MySplashScreen(wx.SplashScreen):
 #------------------------------------------------------------------------------\r
 \r
 def main():\r
-\r
-    #save the directory where Hooke is located\r
-    config['hookedir']=os.getcwd()\r
-\r
-    #now change to the working directory.\r
-    try:\r
-        os.chdir(config['workdir'])\r
-    except OSError:\r
-        print "Warning: Invalid work directory."\r
-\r
     app=wx.PySimpleApp()\r
 \r
     def make_gui_class(*bases):\r
@@ -808,7 +810,6 @@ def main():
     my_cmdline=CliThread(main_frame, list_of_events)\r
     my_cmdline.start()\r
 \r
-\r
     app.MainLoop()\r
 \r
 if __name__ == '__main__':\r