[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 | |
---|
[bb9f322] | 137 | class ModelManagerBase: |
---|
[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 |
---|
[6bbeacd4] | 161 | def __init__(self): |
---|
| 162 | """ |
---|
| 163 | """ |
---|
[bb9f322] | 164 | |
---|
[b2d9826] | 165 | self.stored_plugins = {} |
---|
[6bbeacd4] | 166 | self._getModelList() |
---|
| 167 | |
---|
[bb9f322] | 168 | |
---|
[9466f2d6] | 169 | def findModels(self): |
---|
| 170 | """ |
---|
| 171 | find plugin model in directory of plugin .recompile all file |
---|
| 172 | in the directory if file were modified |
---|
| 173 | """ |
---|
| 174 | if self.is_changed(): |
---|
| 175 | #always recompile the folder plugin |
---|
| 176 | import compileall |
---|
| 177 | compileall.compile_dir(dir=PLUGIN_DIR, force=1) |
---|
| 178 | log("looking for models in: %s/plugins" % os.getcwd()) |
---|
| 179 | return _findModels(PLUGIN_DIR) |
---|
| 180 | return {} |
---|
| 181 | |
---|
| 182 | |
---|
[d89f09b] | 183 | def _getModelList(self): |
---|
| 184 | """ |
---|
[5062bbf] | 185 | List of models we want to make available by default |
---|
| 186 | for this application |
---|
| 187 | |
---|
| 188 | :return: the next free event ID following the new menu events |
---|
[e7b1ccf] | 189 | |
---|
[d89f09b] | 190 | """ |
---|
[442895f] | 191 | from sans.models.SphereModel import SphereModel |
---|
[bb18ef1] | 192 | self.shape_list.append(SphereModel) |
---|
[376916c] | 193 | self.multiplication_factor.append(SphereModel) |
---|
[442895f] | 194 | |
---|
[1a395a6] | 195 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
| 196 | self.shape_list.append(BinaryHSModel) |
---|
| 197 | |
---|
[ce07fa8] | 198 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
| 199 | self.shape_list.append(FuzzySphereModel) |
---|
| 200 | self.multiplication_factor.append(FuzzySphereModel) |
---|
[fb59ed9] | 201 | |
---|
[442895f] | 202 | from sans.models.CoreShellModel import CoreShellModel |
---|
[bb18ef1] | 203 | self.shape_list.append(CoreShellModel) |
---|
[5eb9154] | 204 | self.multiplication_factor.append(CoreShellModel) |
---|
[4523b68] | 205 | |
---|
| 206 | from sans.models.CoreMultiShellModel import CoreMultiShellModel |
---|
| 207 | self.shape_list.append(CoreMultiShellModel) |
---|
| 208 | self.multiplication_factor.append(CoreMultiShellModel) |
---|
[a1b2471] | 209 | self.multi_func_list.append(CoreMultiShellModel) |
---|
[fb59ed9] | 210 | |
---|
[eddff027] | 211 | from sans.models.VesicleModel import VesicleModel |
---|
| 212 | self.shape_list.append(VesicleModel) |
---|
[5eb9154] | 213 | self.multiplication_factor.append(VesicleModel) |
---|
| 214 | |
---|
| 215 | from sans.models.MultiShellModel import MultiShellModel |
---|
| 216 | self.shape_list.append(MultiShellModel) |
---|
| 217 | self.multiplication_factor.append(MultiShellModel) |
---|
[eddff027] | 218 | |
---|
[1a395a6] | 219 | from sans.models.OnionExpShellModel import OnionExpShellModel |
---|
| 220 | self.shape_list.append(OnionExpShellModel) |
---|
| 221 | self.multiplication_factor.append(OnionExpShellModel) |
---|
| 222 | self.multi_func_list.append(OnionExpShellModel) |
---|
| 223 | |
---|
| 224 | from sans.models.SphericalSLDModel import SphericalSLDModel |
---|
| 225 | self.shape_list.append(SphericalSLDModel) |
---|
| 226 | self.multiplication_factor.append(SphericalSLDModel) |
---|
| 227 | self.multi_func_list.append(SphericalSLDModel) |
---|
[cee6867] | 228 | |
---|
[eddff027] | 229 | from sans.models.CylinderModel import CylinderModel |
---|
| 230 | self.shape_list.append(CylinderModel) |
---|
| 231 | self.multiplication_factor.append(CylinderModel) |
---|
| 232 | |
---|
| 233 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
| 234 | self.shape_list.append(CoreShellCylinderModel) |
---|
[5eb9154] | 235 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
[cee6867] | 236 | |
---|
| 237 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
| 238 | self.shape_list.append(HollowCylinderModel) |
---|
[5eb9154] | 239 | self.multiplication_factor.append(HollowCylinderModel) |
---|
[eddff027] | 240 | |
---|
| 241 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
| 242 | self.shape_list.append(FlexibleCylinderModel) |
---|
[72f719b] | 243 | |
---|
[ce07fa8] | 244 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
| 245 | self.shape_list.append(FlexCylEllipXModel) |
---|
[eddff027] | 246 | |
---|
| 247 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
| 248 | self.shape_list.append(StackedDisksModel) |
---|
[5eb9154] | 249 | self.multiplication_factor.append(StackedDisksModel) |
---|
[eddff027] | 250 | |
---|
| 251 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
| 252 | self.shape_list.append(ParallelepipedModel) |
---|
[72f719b] | 253 | self.multiplication_factor.append(ParallelepipedModel) |
---|
[cee6867] | 254 | |
---|
[fb59ed9] | 255 | from sans.models.CSParallelepipedModel import CSParallelepipedModel |
---|
| 256 | self.shape_list.append(CSParallelepipedModel) |
---|
| 257 | self.multiplication_factor.append(CSParallelepipedModel) |
---|
| 258 | |
---|
[442895f] | 259 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
[bb18ef1] | 260 | self.shape_list.append(EllipticalCylinderModel) |
---|
[72f719b] | 261 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
[fb59ed9] | 262 | |
---|
| 263 | from sans.models.BarBellModel import BarBellModel |
---|
| 264 | self.shape_list.append(BarBellModel) |
---|
| 265 | # not implemeted yet! |
---|
| 266 | #self.multiplication_factor.append(BarBellModel) |
---|
| 267 | |
---|
| 268 | from sans.models.CappedCylinderModel import CappedCylinderModel |
---|
| 269 | self.shape_list.append(CappedCylinderModel) |
---|
| 270 | # not implemeted yet! |
---|
| 271 | #self.multiplication_factor.append(CappedCylinderModel) |
---|
| 272 | |
---|
[442895f] | 273 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
[bb18ef1] | 274 | self.shape_list.append(EllipsoidModel) |
---|
[376916c] | 275 | self.multiplication_factor.append(EllipsoidModel) |
---|
[eddff027] | 276 | |
---|
| 277 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
| 278 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
[5eb9154] | 279 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
[bb18ef1] | 280 | |
---|
[e65050e] | 281 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
| 282 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
[9002927] | 283 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
[e65050e] | 284 | |
---|
| 285 | from sans.models.LamellarModel import LamellarModel |
---|
| 286 | self.shape_list.append(LamellarModel) |
---|
| 287 | |
---|
| 288 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
| 289 | self.shape_list.append(LamellarFFHGModel) |
---|
| 290 | |
---|
| 291 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
| 292 | self.shape_list.append(LamellarPSModel) |
---|
[7a69683] | 293 | |
---|
[e65050e] | 294 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
| 295 | self.shape_list.append(LamellarPSHGModel) |
---|
[fb59ed9] | 296 | |
---|
| 297 | from sans.models.LamellarPCrystalModel import LamellarPCrystalModel |
---|
| 298 | self.shape_list.append(LamellarPCrystalModel) |
---|
| 299 | |
---|
| 300 | from sans.models.SCCrystalModel import SCCrystalModel |
---|
| 301 | self.shape_list.append(SCCrystalModel) |
---|
| 302 | |
---|
| 303 | from sans.models.FCCrystalModel import FCCrystalModel |
---|
| 304 | self.shape_list.append(FCCrystalModel) |
---|
| 305 | |
---|
| 306 | from sans.models.BCCrystalModel import BCCrystalModel |
---|
| 307 | self.shape_list.append(BCCrystalModel) |
---|
[7a69683] | 308 | |
---|
[376916c] | 309 | ## Structure factor |
---|
[8346667] | 310 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
[bb18ef1] | 311 | self.struct_list.append(SquareWellStructure) |
---|
[8346667] | 312 | |
---|
| 313 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
[bb18ef1] | 314 | self.struct_list.append(HardsphereStructure) |
---|
| 315 | |
---|
[8346667] | 316 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
[bb18ef1] | 317 | self.struct_list.append(StickyHSStructure) |
---|
[8346667] | 318 | |
---|
| 319 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
[bb18ef1] | 320 | self.struct_list.append(HayterMSAStructure) |
---|
[fb59ed9] | 321 | |
---|
| 322 | ##shape-independent models |
---|
[ce07fa8] | 323 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
| 324 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
| 325 | |
---|
[442895f] | 326 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
[bb18ef1] | 327 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
| 328 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
[fb59ed9] | 329 | |
---|
| 330 | from sans.models.BroadPeakModel import BroadPeakModel |
---|
| 331 | self.shape_indep_list.append(BroadPeakModel) |
---|
| 332 | |
---|
| 333 | from sans.models.CorrLengthModel import CorrLengthModel |
---|
| 334 | self.shape_indep_list.append(CorrLengthModel) |
---|
| 335 | |
---|
[442895f] | 336 | from sans.models.DABModel import DABModel |
---|
[bb18ef1] | 337 | self.shape_indep_list.append(DABModel ) |
---|
[442895f] | 338 | |
---|
[ce07fa8] | 339 | from sans.models.DebyeModel import DebyeModel |
---|
| 340 | self.shape_indep_list.append(DebyeModel ) |
---|
| 341 | |
---|
[fb59ed9] | 342 | #FractalModel (a c-model)is now being used instead of FractalAbsModel. |
---|
[ce07fa8] | 343 | from sans.models.FractalModel import FractalModel |
---|
| 344 | self.shape_indep_list.append(FractalModel ) |
---|
[bb18ef1] | 345 | |
---|
[fb59ed9] | 346 | from sans.models.FractalCoreShellModel import FractalCoreShellModel |
---|
| 347 | self.shape_indep_list.append(FractalCoreShellModel ) |
---|
| 348 | |
---|
| 349 | from sans.models.GaussLorentzGelModel import GaussLorentzGelModel |
---|
| 350 | self.shape_indep_list.append(GaussLorentzGelModel) |
---|
| 351 | |
---|
| 352 | from sans.models.GuinierModel import GuinierModel |
---|
| 353 | self.shape_indep_list.append(GuinierModel ) |
---|
| 354 | |
---|
| 355 | from sans.models.GuinierPorodModel import GuinierPorodModel |
---|
| 356 | self.shape_indep_list.append(GuinierPorodModel ) |
---|
| 357 | |
---|
[ce07fa8] | 358 | from sans.models.LorentzModel import LorentzModel |
---|
| 359 | self.shape_indep_list.append( LorentzModel) |
---|
[442895f] | 360 | |
---|
[cee6867] | 361 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
| 362 | self.shape_indep_list.append(PeakGaussModel) |
---|
| 363 | |
---|
| 364 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
| 365 | self.shape_indep_list.append(PeakLorentzModel) |
---|
| 366 | |
---|
[ce07fa8] | 367 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
| 368 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
[fb59ed9] | 369 | |
---|
| 370 | from sans.models.PolymerExclVolume import PolymerExclVolume |
---|
| 371 | self.shape_indep_list.append(PolymerExclVolume) |
---|
| 372 | |
---|
[ce07fa8] | 373 | from sans.models.PorodModel import PorodModel |
---|
[fb59ed9] | 374 | self.shape_indep_list.append(PorodModel ) |
---|
[442895f] | 375 | |
---|
[fb59ed9] | 376 | from sans.models.RPA10Model import RPA10Model |
---|
| 377 | self.shape_indep_list.append(RPA10Model) |
---|
| 378 | self.multi_func_list.append(RPA10Model) |
---|
[81bece4] | 379 | |
---|
[442895f] | 380 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
[bb18ef1] | 381 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
[eddff027] | 382 | |
---|
[fb59ed9] | 383 | from sans.models.TwoLorentzianModel import TwoLorentzianModel |
---|
| 384 | self.shape_indep_list.append(TwoLorentzianModel ) |
---|
| 385 | |
---|
| 386 | from sans.models.TwoPowerLawModel import TwoPowerLawModel |
---|
| 387 | self.shape_indep_list.append(TwoPowerLawModel ) |
---|
| 388 | |
---|
| 389 | from sans.models.UnifiedPowerRgModel import UnifiedPowerRgModel |
---|
| 390 | self.shape_indep_list.append(UnifiedPowerRgModel ) |
---|
| 391 | self.multi_func_list.append(UnifiedPowerRgModel) |
---|
| 392 | |
---|
[eddff027] | 393 | from sans.models.LineModel import LineModel |
---|
| 394 | self.shape_indep_list.append(LineModel) |
---|
[5062bbf] | 395 | |
---|
[fb59ed9] | 396 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
| 397 | self.multi_func_list.append(ReflectivityModel) |
---|
[1cc23fd] | 398 | |
---|
| 399 | from sans.models.ReflectivityIIModel import ReflectivityIIModel |
---|
| 400 | self.multi_func_list.append(ReflectivityIIModel) |
---|
[fb59ed9] | 401 | |
---|
[49b7efa] | 402 | #Looking for plugins |
---|
[9466f2d6] | 403 | self.stored_plugins = self.findModels() |
---|
[b2d9826] | 404 | self.plugins = self.stored_plugins.values() |
---|
[fb59ed9] | 405 | self.plugins.append(ReflectivityModel) |
---|
[1cc23fd] | 406 | self.plugins.append(ReflectivityIIModel) |
---|
[b2d9826] | 407 | self._get_multifunc_models() |
---|
| 408 | |
---|
[d89f09b] | 409 | return 0 |
---|
| 410 | |
---|
[9466f2d6] | 411 | def is_changed(self): |
---|
| 412 | """ |
---|
| 413 | check the last time the plugin dir has changed and return true |
---|
| 414 | is the directory was modified else return false |
---|
| 415 | """ |
---|
| 416 | is_modified = False |
---|
| 417 | if os.path.isdir(PLUGIN_DIR): |
---|
| 418 | temp = os.path.getmtime(PLUGIN_DIR) |
---|
| 419 | if self.last_time_dir_modified != temp: |
---|
| 420 | is_modified = True |
---|
| 421 | self.last_time_dir_modified = temp |
---|
[bb9f322] | 422 | |
---|
[9466f2d6] | 423 | return is_modified |
---|
[d89f09b] | 424 | |
---|
[b2d9826] | 425 | def update(self): |
---|
| 426 | """ |
---|
[9466f2d6] | 427 | return a dictionary of model if |
---|
| 428 | new models were added else return empty dictionary |
---|
[b2d9826] | 429 | """ |
---|
[9466f2d6] | 430 | new_plugins = self.findModels() |
---|
| 431 | if len(new_plugins) > 0: |
---|
| 432 | for name, plug in new_plugins.iteritems(): |
---|
| 433 | if name not in self.stored_plugins.keys(): |
---|
| 434 | self.stored_plugins[name] = plug |
---|
| 435 | self.plugins.append(plug) |
---|
| 436 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
| 437 | return self.model_combobox.get_list() |
---|
| 438 | else: |
---|
| 439 | return {} |
---|
[b2d9826] | 440 | |
---|
[d89f09b] | 441 | def populate_menu(self, modelmenu, event_owner): |
---|
| 442 | """ |
---|
[5062bbf] | 443 | Populate a menu with our models |
---|
| 444 | |
---|
| 445 | :param id: first menu event ID to use when binding the menu events |
---|
| 446 | :param modelmenu: wx.Menu object to populate |
---|
| 447 | :param event_owner: wx object to bind the menu events to |
---|
| 448 | |
---|
| 449 | :return: the next free event ID following the new menu events |
---|
| 450 | |
---|
[d89f09b] | 451 | """ |
---|
[bb18ef1] | 452 | ## Fill model lists |
---|
[d89f09b] | 453 | self._getModelList() |
---|
[bb18ef1] | 454 | ## store reference to model menu of guiframe |
---|
| 455 | self.modelmenu = modelmenu |
---|
| 456 | ## guiframe reference |
---|
[d89f09b] | 457 | self.event_owner = event_owner |
---|
[bb18ef1] | 458 | |
---|
| 459 | shape_submenu = wx.Menu() |
---|
| 460 | shape_indep_submenu = wx.Menu() |
---|
| 461 | structure_factor = wx.Menu() |
---|
[b30f001] | 462 | added_models = wx.Menu() |
---|
[376916c] | 463 | multip_models = wx.Menu() |
---|
[bb18ef1] | 464 | ## create menu with shape |
---|
[5062bbf] | 465 | self._fill_simple_menu(menuinfo=["Shapes",shape_submenu," simple shape"], |
---|
| 466 | list1=self.shape_list) |
---|
[376916c] | 467 | |
---|
[5062bbf] | 468 | self._fill_simple_menu(menuinfo=["Shape-Independent",shape_indep_submenu, |
---|
[bb18ef1] | 469 | "List of shape-independent models"], |
---|
[5062bbf] | 470 | list1=self.shape_indep_list ) |
---|
[bb18ef1] | 471 | |
---|
[5062bbf] | 472 | self._fill_simple_menu(menuinfo=["Structure Factors",structure_factor, |
---|
[bb18ef1] | 473 | "List of Structure factors models" ], |
---|
[5062bbf] | 474 | list1=self.struct_list) |
---|
[bb18ef1] | 475 | |
---|
[5062bbf] | 476 | self._fill_plugin_menu(menuinfo=["Customized Models", added_models, |
---|
[376916c] | 477 | "List of additional models"], |
---|
[5062bbf] | 478 | list1=self.plugins) |
---|
[376916c] | 479 | |
---|
| 480 | self._fill_menu(menuinfo=["P(Q)*S(Q)",multip_models, |
---|
| 481 | "mulplication of 2 models"], |
---|
[5062bbf] | 482 | list1=self.multiplication_factor , |
---|
| 483 | list2= self.struct_list) |
---|
[d89f09b] | 484 | return 0 |
---|
| 485 | |
---|
[5062bbf] | 486 | def _fill_plugin_menu(self, menuinfo, list1): |
---|
[bfe4644] | 487 | """ |
---|
[5062bbf] | 488 | fill the plugin menu with costumized models |
---|
[bfe4644] | 489 | """ |
---|
| 490 | if len(list1)==0: |
---|
| 491 | id = wx.NewId() |
---|
| 492 | msg= "No model available check plugins.log for errors to fix problem" |
---|
| 493 | menuinfo[1].Append(int(id),"Empty",msg) |
---|
| 494 | self._fill_simple_menu( menuinfo,list1) |
---|
| 495 | |
---|
[5062bbf] | 496 | def _fill_simple_menu(self, menuinfo, list1): |
---|
[bb18ef1] | 497 | """ |
---|
[5062bbf] | 498 | Fill the menu with list item |
---|
| 499 | |
---|
| 500 | :param modelmenu: the menu to fill |
---|
| 501 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
| 502 | with info.Should be a list : |
---|
| 503 | [name(string) , menu(wx.menu), help(string)] |
---|
| 504 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
| 505 | |
---|
[bb18ef1] | 506 | """ |
---|
| 507 | if len(list1)>0: |
---|
| 508 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
[e7b1ccf] | 509 | |
---|
[bb18ef1] | 510 | for item in list1: |
---|
[e7b1ccf] | 511 | try: |
---|
| 512 | id = wx.NewId() |
---|
| 513 | struct_factor=item() |
---|
| 514 | struct_name = struct_factor.__class__.__name__ |
---|
| 515 | if hasattr(struct_factor, "name"): |
---|
| 516 | struct_name = struct_factor.name |
---|
| 517 | |
---|
| 518 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
| 519 | if not item in self.struct_factor_dict.itervalues(): |
---|
| 520 | self.struct_factor_dict[str(id)]= item |
---|
| 521 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
| 522 | except: |
---|
| 523 | msg= "Error Occured: %s"%sys.exc_value |
---|
| 524 | wx.PostEvent(self.event_owner, StatusEvent(status=msg)) |
---|
[bb18ef1] | 525 | |
---|
| 526 | id = wx.NewId() |
---|
| 527 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
| 528 | |
---|
[5062bbf] | 529 | def _fill_menu(self, menuinfo, list1, list2): |
---|
[bb18ef1] | 530 | """ |
---|
[5062bbf] | 531 | Fill the menu with list item |
---|
| 532 | |
---|
| 533 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
| 534 | with info.Should be a list : |
---|
| 535 | [name(string) , menu(wx.menu), help(string)] |
---|
| 536 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
| 537 | :param list2: contains item (Structure factor )to fill modelmenu |
---|
| 538 | third column |
---|
| 539 | |
---|
[bb18ef1] | 540 | """ |
---|
| 541 | if len(list1)>0: |
---|
| 542 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
| 543 | |
---|
| 544 | for item in list1: |
---|
| 545 | form_factor= item() |
---|
| 546 | form_name = form_factor.__class__.__name__ |
---|
| 547 | if hasattr(form_factor, "name"): |
---|
| 548 | form_name = form_factor.name |
---|
| 549 | ### store form factor to return to other users |
---|
| 550 | newmenu= wx.Menu() |
---|
| 551 | if len(list2)>0: |
---|
| 552 | for model in list2: |
---|
| 553 | id = wx.NewId() |
---|
| 554 | struct_factor = model() |
---|
| 555 | name = struct_factor.__class__.__name__ |
---|
| 556 | if hasattr(struct_factor, "name"): |
---|
| 557 | name = struct_factor.name |
---|
| 558 | newmenu.Append(id,name, name) |
---|
| 559 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
| 560 | ## save form_fact and struct_fact |
---|
| 561 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
| 562 | |
---|
| 563 | form_id= wx.NewId() |
---|
| 564 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
| 565 | id=wx.NewId() |
---|
| 566 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
| 567 | |
---|
[d89f09b] | 568 | def _on_model(self, evt): |
---|
| 569 | """ |
---|
[5062bbf] | 570 | React to a model menu event |
---|
| 571 | |
---|
| 572 | :param event: wx menu event |
---|
| 573 | |
---|
[d89f09b] | 574 | """ |
---|
[bb18ef1] | 575 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
| 576 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
| 577 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
[5062bbf] | 578 | model = MultiplicationModel(model1, model2) |
---|
[bb18ef1] | 579 | else: |
---|
| 580 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
[6886000] | 581 | evt = ModelEvent(model=model) |
---|
[bb18ef1] | 582 | wx.PostEvent(self.event_owner, evt) |
---|
[d89f09b] | 583 | |
---|
[fb59ed9] | 584 | def _get_multifunc_models(self): |
---|
| 585 | """ |
---|
| 586 | Get the multifunctional models |
---|
| 587 | """ |
---|
| 588 | for item in self.plugins: |
---|
| 589 | try: |
---|
| 590 | # check the multiplicity if any |
---|
| 591 | if item.multiplicity_info[0] > 1: |
---|
| 592 | self.multi_func_list.append(item) |
---|
| 593 | except: |
---|
| 594 | # pass to other items |
---|
| 595 | pass |
---|
| 596 | |
---|
[d89f09b] | 597 | def get_model_list(self): |
---|
[5062bbf] | 598 | """ |
---|
| 599 | return dictionary of models for fitpanel use |
---|
| 600 | |
---|
| 601 | """ |
---|
[6bbeacd4] | 602 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
| 603 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
| 604 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
| 605 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
| 606 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
[376916c] | 607 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
[e87f9fc] | 608 | self.model_combobox.set_list("Multi-Functions", self.multi_func_list) |
---|
[b2d9826] | 609 | return self.model_combobox.get_list() |
---|
[d89f09b] | 610 | |
---|
[376916c] | 611 | |
---|
[bb18ef1] | 612 | |
---|
[bb9f322] | 613 | class ModelManager(object): |
---|
| 614 | """ |
---|
| 615 | implement model |
---|
| 616 | """ |
---|
| 617 | __modelmanager = ModelManagerBase() |
---|
| 618 | |
---|
| 619 | def findModels(self): |
---|
| 620 | return self.__modelmanager.findModels() |
---|
| 621 | |
---|
| 622 | def _getModelList(self): |
---|
| 623 | return self.__modelmanager._getModelList() |
---|
| 624 | |
---|
| 625 | def is_changed(self): |
---|
| 626 | return self.__modelmanager.is_changed() |
---|
| 627 | |
---|
| 628 | def update(self): |
---|
| 629 | return self.__modelmanager.update() |
---|
| 630 | |
---|
| 631 | def populate_menu(self, modelmenu, event_owner): |
---|
| 632 | return self.__modelmanager.populate_menu(modelmenu, event_owner) |
---|
| 633 | |
---|
| 634 | def _on_model(self, evt): |
---|
| 635 | return self.__modelmanager._on_model(evt) |
---|
| 636 | |
---|
| 637 | def _get_multifunc_models(self): |
---|
| 638 | return self.__modelmanager._get_multifunc_models() |
---|
| 639 | |
---|
| 640 | def get_model_list(self): |
---|
| 641 | return self.__modelmanager.get_model_list() |
---|
[bb18ef1] | 642 | |
---|
[d89f09b] | 643 | |
---|
[bb18ef1] | 644 | |
---|