[33afff7] | 1 | |
---|
[d89f09b] | 2 | import wx |
---|
[bb18ef1] | 3 | import wx.lib.newevent |
---|
[b30f001] | 4 | import imp |
---|
[9466f2d6] | 5 | import os |
---|
| 6 | import sys |
---|
| 7 | import math |
---|
[d89f09b] | 8 | import os.path |
---|
[33afff7] | 9 | # Time is needed by the log method |
---|
| 10 | import time |
---|
[2dbb681] | 11 | |
---|
[9466f2d6] | 12 | from sans.guiframe.events import StatusEvent |
---|
[33afff7] | 13 | # Explicitly import from the pluginmodel module so that py2exe |
---|
| 14 | # places it in the distribution. The Model1DPlugin class is used |
---|
| 15 | # as the base class of plug-in models. |
---|
| 16 | from sans.models.pluginmodel import Model1DPlugin |
---|
[9466f2d6] | 17 | |
---|
| 18 | PLUGIN_DIR = 'plugins' |
---|
| 19 | |
---|
[b30f001] | 20 | def log(message): |
---|
[5062bbf] | 21 | """ |
---|
| 22 | """ |
---|
[b30f001] | 23 | out = open("plugins.log", 'a') |
---|
| 24 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
| 25 | out.close() |
---|
| 26 | |
---|
[9466f2d6] | 27 | |
---|
[bfe4644] | 28 | def _check_plugin(model, name): |
---|
| 29 | """ |
---|
[5062bbf] | 30 | Do some checking before model adding plugins in the list |
---|
| 31 | |
---|
| 32 | :param model: class model to add into the plugin list |
---|
| 33 | :param name:name of the module plugin |
---|
| 34 | |
---|
| 35 | :return model: model if valid model or None if not valid |
---|
| 36 | |
---|
[bfe4644] | 37 | """ |
---|
| 38 | #Check is the plugin is of type Model1DPlugin |
---|
| 39 | if not issubclass(model, Model1DPlugin): |
---|
[9a22655] | 40 | msg = "Plugin %s must be of type Model1DPlugin \n"%str(name) |
---|
[bfe4644] | 41 | log(msg) |
---|
[8d78399] | 42 | return None |
---|
[bfe4644] | 43 | if model.__name__!="Model": |
---|
| 44 | msg= "Plugin %s class name must be Model \n"%str(name) |
---|
| 45 | log(msg) |
---|
[8d78399] | 46 | return None |
---|
[bfe4644] | 47 | try: |
---|
| 48 | new_instance= model() |
---|
| 49 | except: |
---|
| 50 | msg="Plugin %s error in __init__ \n\t: %s %s\n"%(str(name), |
---|
| 51 | str(sys.exc_type),sys.exc_value) |
---|
| 52 | log(msg) |
---|
[8d78399] | 53 | return None |
---|
[bfe4644] | 54 | |
---|
| 55 | new_instance= model() |
---|
| 56 | if hasattr(new_instance,"function"): |
---|
| 57 | try: |
---|
| 58 | value=new_instance.function() |
---|
| 59 | except: |
---|
| 60 | msg="Plugin %s: error writing function \n\t :%s %s\n "%(str(name), |
---|
| 61 | str(sys.exc_type),sys.exc_value) |
---|
| 62 | log(msg) |
---|
[8d78399] | 63 | return None |
---|
[bfe4644] | 64 | else: |
---|
| 65 | msg="Plugin %s needs a method called function \n"%str(name) |
---|
| 66 | log(msg) |
---|
[8d78399] | 67 | return None |
---|
[bfe4644] | 68 | return model |
---|
| 69 | |
---|
| 70 | |
---|
[1c66bc5] | 71 | def _findModels(dir): |
---|
[5062bbf] | 72 | """ |
---|
| 73 | """ |
---|
[1c66bc5] | 74 | # List of plugin objects |
---|
[b2d9826] | 75 | plugins = {} |
---|
[1c66bc5] | 76 | # Go through files in plug-in directory |
---|
| 77 | try: |
---|
| 78 | list = os.listdir(dir) |
---|
| 79 | for item in list: |
---|
| 80 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 81 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
| 82 | name = toks[0] |
---|
| 83 | |
---|
| 84 | path = [os.path.abspath(dir)] |
---|
| 85 | file = None |
---|
| 86 | try: |
---|
| 87 | (file, path, info) = imp.find_module(name, path) |
---|
| 88 | module = imp.load_module( name, file, item, info ) |
---|
| 89 | if hasattr(module, "Model"): |
---|
| 90 | try: |
---|
[bfe4644] | 91 | if _check_plugin(module.Model, name)!=None: |
---|
[b2d9826] | 92 | plugins[name] = module.Model |
---|
[1c66bc5] | 93 | except: |
---|
[bfe4644] | 94 | msg="Error accessing Model" |
---|
| 95 | msg+="in %s\n %s %s\n" % (name, |
---|
| 96 | str(sys.exc_type), sys.exc_value) |
---|
| 97 | log(msg) |
---|
[1c66bc5] | 98 | except: |
---|
[bfe4644] | 99 | msg="Error accessing Model" |
---|
| 100 | msg +=" in %s\n %s %s \n" %(name, |
---|
| 101 | str(sys.exc_type), sys.exc_value) |
---|
| 102 | log(msg) |
---|
[1c66bc5] | 103 | finally: |
---|
[a92d51b] | 104 | |
---|
[1c66bc5] | 105 | if not file==None: |
---|
| 106 | file.close() |
---|
| 107 | except: |
---|
[33afff7] | 108 | # Don't deal with bad plug-in imports. Just skip. |
---|
[1c66bc5] | 109 | pass |
---|
| 110 | return plugins |
---|
[bb18ef1] | 111 | |
---|
| 112 | class ModelList(object): |
---|
| 113 | """ |
---|
[5062bbf] | 114 | Contains dictionary of model and their type |
---|
[bb18ef1] | 115 | """ |
---|
| 116 | def __init__(self): |
---|
[5062bbf] | 117 | """ |
---|
| 118 | """ |
---|
| 119 | self.mydict = {} |
---|
[bb18ef1] | 120 | |
---|
| 121 | def set_list(self, name, mylist): |
---|
| 122 | """ |
---|
[5062bbf] | 123 | :param name: the type of the list |
---|
| 124 | :param mylist: the list to add |
---|
| 125 | |
---|
[bb18ef1] | 126 | """ |
---|
| 127 | if name not in self.mydict.keys(): |
---|
| 128 | self.mydict[name] = mylist |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | def get_list(self): |
---|
| 132 | """ |
---|
[5062bbf] | 133 | return all the list stored in a dictionary object |
---|
[bb18ef1] | 134 | """ |
---|
| 135 | return self.mydict |
---|
| 136 | |
---|
[d89f09b] | 137 | class ModelManager: |
---|
[5062bbf] | 138 | """ |
---|
| 139 | """ |
---|
[bb18ef1] | 140 | ## external dict for models |
---|
| 141 | model_combobox = ModelList() |
---|
| 142 | ## Dictionary of form models |
---|
| 143 | form_factor_dict = {} |
---|
| 144 | ## dictionary of other |
---|
| 145 | struct_factor_dict = {} |
---|
| 146 | ##list of form factors |
---|
[b2d9826] | 147 | shape_list = [] |
---|
[bb18ef1] | 148 | ## independent shape model list |
---|
| 149 | shape_indep_list = [] |
---|
| 150 | ##list of structure factors |
---|
[b2d9826] | 151 | struct_list = [] |
---|
[376916c] | 152 | ##list of model allowing multiplication |
---|
[b2d9826] | 153 | multiplication_factor = [] |
---|
[e87f9fc] | 154 | ##list of multifunctional shapes |
---|
[b2d9826] | 155 | multi_func_list = [] |
---|
[bb18ef1] | 156 | ## list of added models |
---|
[b2d9826] | 157 | plugins = [] |
---|
[bb18ef1] | 158 | ## Event owner (guiframe) |
---|
[d89f09b] | 159 | event_owner = None |
---|
[9466f2d6] | 160 | last_time_dir_modified = 0 |
---|
| 161 | |
---|
[6bbeacd4] | 162 | def __init__(self): |
---|
| 163 | """ |
---|
| 164 | """ |
---|
[b2d9826] | 165 | self.stored_plugins = {} |
---|
[6bbeacd4] | 166 | self._getModelList() |
---|
| 167 | |
---|
[9466f2d6] | 168 | def findModels(self): |
---|
| 169 | """ |
---|
| 170 | find plugin model in directory of plugin .recompile all file |
---|
| 171 | in the directory if file were modified |
---|
| 172 | """ |
---|
| 173 | if self.is_changed(): |
---|
| 174 | #always recompile the folder plugin |
---|
| 175 | import compileall |
---|
| 176 | compileall.compile_dir(dir=PLUGIN_DIR, force=1) |
---|
| 177 | log("looking for models in: %s/plugins" % os.getcwd()) |
---|
| 178 | return _findModels(PLUGIN_DIR) |
---|
| 179 | return {} |
---|
| 180 | |
---|
| 181 | |
---|
[d89f09b] | 182 | def _getModelList(self): |
---|
| 183 | """ |
---|
[5062bbf] | 184 | List of models we want to make available by default |
---|
| 185 | for this application |
---|
| 186 | |
---|
| 187 | :return: the next free event ID following the new menu events |
---|
[e7b1ccf] | 188 | |
---|
[d89f09b] | 189 | """ |
---|
[442895f] | 190 | from sans.models.SphereModel import SphereModel |
---|
[bb18ef1] | 191 | self.shape_list.append(SphereModel) |
---|
[376916c] | 192 | self.multiplication_factor.append(SphereModel) |
---|
[442895f] | 193 | |
---|
[1a395a6] | 194 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
| 195 | self.shape_list.append(BinaryHSModel) |
---|
| 196 | |
---|
[ce07fa8] | 197 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
| 198 | self.shape_list.append(FuzzySphereModel) |
---|
| 199 | self.multiplication_factor.append(FuzzySphereModel) |
---|
[fb59ed9] | 200 | |
---|
[442895f] | 201 | from sans.models.CoreShellModel import CoreShellModel |
---|
[bb18ef1] | 202 | self.shape_list.append(CoreShellModel) |
---|
[5eb9154] | 203 | self.multiplication_factor.append(CoreShellModel) |
---|
[4523b68] | 204 | |
---|
| 205 | from sans.models.CoreMultiShellModel import CoreMultiShellModel |
---|
| 206 | self.shape_list.append(CoreMultiShellModel) |
---|
| 207 | self.multiplication_factor.append(CoreMultiShellModel) |
---|
[a1b2471] | 208 | self.multi_func_list.append(CoreMultiShellModel) |
---|
[fb59ed9] | 209 | |
---|
[eddff027] | 210 | from sans.models.VesicleModel import VesicleModel |
---|
| 211 | self.shape_list.append(VesicleModel) |
---|
[5eb9154] | 212 | self.multiplication_factor.append(VesicleModel) |
---|
| 213 | |
---|
| 214 | from sans.models.MultiShellModel import MultiShellModel |
---|
| 215 | self.shape_list.append(MultiShellModel) |
---|
| 216 | self.multiplication_factor.append(MultiShellModel) |
---|
[eddff027] | 217 | |
---|
[1a395a6] | 218 | from sans.models.OnionExpShellModel import OnionExpShellModel |
---|
| 219 | self.shape_list.append(OnionExpShellModel) |
---|
| 220 | self.multiplication_factor.append(OnionExpShellModel) |
---|
| 221 | self.multi_func_list.append(OnionExpShellModel) |
---|
| 222 | |
---|
| 223 | from sans.models.SphericalSLDModel import SphericalSLDModel |
---|
| 224 | self.shape_list.append(SphericalSLDModel) |
---|
| 225 | self.multiplication_factor.append(SphericalSLDModel) |
---|
| 226 | self.multi_func_list.append(SphericalSLDModel) |
---|
[cee6867] | 227 | |
---|
[eddff027] | 228 | from sans.models.CylinderModel import CylinderModel |
---|
| 229 | self.shape_list.append(CylinderModel) |
---|
| 230 | self.multiplication_factor.append(CylinderModel) |
---|
| 231 | |
---|
| 232 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
| 233 | self.shape_list.append(CoreShellCylinderModel) |
---|
[5eb9154] | 234 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
[cee6867] | 235 | |
---|
| 236 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
| 237 | self.shape_list.append(HollowCylinderModel) |
---|
[5eb9154] | 238 | self.multiplication_factor.append(HollowCylinderModel) |
---|
[eddff027] | 239 | |
---|
| 240 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
| 241 | self.shape_list.append(FlexibleCylinderModel) |
---|
[72f719b] | 242 | |
---|
[ce07fa8] | 243 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
| 244 | self.shape_list.append(FlexCylEllipXModel) |
---|
[eddff027] | 245 | |
---|
| 246 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
| 247 | self.shape_list.append(StackedDisksModel) |
---|
[5eb9154] | 248 | self.multiplication_factor.append(StackedDisksModel) |
---|
[eddff027] | 249 | |
---|
| 250 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
| 251 | self.shape_list.append(ParallelepipedModel) |
---|
[72f719b] | 252 | self.multiplication_factor.append(ParallelepipedModel) |
---|
[cee6867] | 253 | |
---|
[fb59ed9] | 254 | from sans.models.CSParallelepipedModel import CSParallelepipedModel |
---|
| 255 | self.shape_list.append(CSParallelepipedModel) |
---|
| 256 | self.multiplication_factor.append(CSParallelepipedModel) |
---|
| 257 | |
---|
[442895f] | 258 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
[bb18ef1] | 259 | self.shape_list.append(EllipticalCylinderModel) |
---|
[72f719b] | 260 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
[fb59ed9] | 261 | |
---|
| 262 | from sans.models.BarBellModel import BarBellModel |
---|
| 263 | self.shape_list.append(BarBellModel) |
---|
| 264 | # not implemeted yet! |
---|
| 265 | #self.multiplication_factor.append(BarBellModel) |
---|
| 266 | |
---|
| 267 | from sans.models.CappedCylinderModel import CappedCylinderModel |
---|
| 268 | self.shape_list.append(CappedCylinderModel) |
---|
| 269 | # not implemeted yet! |
---|
| 270 | #self.multiplication_factor.append(CappedCylinderModel) |
---|
| 271 | |
---|
[442895f] | 272 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
[bb18ef1] | 273 | self.shape_list.append(EllipsoidModel) |
---|
[376916c] | 274 | self.multiplication_factor.append(EllipsoidModel) |
---|
[eddff027] | 275 | |
---|
| 276 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
| 277 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
[5eb9154] | 278 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
[bb18ef1] | 279 | |
---|
[e65050e] | 280 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
| 281 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
[9002927] | 282 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
[e65050e] | 283 | |
---|
| 284 | from sans.models.LamellarModel import LamellarModel |
---|
| 285 | self.shape_list.append(LamellarModel) |
---|
| 286 | |
---|
| 287 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
| 288 | self.shape_list.append(LamellarFFHGModel) |
---|
| 289 | |
---|
| 290 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
| 291 | self.shape_list.append(LamellarPSModel) |
---|
[7a69683] | 292 | |
---|
[e65050e] | 293 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
| 294 | self.shape_list.append(LamellarPSHGModel) |
---|
[fb59ed9] | 295 | |
---|
| 296 | from sans.models.LamellarPCrystalModel import LamellarPCrystalModel |
---|
| 297 | self.shape_list.append(LamellarPCrystalModel) |
---|
| 298 | |
---|
| 299 | from sans.models.SCCrystalModel import SCCrystalModel |
---|
| 300 | self.shape_list.append(SCCrystalModel) |
---|
| 301 | |
---|
| 302 | from sans.models.FCCrystalModel import FCCrystalModel |
---|
| 303 | self.shape_list.append(FCCrystalModel) |
---|
| 304 | |
---|
| 305 | from sans.models.BCCrystalModel import BCCrystalModel |
---|
| 306 | self.shape_list.append(BCCrystalModel) |
---|
[7a69683] | 307 | |
---|
[376916c] | 308 | ## Structure factor |
---|
[8346667] | 309 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
[bb18ef1] | 310 | self.struct_list.append(SquareWellStructure) |
---|
[8346667] | 311 | |
---|
| 312 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
[bb18ef1] | 313 | self.struct_list.append(HardsphereStructure) |
---|
| 314 | |
---|
[8346667] | 315 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
[bb18ef1] | 316 | self.struct_list.append(StickyHSStructure) |
---|
[8346667] | 317 | |
---|
| 318 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
[bb18ef1] | 319 | self.struct_list.append(HayterMSAStructure) |
---|
[fb59ed9] | 320 | |
---|
| 321 | ##shape-independent models |
---|
[ce07fa8] | 322 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
| 323 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
| 324 | |
---|
[442895f] | 325 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
[bb18ef1] | 326 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
| 327 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
[fb59ed9] | 328 | |
---|
| 329 | from sans.models.BroadPeakModel import BroadPeakModel |
---|
| 330 | self.shape_indep_list.append(BroadPeakModel) |
---|
| 331 | |
---|
| 332 | from sans.models.CorrLengthModel import CorrLengthModel |
---|
| 333 | self.shape_indep_list.append(CorrLengthModel) |
---|
| 334 | |
---|
[442895f] | 335 | from sans.models.DABModel import DABModel |
---|
[bb18ef1] | 336 | self.shape_indep_list.append(DABModel ) |
---|
[442895f] | 337 | |
---|
[ce07fa8] | 338 | from sans.models.DebyeModel import DebyeModel |
---|
| 339 | self.shape_indep_list.append(DebyeModel ) |
---|
| 340 | |
---|
[fb59ed9] | 341 | #FractalModel (a c-model)is now being used instead of FractalAbsModel. |
---|
[ce07fa8] | 342 | from sans.models.FractalModel import FractalModel |
---|
| 343 | self.shape_indep_list.append(FractalModel ) |
---|
[bb18ef1] | 344 | |
---|
[fb59ed9] | 345 | from sans.models.FractalCoreShellModel import FractalCoreShellModel |
---|
| 346 | self.shape_indep_list.append(FractalCoreShellModel ) |
---|
| 347 | |
---|
| 348 | from sans.models.GaussLorentzGelModel import GaussLorentzGelModel |
---|
| 349 | self.shape_indep_list.append(GaussLorentzGelModel) |
---|
| 350 | |
---|
| 351 | from sans.models.GuinierModel import GuinierModel |
---|
| 352 | self.shape_indep_list.append(GuinierModel ) |
---|
| 353 | |
---|
| 354 | from sans.models.GuinierPorodModel import GuinierPorodModel |
---|
| 355 | self.shape_indep_list.append(GuinierPorodModel ) |
---|
| 356 | |
---|
[ce07fa8] | 357 | from sans.models.LorentzModel import LorentzModel |
---|
| 358 | self.shape_indep_list.append( LorentzModel) |
---|
[442895f] | 359 | |
---|
[cee6867] | 360 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
| 361 | self.shape_indep_list.append(PeakGaussModel) |
---|
| 362 | |
---|
| 363 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
| 364 | self.shape_indep_list.append(PeakLorentzModel) |
---|
| 365 | |
---|
[ce07fa8] | 366 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
| 367 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
[fb59ed9] | 368 | |
---|
| 369 | from sans.models.PolymerExclVolume import PolymerExclVolume |
---|
| 370 | self.shape_indep_list.append(PolymerExclVolume) |
---|
| 371 | |
---|
[ce07fa8] | 372 | from sans.models.PorodModel import PorodModel |
---|
[fb59ed9] | 373 | self.shape_indep_list.append(PorodModel ) |
---|
[442895f] | 374 | |
---|
[fb59ed9] | 375 | from sans.models.RPA10Model import RPA10Model |
---|
| 376 | self.shape_indep_list.append(RPA10Model) |
---|
| 377 | self.multi_func_list.append(RPA10Model) |
---|
[81bece4] | 378 | |
---|
[442895f] | 379 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
[bb18ef1] | 380 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
[eddff027] | 381 | |
---|
[fb59ed9] | 382 | from sans.models.TwoLorentzianModel import TwoLorentzianModel |
---|
| 383 | self.shape_indep_list.append(TwoLorentzianModel ) |
---|
| 384 | |
---|
| 385 | from sans.models.TwoPowerLawModel import TwoPowerLawModel |
---|
| 386 | self.shape_indep_list.append(TwoPowerLawModel ) |
---|
| 387 | |
---|
| 388 | from sans.models.UnifiedPowerRgModel import UnifiedPowerRgModel |
---|
| 389 | self.shape_indep_list.append(UnifiedPowerRgModel ) |
---|
| 390 | self.multi_func_list.append(UnifiedPowerRgModel) |
---|
| 391 | |
---|
[eddff027] | 392 | from sans.models.LineModel import LineModel |
---|
| 393 | self.shape_indep_list.append(LineModel) |
---|
[5062bbf] | 394 | |
---|
[fb59ed9] | 395 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
| 396 | self.multi_func_list.append(ReflectivityModel) |
---|
[1cc23fd] | 397 | |
---|
| 398 | from sans.models.ReflectivityIIModel import ReflectivityIIModel |
---|
| 399 | self.multi_func_list.append(ReflectivityIIModel) |
---|
[fb59ed9] | 400 | |
---|
[49b7efa] | 401 | #Looking for plugins |
---|
[9466f2d6] | 402 | self.stored_plugins = self.findModels() |
---|
[b2d9826] | 403 | self.plugins = self.stored_plugins.values() |
---|
[fb59ed9] | 404 | self.plugins.append(ReflectivityModel) |
---|
[1cc23fd] | 405 | self.plugins.append(ReflectivityIIModel) |
---|
[b2d9826] | 406 | self._get_multifunc_models() |
---|
| 407 | |
---|
[d89f09b] | 408 | return 0 |
---|
| 409 | |
---|
[9466f2d6] | 410 | def is_changed(self): |
---|
| 411 | """ |
---|
| 412 | check the last time the plugin dir has changed and return true |
---|
| 413 | is the directory was modified else return false |
---|
| 414 | """ |
---|
| 415 | is_modified = False |
---|
| 416 | if os.path.isdir(PLUGIN_DIR): |
---|
| 417 | temp = os.path.getmtime(PLUGIN_DIR) |
---|
| 418 | if self.last_time_dir_modified != temp: |
---|
| 419 | is_modified = True |
---|
| 420 | self.last_time_dir_modified = temp |
---|
| 421 | return is_modified |
---|
[d89f09b] | 422 | |
---|
[b2d9826] | 423 | def update(self): |
---|
| 424 | """ |
---|
[9466f2d6] | 425 | return a dictionary of model if |
---|
| 426 | new models were added else return empty dictionary |
---|
[b2d9826] | 427 | """ |
---|
[9466f2d6] | 428 | new_plugins = self.findModels() |
---|
| 429 | if len(new_plugins) > 0: |
---|
| 430 | for name, plug in new_plugins.iteritems(): |
---|
| 431 | if name not in self.stored_plugins.keys(): |
---|
| 432 | self.stored_plugins[name] = plug |
---|
| 433 | self.plugins.append(plug) |
---|
| 434 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
| 435 | return self.model_combobox.get_list() |
---|
| 436 | else: |
---|
| 437 | return {} |
---|
[b2d9826] | 438 | |
---|
[d89f09b] | 439 | def populate_menu(self, modelmenu, event_owner): |
---|
| 440 | """ |
---|
[5062bbf] | 441 | Populate a menu with our models |
---|
| 442 | |
---|
| 443 | :param id: first menu event ID to use when binding the menu events |
---|
| 444 | :param modelmenu: wx.Menu object to populate |
---|
| 445 | :param event_owner: wx object to bind the menu events to |
---|
| 446 | |
---|
| 447 | :return: the next free event ID following the new menu events |
---|
| 448 | |
---|
[d89f09b] | 449 | """ |
---|
[bb18ef1] | 450 | ## Fill model lists |
---|
[d89f09b] | 451 | self._getModelList() |
---|
[bb18ef1] | 452 | ## store reference to model menu of guiframe |
---|
| 453 | self.modelmenu = modelmenu |
---|
| 454 | ## guiframe reference |
---|
[d89f09b] | 455 | self.event_owner = event_owner |
---|
[bb18ef1] | 456 | |
---|
| 457 | shape_submenu = wx.Menu() |
---|
| 458 | shape_indep_submenu = wx.Menu() |
---|
| 459 | structure_factor = wx.Menu() |
---|
[b30f001] | 460 | added_models = wx.Menu() |
---|
[376916c] | 461 | multip_models = wx.Menu() |
---|
[bb18ef1] | 462 | ## create menu with shape |
---|
[5062bbf] | 463 | self._fill_simple_menu(menuinfo=["Shapes",shape_submenu," simple shape"], |
---|
| 464 | list1=self.shape_list) |
---|
[376916c] | 465 | |
---|
[5062bbf] | 466 | self._fill_simple_menu(menuinfo=["Shape-Independent",shape_indep_submenu, |
---|
[bb18ef1] | 467 | "List of shape-independent models"], |
---|
[5062bbf] | 468 | list1=self.shape_indep_list ) |
---|
[bb18ef1] | 469 | |
---|
[5062bbf] | 470 | self._fill_simple_menu(menuinfo=["Structure Factors",structure_factor, |
---|
[bb18ef1] | 471 | "List of Structure factors models" ], |
---|
[5062bbf] | 472 | list1=self.struct_list) |
---|
[bb18ef1] | 473 | |
---|
[5062bbf] | 474 | self._fill_plugin_menu(menuinfo=["Customized Models", added_models, |
---|
[376916c] | 475 | "List of additional models"], |
---|
[5062bbf] | 476 | list1=self.plugins) |
---|
[376916c] | 477 | |
---|
| 478 | self._fill_menu(menuinfo=["P(Q)*S(Q)",multip_models, |
---|
| 479 | "mulplication of 2 models"], |
---|
[5062bbf] | 480 | list1=self.multiplication_factor , |
---|
| 481 | list2= self.struct_list) |
---|
[d89f09b] | 482 | return 0 |
---|
| 483 | |
---|
[5062bbf] | 484 | def _fill_plugin_menu(self, menuinfo, list1): |
---|
[bfe4644] | 485 | """ |
---|
[5062bbf] | 486 | fill the plugin menu with costumized models |
---|
[bfe4644] | 487 | """ |
---|
| 488 | if len(list1)==0: |
---|
| 489 | id = wx.NewId() |
---|
| 490 | msg= "No model available check plugins.log for errors to fix problem" |
---|
| 491 | menuinfo[1].Append(int(id),"Empty",msg) |
---|
| 492 | self._fill_simple_menu( menuinfo,list1) |
---|
| 493 | |
---|
[5062bbf] | 494 | def _fill_simple_menu(self, menuinfo, list1): |
---|
[bb18ef1] | 495 | """ |
---|
[5062bbf] | 496 | Fill the menu with list item |
---|
| 497 | |
---|
| 498 | :param modelmenu: the menu to fill |
---|
| 499 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
| 500 | with info.Should be a list : |
---|
| 501 | [name(string) , menu(wx.menu), help(string)] |
---|
| 502 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
| 503 | |
---|
[bb18ef1] | 504 | """ |
---|
| 505 | if len(list1)>0: |
---|
| 506 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
[e7b1ccf] | 507 | |
---|
[bb18ef1] | 508 | for item in list1: |
---|
[e7b1ccf] | 509 | try: |
---|
| 510 | id = wx.NewId() |
---|
| 511 | struct_factor=item() |
---|
| 512 | struct_name = struct_factor.__class__.__name__ |
---|
| 513 | if hasattr(struct_factor, "name"): |
---|
| 514 | struct_name = struct_factor.name |
---|
| 515 | |
---|
| 516 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
| 517 | if not item in self.struct_factor_dict.itervalues(): |
---|
| 518 | self.struct_factor_dict[str(id)]= item |
---|
| 519 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
| 520 | except: |
---|
| 521 | msg= "Error Occured: %s"%sys.exc_value |
---|
| 522 | wx.PostEvent(self.event_owner, StatusEvent(status=msg)) |
---|
[bb18ef1] | 523 | |
---|
| 524 | id = wx.NewId() |
---|
| 525 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
| 526 | |
---|
[5062bbf] | 527 | def _fill_menu(self, menuinfo, list1, list2): |
---|
[bb18ef1] | 528 | """ |
---|
[5062bbf] | 529 | Fill the menu with list item |
---|
| 530 | |
---|
| 531 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
| 532 | with info.Should be a list : |
---|
| 533 | [name(string) , menu(wx.menu), help(string)] |
---|
| 534 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
| 535 | :param list2: contains item (Structure factor )to fill modelmenu |
---|
| 536 | third column |
---|
| 537 | |
---|
[bb18ef1] | 538 | """ |
---|
| 539 | if len(list1)>0: |
---|
| 540 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
| 541 | |
---|
| 542 | for item in list1: |
---|
| 543 | form_factor= item() |
---|
| 544 | form_name = form_factor.__class__.__name__ |
---|
| 545 | if hasattr(form_factor, "name"): |
---|
| 546 | form_name = form_factor.name |
---|
| 547 | ### store form factor to return to other users |
---|
| 548 | newmenu= wx.Menu() |
---|
| 549 | if len(list2)>0: |
---|
| 550 | for model in list2: |
---|
| 551 | id = wx.NewId() |
---|
| 552 | struct_factor = model() |
---|
| 553 | name = struct_factor.__class__.__name__ |
---|
| 554 | if hasattr(struct_factor, "name"): |
---|
| 555 | name = struct_factor.name |
---|
| 556 | newmenu.Append(id,name, name) |
---|
| 557 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
| 558 | ## save form_fact and struct_fact |
---|
| 559 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
| 560 | |
---|
| 561 | form_id= wx.NewId() |
---|
| 562 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
| 563 | id=wx.NewId() |
---|
| 564 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
| 565 | |
---|
[d89f09b] | 566 | def _on_model(self, evt): |
---|
| 567 | """ |
---|
[5062bbf] | 568 | React to a model menu event |
---|
| 569 | |
---|
| 570 | :param event: wx menu event |
---|
| 571 | |
---|
[d89f09b] | 572 | """ |
---|
[bb18ef1] | 573 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
| 574 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
| 575 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
[5062bbf] | 576 | model = MultiplicationModel(model1, model2) |
---|
[bb18ef1] | 577 | else: |
---|
| 578 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
[6886000] | 579 | evt = ModelEvent(model=model) |
---|
[bb18ef1] | 580 | wx.PostEvent(self.event_owner, evt) |
---|
[d89f09b] | 581 | |
---|
[fb59ed9] | 582 | def _get_multifunc_models(self): |
---|
| 583 | """ |
---|
| 584 | Get the multifunctional models |
---|
| 585 | """ |
---|
| 586 | for item in self.plugins: |
---|
| 587 | try: |
---|
| 588 | # check the multiplicity if any |
---|
| 589 | if item.multiplicity_info[0] > 1: |
---|
| 590 | self.multi_func_list.append(item) |
---|
| 591 | except: |
---|
| 592 | # pass to other items |
---|
| 593 | pass |
---|
| 594 | |
---|
[d89f09b] | 595 | def get_model_list(self): |
---|
[5062bbf] | 596 | """ |
---|
| 597 | return dictionary of models for fitpanel use |
---|
| 598 | |
---|
| 599 | """ |
---|
[6bbeacd4] | 600 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
| 601 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
| 602 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
| 603 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
| 604 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
[376916c] | 605 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
[e87f9fc] | 606 | self.model_combobox.set_list("Multi-Functions", self.multi_func_list) |
---|
[b2d9826] | 607 | return self.model_combobox.get_list() |
---|
[d89f09b] | 608 | |
---|
[376916c] | 609 | |
---|
[bb18ef1] | 610 | |
---|
| 611 | |
---|
[d89f09b] | 612 | |
---|
[bb18ef1] | 613 | |
---|