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