source: sasview/sansview/perspectives/fitting/models.py @ aa92772

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since aa92772 was aa92772, checked in by Gervaise Alina <gervyh@…>, 16 years ago

moving plugins folder

  • Property mode set to 100644
File size: 4.3 KB
Line 
1import wx
2import os
3import os.path
4
5(ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent()
6def findModels():
7    print "looking for models"
8    try:
9        cwd= os.path.split(__file__)[0]
10    except:
11        cwd= os.getcwd()
12    print "models cwd",cwd
13    dir=os.path.join(cwd,'plugins')
14    print "models: find plugins",dir
15    if os.path.isdir(dir):
16        return _findModels(dir)
17    else:
18        return []
19   
20   
21   
22def _findModels(dir):
23    # List of plugin objects
24    plugins = []
25    # Go through files in plug-in directory
26    try:
27        list = os.listdir(dir)
28        for item in list:
29            print "models: _findModels:",item
30            toks = os.path.splitext(os.path.basename(item))
31            print "models: toks:",toks
32            if toks[1]=='.py' and not toks[0]=='__init__':
33                name = toks[0]
34           
35                path = [os.path.abspath(dir)]
36                file = None
37                try:
38                    (file, path, info) = imp.find_module(name, path)
39                    module = imp.load_module( name, file, item, info )
40                    if hasattr(module, "Model"):
41                        try:
42                            plugins.append(module.Model)
43                        except:
44                            log("Error accessing Model in %s\n  %s" % (name, sys.exc_value))
45                except:
46                    pass
47                finally:
48                    if not file==None:
49                        file.close()
50    except:
51        pass
52    return plugins
53   
54
55
56class ModelManager:
57   
58    ## Dictionary of models
59    model_list = {}
60    model_list_box = {}
61    ## Event owner
62    event_owner = None
63   
64    def _getModelList(self):
65        """
66            List of models we want to make available by default
67            for this application
68           
69            @param id: first event ID to register the menu events with
70            @return: the next free event ID following the new menu events
71        """
72        self.model_list = {}
73        self.model_list_box = {}
74        from sans.models.CylinderModel import CylinderModel
75        self.model_list[str(wx.NewId())] = CylinderModel
76     
77        from sans.models.SphereModel import SphereModel
78        self.model_list[str(wx.NewId())] = SphereModel
79   
80        from sans.guitools.LineModel import LineModel
81        self.model_list[str(wx.NewId())]  = LineModel
82     
83        return 0
84
85   
86    def populate_menu(self, modelmenu, event_owner):
87        """
88            Populate a menu with our models
89           
90            @param id: first menu event ID to use when binding the menu events
91            @param modelmenu: wx.Menu object to populate
92            @param event_owner: wx object to bind the menu events to
93            @return: the next free event ID following the new menu events
94        """
95       
96        self._getModelList()
97        self.event_owner = event_owner
98
99        for id_str,value in self.model_list.iteritems():
100            item = self.model_list[id_str]
101           
102            name = item.__name__
103            if hasattr(item, "name"):
104                name = item.name
105               
106            self.model_list_box[name] =value
107           
108            modelmenu.Append(int(id_str), name, name)
109            wx.EVT_MENU(event_owner, int(id_str), self._on_model)       
110        plugings=findModels()
111        print "models: plugings",plugings
112        return 0
113   
114    def _on_model(self, evt):
115        """
116            React to a model menu event
117            @param event: wx menu event
118        """
119        if str(evt.GetId()) in self.model_list.keys():
120            # Notify the application manager that a new model has been set
121            #self.app_manager.set_model(self.model_list[str(evt.GetId())]())
122           
123            #TODO: post a model event to update all panels that need
124            #evt = ModelEvent(model=self.model_list[str(evt.GetId())]())
125           
126            model = self.model_list[str(evt.GetId())]()
127            evt = ModelEvent(model=model)
128            wx.PostEvent(self.event_owner, evt)
129       
130    def get_model_list(self):   
131        """ @ return dictionary of models for fitpanel use """
132        return self.model_list_box
133   
134   
135   
136 
Note: See TracBrowser for help on using the repository browser.