1 | import wx |
---|
2 | import imp |
---|
3 | import os,sys,math |
---|
4 | import os.path |
---|
5 | |
---|
6 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | def log(message): |
---|
12 | print message |
---|
13 | out = open("plugins.log", 'a') |
---|
14 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
15 | out.close() |
---|
16 | |
---|
17 | def findModels(): |
---|
18 | print "looking for models" |
---|
19 | if os.path.isdir('plugins'): |
---|
20 | return _findModels('plugins') |
---|
21 | return [] |
---|
22 | |
---|
23 | def _findModels(dir): |
---|
24 | # List of plugin objects |
---|
25 | plugins = [] |
---|
26 | # Go through files in plug-in directory |
---|
27 | try: |
---|
28 | list = os.listdir(dir) |
---|
29 | for item in list: |
---|
30 | toks = os.path.splitext(os.path.basename(item)) |
---|
31 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
32 | name = toks[0] |
---|
33 | |
---|
34 | path = [os.path.abspath(dir)] |
---|
35 | file = None |
---|
36 | try: |
---|
37 | (file, path, info) = imp.find_module(name, path) |
---|
38 | module = imp.load_module( name, file, item, info ) |
---|
39 | if hasattr(module, "Model"): |
---|
40 | try: |
---|
41 | plugins.append(module.Model) |
---|
42 | except: |
---|
43 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
44 | except: |
---|
45 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
46 | finally: |
---|
47 | |
---|
48 | if not file==None: |
---|
49 | file.close() |
---|
50 | except: |
---|
51 | pass |
---|
52 | return plugins |
---|
53 | class ModelManager: |
---|
54 | |
---|
55 | ## Dictionary of models |
---|
56 | model_list = {} |
---|
57 | indep_model_list = {} |
---|
58 | model_list_box = {} |
---|
59 | custom_models={} |
---|
60 | plugins=[] |
---|
61 | indep_model=[] |
---|
62 | ## Event owner |
---|
63 | event_owner = None |
---|
64 | |
---|
65 | def _getModelList(self): |
---|
66 | """ |
---|
67 | List of models we want to make available by default |
---|
68 | for this application |
---|
69 | |
---|
70 | @param id: first event ID to register the menu events with |
---|
71 | @return: the next free event ID following the new menu events |
---|
72 | """ |
---|
73 | self.model_list = {} |
---|
74 | self.model_list_box = {} |
---|
75 | |
---|
76 | |
---|
77 | from sans.models.SphereModel import SphereModel |
---|
78 | self.model_list[str(wx.NewId())] = SphereModel |
---|
79 | |
---|
80 | from sans.models.CylinderModel import CylinderModel |
---|
81 | self.model_list[str(wx.NewId())] =CylinderModel |
---|
82 | |
---|
83 | from sans.models.CoreShellModel import CoreShellModel |
---|
84 | self.model_list[str(wx.NewId())] = CoreShellModel |
---|
85 | |
---|
86 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
87 | self.model_list[str(wx.NewId())] =CoreShellCylinderModel |
---|
88 | |
---|
89 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
90 | self.model_list[str(wx.NewId())] =EllipticalCylinderModel |
---|
91 | |
---|
92 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
93 | self.model_list[str(wx.NewId())] = EllipsoidModel |
---|
94 | |
---|
95 | from sans.models.LineModel import LineModel |
---|
96 | self.model_list[str(wx.NewId())] = LineModel |
---|
97 | |
---|
98 | |
---|
99 | model_info="shape-independent models" |
---|
100 | |
---|
101 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
102 | self.indep_model.append(BEPolyelectrolyte ) |
---|
103 | |
---|
104 | from sans.models.DABModel import DABModel |
---|
105 | self.indep_model.append(DABModel ) |
---|
106 | |
---|
107 | from sans.models.GuinierModel import GuinierModel |
---|
108 | self.indep_model.append(GuinierModel ) |
---|
109 | |
---|
110 | from sans.models.DebyeModel import DebyeModel |
---|
111 | self.indep_model.append(DebyeModel ) |
---|
112 | |
---|
113 | from sans.models.FractalModel import FractalModel |
---|
114 | class FractalAbsModel(FractalModel): |
---|
115 | def _Fractal(self, x): |
---|
116 | return FractalModel._Fractal(self, math.fabs(x)) |
---|
117 | self.indep_model.append(FractalAbsModel) |
---|
118 | |
---|
119 | from sans.models.LorentzModel import LorentzModel |
---|
120 | self.indep_model.append( LorentzModel) |
---|
121 | |
---|
122 | from sans.models.PowerLawModel import PowerLawModel |
---|
123 | class PowerLawAbsModel(PowerLawModel): |
---|
124 | def _PowerLaw(self, x): |
---|
125 | try: |
---|
126 | return PowerLawModel._PowerLaw(self, math.fabs(x)) |
---|
127 | except: |
---|
128 | print sys.exc_value |
---|
129 | self.indep_model.append( PowerLawAbsModel ) |
---|
130 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
131 | self.indep_model.append(TeubnerStreyModel ) |
---|
132 | |
---|
133 | #Looking for plugins |
---|
134 | self.plugins = findModels() |
---|
135 | |
---|
136 | return 0 |
---|
137 | |
---|
138 | |
---|
139 | def populate_menu(self, modelmenu, event_owner): |
---|
140 | """ |
---|
141 | Populate a menu with our models |
---|
142 | |
---|
143 | @param id: first menu event ID to use when binding the menu events |
---|
144 | @param modelmenu: wx.Menu object to populate |
---|
145 | @param event_owner: wx object to bind the menu events to |
---|
146 | @return: the next free event ID following the new menu events |
---|
147 | """ |
---|
148 | self._getModelList() |
---|
149 | self.event_owner = event_owner |
---|
150 | shape_submenu= wx.Menu() |
---|
151 | indep_submenu = wx.Menu() |
---|
152 | added_models = wx.Menu() |
---|
153 | for id_str,value in self.model_list.iteritems(): |
---|
154 | item = self.model_list[id_str]() |
---|
155 | name = item.__class__.__name__ |
---|
156 | if hasattr(item, "name"): |
---|
157 | name = item.name |
---|
158 | self.model_list_box[name] =value |
---|
159 | shape_submenu.Append(int(id_str), name, name) |
---|
160 | wx.EVT_MENU(event_owner, int(id_str), self._on_model) |
---|
161 | modelmenu.AppendMenu(wx.NewId(), "Shapes...", shape_submenu, "List of shape-based models") |
---|
162 | id = wx.NewId() |
---|
163 | if len(self.indep_model_list) == 0: |
---|
164 | for items in self.indep_model: |
---|
165 | #if item not in self.indep_model_list.values(): |
---|
166 | #self.indep_model_list[str(id)] = item |
---|
167 | self.model_list[str(id)]=items |
---|
168 | item=items() |
---|
169 | name = item.__class__.__name__ |
---|
170 | if hasattr(item, "name"): |
---|
171 | name = item.name |
---|
172 | indep_submenu.Append(id,name, name) |
---|
173 | self.model_list_box[name] =items |
---|
174 | wx.EVT_MENU(event_owner, int(id), self._on_model) |
---|
175 | id = wx.NewId() |
---|
176 | modelmenu.AppendMenu(wx.NewId(), "Shape-independent...", indep_submenu, "List of shape-independent models") |
---|
177 | id = wx.NewId() |
---|
178 | if len(self.custom_models) == 0: |
---|
179 | for items in self.plugins: |
---|
180 | #if item not in self.custom_models.values(): |
---|
181 | #self.custom_models[str(id)] = item |
---|
182 | self.model_list[str(id)]=items |
---|
183 | name = items.__name__ |
---|
184 | if hasattr(items, "name"): |
---|
185 | name = items.name |
---|
186 | added_models.Append(id, name, name) |
---|
187 | self.model_list_box[name] =items |
---|
188 | wx.EVT_MENU(event_owner, int(id), self._on_model) |
---|
189 | id = wx.NewId() |
---|
190 | modelmenu.AppendMenu(wx.NewId(),"Added models...", added_models, "List of additional models") |
---|
191 | return 0 |
---|
192 | |
---|
193 | def _on_model(self, evt): |
---|
194 | """ |
---|
195 | React to a model menu event |
---|
196 | @param event: wx menu event |
---|
197 | """ |
---|
198 | if str(evt.GetId()) in self.model_list.keys(): |
---|
199 | # Notify the application manager that a new model has been set |
---|
200 | #self.app_manager.set_model(self.model_list[str(evt.GetId())]()) |
---|
201 | |
---|
202 | #TODO: post a model event to update all panels that need |
---|
203 | #evt = ModelEvent(model=self.model_list[str(evt.GetId())]()) |
---|
204 | |
---|
205 | model = self.model_list[str(evt.GetId())] |
---|
206 | evt = ModelEvent(model= model ) |
---|
207 | wx.PostEvent(self.event_owner, evt) |
---|
208 | |
---|
209 | def get_model_list(self): |
---|
210 | """ @ return dictionary of models for fitpanel use """ |
---|
211 | return self.model_list_box |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | |
---|