[d89f09b] | 1 | import wx |
---|
[b30f001] | 2 | import imp |
---|
| 3 | import os,sys |
---|
[d89f09b] | 4 | import os.path |
---|
| 5 | |
---|
| 6 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
[49b7efa] | 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 |
---|
[b30f001] | 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 | |
---|
[aa92772] | 21 | def findModels(): |
---|
| 22 | print "looking for models" |
---|
[b30f001] | 23 | if os.path.isdir('plugins'): |
---|
| 24 | return _findModels('plugins') |
---|
| 25 | return [] |
---|
[aa92772] | 26 | |
---|
[1c66bc5] | 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: |
---|
[b30f001] | 49 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
[1c66bc5] | 50 | finally: |
---|
| 51 | if not file==None: |
---|
| 52 | file.close() |
---|
| 53 | except: |
---|
| 54 | pass |
---|
| 55 | return plugins |
---|
[d89f09b] | 56 | class ModelManager: |
---|
| 57 | |
---|
| 58 | ## Dictionary of models |
---|
| 59 | model_list = {} |
---|
[49b7efa] | 60 | indep_model_list = {} |
---|
[d89f09b] | 61 | model_list_box = {} |
---|
[b30f001] | 62 | custom_models={} |
---|
| 63 | plugins=[] |
---|
[49b7efa] | 64 | indep_model=[] |
---|
[d89f09b] | 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 = {} |
---|
[49b7efa] | 78 | model_info="shape-based models" |
---|
[d89f09b] | 79 | from sans.models.CylinderModel import CylinderModel |
---|
[49b7efa] | 80 | self.model_list[str(wx.NewId())] = ModelInfo(CylinderModel , model_info) |
---|
[d89f09b] | 81 | |
---|
| 82 | from sans.models.SphereModel import SphereModel |
---|
[49b7efa] | 83 | self.model_list[str(wx.NewId())] = ModelInfo(SphereModel , model_info) |
---|
[d89f09b] | 84 | |
---|
| 85 | from sans.guitools.LineModel import LineModel |
---|
[49b7efa] | 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) ) |
---|
[b30f001] | 90 | |
---|
[49b7efa] | 91 | #Looking for plugins |
---|
| 92 | self.plugins = findModels() |
---|
| 93 | |
---|
[d89f09b] | 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 |
---|
[49b7efa] | 108 | |
---|
[b30f001] | 109 | shape_submenu= wx.Menu() |
---|
| 110 | indep_submenu = wx.Menu() |
---|
| 111 | added_models = wx.Menu() |
---|
[49b7efa] | 112 | |
---|
[d89f09b] | 113 | for id_str,value in self.model_list.iteritems(): |
---|
| 114 | item = self.model_list[id_str] |
---|
[49b7efa] | 115 | name = item.model.__name__ |
---|
[d89f09b] | 116 | if hasattr(item, "name"): |
---|
[49b7efa] | 117 | name = item.model.name |
---|
| 118 | self.model_list_box[name] =value.model |
---|
[b30f001] | 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() |
---|
[49b7efa] | 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") |
---|
[3b19ac9] | 138 | |
---|
| 139 | |
---|
| 140 | |
---|
[49b7efa] | 141 | model_info="additional models" |
---|
| 142 | id = wx.NewId() |
---|
[b30f001] | 143 | if len(self.custom_models) == 0: |
---|
| 144 | for item in self.plugins: |
---|
[49b7efa] | 145 | if item not in self.custom_models.values(): |
---|
[b30f001] | 146 | self.custom_models[str(id)] = item |
---|
[49b7efa] | 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 |
---|
[b30f001] | 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") |
---|
[d89f09b] | 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 | |
---|
[49b7efa] | 172 | model = self.model_list[str(evt.GetId())] |
---|
| 173 | evt = ModelEvent(modelinfo=model) |
---|
[d89f09b] | 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 | |
---|