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.DebyeModel import DebyeModel |
---|
108 | self.indep_model.append(DebyeModel ) |
---|
109 | |
---|
110 | from sans.models.FractalModel import FractalModel |
---|
111 | class FractalAbsModel(FractalModel): |
---|
112 | def _Fractal(self, x): |
---|
113 | return FractalModel._Fractal(self, math.fabs(x)) |
---|
114 | self.indep_model.append(FractalAbsModel) |
---|
115 | |
---|
116 | from sans.models.LorentzModel import LorentzModel |
---|
117 | self.indep_model.append( LorentzModel) |
---|
118 | |
---|
119 | from sans.models.PowerLawModel import PowerLawModel |
---|
120 | class PowerLawAbsModel(PowerLawModel): |
---|
121 | def _PowerLaw(self, x): |
---|
122 | try: |
---|
123 | return PowerLawModel._PowerLaw(self, math.fabs(x)) |
---|
124 | except: |
---|
125 | print sys.exc_value |
---|
126 | self.indep_model.append( PowerLawAbsModel ) |
---|
127 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
128 | self.indep_model.append(TeubnerStreyModel ) |
---|
129 | |
---|
130 | #Looking for plugins |
---|
131 | self.plugins = findModels() |
---|
132 | |
---|
133 | return 0 |
---|
134 | |
---|
135 | |
---|
136 | def populate_menu(self, modelmenu, event_owner): |
---|
137 | """ |
---|
138 | Populate a menu with our models |
---|
139 | |
---|
140 | @param id: first menu event ID to use when binding the menu events |
---|
141 | @param modelmenu: wx.Menu object to populate |
---|
142 | @param event_owner: wx object to bind the menu events to |
---|
143 | @return: the next free event ID following the new menu events |
---|
144 | """ |
---|
145 | self._getModelList() |
---|
146 | self.event_owner = event_owner |
---|
147 | shape_submenu= wx.Menu() |
---|
148 | indep_submenu = wx.Menu() |
---|
149 | added_models = wx.Menu() |
---|
150 | for id_str,value in self.model_list.iteritems(): |
---|
151 | item = self.model_list[id_str]() |
---|
152 | name = item.__class__.__name__ |
---|
153 | if hasattr(item, "name"): |
---|
154 | name = item.name |
---|
155 | self.model_list_box[name] =value |
---|
156 | shape_submenu.Append(int(id_str), name, name) |
---|
157 | wx.EVT_MENU(event_owner, int(id_str), self._on_model) |
---|
158 | modelmenu.AppendMenu(wx.NewId(), "Shapes...", shape_submenu, "List of shape-based models") |
---|
159 | id = wx.NewId() |
---|
160 | if len(self.indep_model_list) == 0: |
---|
161 | for items in self.indep_model: |
---|
162 | #if item not in self.indep_model_list.values(): |
---|
163 | #self.indep_model_list[str(id)] = item |
---|
164 | self.model_list[str(id)]=items |
---|
165 | item=items() |
---|
166 | name = item.__class__.__name__ |
---|
167 | if hasattr(item, "name"): |
---|
168 | name = item.name |
---|
169 | indep_submenu.Append(id,name, name) |
---|
170 | self.model_list_box[name] =items |
---|
171 | wx.EVT_MENU(event_owner, int(id), self._on_model) |
---|
172 | id = wx.NewId() |
---|
173 | modelmenu.AppendMenu(wx.NewId(), "Shape-independent...", indep_submenu, "List of shape-independent models") |
---|
174 | id = wx.NewId() |
---|
175 | if len(self.custom_models) == 0: |
---|
176 | for items in self.plugins: |
---|
177 | #if item not in self.custom_models.values(): |
---|
178 | #self.custom_models[str(id)] = item |
---|
179 | self.model_list[str(id)]=items |
---|
180 | name = items.__name__ |
---|
181 | if hasattr(items, "name"): |
---|
182 | name = items.name |
---|
183 | added_models.Append(id, name, name) |
---|
184 | self.model_list_box[name] =items |
---|
185 | wx.EVT_MENU(event_owner, int(id), self._on_model) |
---|
186 | id = wx.NewId() |
---|
187 | modelmenu.AppendMenu(wx.NewId(),"Added models...", added_models, "List of additional models") |
---|
188 | return 0 |
---|
189 | |
---|
190 | def _on_model(self, evt): |
---|
191 | """ |
---|
192 | React to a model menu event |
---|
193 | @param event: wx menu event |
---|
194 | """ |
---|
195 | if str(evt.GetId()) in self.model_list.keys(): |
---|
196 | # Notify the application manager that a new model has been set |
---|
197 | #self.app_manager.set_model(self.model_list[str(evt.GetId())]()) |
---|
198 | |
---|
199 | #TODO: post a model event to update all panels that need |
---|
200 | #evt = ModelEvent(model=self.model_list[str(evt.GetId())]()) |
---|
201 | |
---|
202 | model = self.model_list[str(evt.GetId())] |
---|
203 | evt = ModelEvent(model= model ) |
---|
204 | wx.PostEvent(self.event_owner, evt) |
---|
205 | |
---|
206 | def get_model_list(self): |
---|
207 | """ @ return dictionary of models for fitpanel use """ |
---|
208 | return self.model_list_box |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | |
---|