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 | module = imp.load_module( name, file, item, info ) |
---|
43 | if hasattr(module, "Model"): |
---|
44 | try: |
---|
45 | plugins.append(module.Model) |
---|
46 | except: |
---|
47 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
48 | except: |
---|
49 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
50 | finally: |
---|
51 | if not file==None: |
---|
52 | file.close() |
---|
53 | except: |
---|
54 | pass |
---|
55 | return plugins |
---|
56 | class ModelManager: |
---|
57 | |
---|
58 | ## Dictionary of models |
---|
59 | model_list = {} |
---|
60 | indep_model_list = {} |
---|
61 | model_list_box = {} |
---|
62 | custom_models={} |
---|
63 | plugins=[] |
---|
64 | indep_model=[] |
---|
65 | ## Event owner |
---|
66 | event_owner = None |
---|
67 | |
---|
68 | def _getModelList(self): |
---|
69 | """ |
---|
70 | List of models we want to make available by default |
---|
71 | for this application |
---|
72 | |
---|
73 | @param id: first event ID to register the menu events with |
---|
74 | @return: the next free event ID following the new menu events |
---|
75 | """ |
---|
76 | self.model_list = {} |
---|
77 | self.model_list_box = {} |
---|
78 | model_info="shape-based models" |
---|
79 | from sans.models.CylinderModel import CylinderModel |
---|
80 | self.model_list[str(wx.NewId())] = ModelInfo(CylinderModel , model_info) |
---|
81 | |
---|
82 | from sans.models.SphereModel import SphereModel |
---|
83 | self.model_list[str(wx.NewId())] = ModelInfo(SphereModel , model_info) |
---|
84 | |
---|
85 | from sans.guitools.LineModel import LineModel |
---|
86 | self.model_list[str(wx.NewId())] = ModelInfo(LineModel , model_info) |
---|
87 | model_info="shape-independent models" |
---|
88 | from sans.models.Lorentzian import Lorentzian |
---|
89 | self.indep_model.append( ModelInfo(Lorentzian , model_info) ) |
---|
90 | |
---|
91 | #Looking for plugins |
---|
92 | self.plugins = findModels() |
---|
93 | |
---|
94 | return 0 |
---|
95 | |
---|
96 | |
---|
97 | def populate_menu(self, modelmenu, event_owner): |
---|
98 | """ |
---|
99 | Populate a menu with our models |
---|
100 | |
---|
101 | @param id: first menu event ID to use when binding the menu events |
---|
102 | @param modelmenu: wx.Menu object to populate |
---|
103 | @param event_owner: wx object to bind the menu events to |
---|
104 | @return: the next free event ID following the new menu events |
---|
105 | """ |
---|
106 | self._getModelList() |
---|
107 | self.event_owner = event_owner |
---|
108 | |
---|
109 | shape_submenu= wx.Menu() |
---|
110 | indep_submenu = wx.Menu() |
---|
111 | added_models = wx.Menu() |
---|
112 | |
---|
113 | for id_str,value in self.model_list.iteritems(): |
---|
114 | item = self.model_list[id_str] |
---|
115 | name = item.model.__name__ |
---|
116 | if hasattr(item, "name"): |
---|
117 | name = item.model.name |
---|
118 | self.model_list_box[name] =value.model |
---|
119 | shape_submenu.Append(int(id_str), name, name) |
---|
120 | wx.EVT_MENU(event_owner, int(id_str), self._on_model) |
---|
121 | modelmenu.AppendMenu(wx.NewId(), "Shapes...", shape_submenu, "List of shape-based models") |
---|
122 | |
---|
123 | id = wx.NewId() |
---|
124 | if len(self.indep_model_list) == 0: |
---|
125 | for item in self.indep_model: |
---|
126 | if item not in self.indep_model_list.values(): |
---|
127 | self.indep_model_list[str(id)] = item |
---|
128 | self.model_list[str(id)]=item |
---|
129 | if hasattr(item, "name"): |
---|
130 | name = item.model.name |
---|
131 | else: |
---|
132 | name = item.model.__name__ |
---|
133 | indep_submenu.Append(id,name, name) |
---|
134 | self.model_list_box[name] =item.model |
---|
135 | wx.EVT_MENU(event_owner, int(id), self._on_model) |
---|
136 | id = wx.NewId() |
---|
137 | modelmenu.AppendMenu(wx.NewId(), "Shape-independent...", indep_submenu, "List of shape-independent models") |
---|
138 | |
---|
139 | |
---|
140 | |
---|
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 | return 0 |
---|
159 | |
---|
160 | def _on_model(self, evt): |
---|
161 | """ |
---|
162 | React to a model menu event |
---|
163 | @param event: wx menu event |
---|
164 | """ |
---|
165 | if str(evt.GetId()) in self.model_list.keys(): |
---|
166 | # Notify the application manager that a new model has been set |
---|
167 | #self.app_manager.set_model(self.model_list[str(evt.GetId())]()) |
---|
168 | |
---|
169 | #TODO: post a model event to update all panels that need |
---|
170 | #evt = ModelEvent(model=self.model_list[str(evt.GetId())]()) |
---|
171 | |
---|
172 | model = self.model_list[str(evt.GetId())] |
---|
173 | evt = ModelEvent(modelinfo=model) |
---|
174 | wx.PostEvent(self.event_owner, evt) |
---|
175 | |
---|
176 | def get_model_list(self): |
---|
177 | """ @ return dictionary of models for fitpanel use """ |
---|
178 | return self.model_list_box |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|