Ignore:
Timestamp:
Aug 31, 2016 11:21:55 AM (8 years ago)
Author:
Gonzalez, Miguel <gonzalez@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
ce94504
Parents:
ec30905 (diff), 4036cb0 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of https://github.com/SasView/sasview

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/perspectives/fitting/models.py

    r6afc14b rdceff6e  
    22    Utilities to manage models 
    33""" 
    4 import imp 
     4import traceback 
    55import os 
    66import sys 
     
    88# Time is needed by the log method 
    99import time 
     10import datetime 
    1011import logging 
    1112import py_compile 
     
    2021 
    2122PLUGIN_DIR = 'plugin_models' 
     23PLUGIN_LOG = os.path.join(os.path.expanduser("~"), '.sasview', PLUGIN_DIR, 
     24                          "plugins.log") 
    2225 
    2326def get_model_python_path(): 
     
    2831 
    2932 
    30 def log(message): 
     33def plugin_log(message): 
    3134    """ 
    3235    Log a message in a file located in the user's home directory 
    3336    """ 
    34     dir = os.path.join(os.path.expanduser("~"), '.sasview', PLUGIN_DIR) 
    35     out = open(os.path.join(dir, "plugins.log"), 'a') 
    36     out.write("%10g:  %s\n" % (time.clock(), message)) 
     37    out = open(PLUGIN_LOG, 'a') 
     38    now = time.time() 
     39    stamp = datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S') 
     40    out.write("%s: %s\n" % (stamp, message)) 
    3741    out.close() 
    3842 
     
    5155    if not issubclass(model, Model1DPlugin): 
    5256        msg = "Plugin %s must be of type Model1DPlugin \n" % str(name) 
    53         log(msg) 
     57        plugin_log(msg) 
    5458        return None 
    5559    if model.__name__ != "Model": 
    5660        msg = "Plugin %s class name must be Model \n" % str(name) 
    57         log(msg) 
     61        plugin_log(msg) 
    5862        return None 
    5963    try: 
     
    6367                                                             str(sys.exc_type), 
    6468                                                             sys.exc_info()[1]) 
    65         log(msg) 
     69        plugin_log(msg) 
    6670        return None 
    6771 
     
    7276            msg = "Plugin %s: error writing function \n\t :%s %s\n " % \ 
    7377                    (str(name), str(sys.exc_type), sys.exc_info()[1]) 
    74             log(msg) 
     78            plugin_log(msg) 
    7579            return None 
    7680    else: 
    7781        msg = "Plugin  %s needs a method called function \n" % str(name) 
    78         log(msg) 
     82        plugin_log(msg) 
    7983        return None 
    8084    return model 
     
    132136    """ 
    133137    def __nonzero__(self): 
    134         type, value, traceback = sys.exc_info() 
     138        type, value, tb = sys.exc_info() 
    135139        if type is not None and issubclass(type, py_compile.PyCompileError): 
    136140            print "Problem with", repr(value) 
    137             raise type, value, traceback 
     141            raise type, value, tb 
    138142        return 1 
    139143 
     
    155159 
    156160def _findModels(dir): 
     161    """ 
     162    Find custom models 
     163    """ 
    157164    # List of plugin objects 
    158     plugins = {} 
    159165    dir = find_plugins_dir() 
    160166    # Go through files in plug-in directory 
    161     #always recompile the folder plugin 
    162167    if not os.path.isdir(dir): 
    163         msg = "SasView couldn't locate Model plugin folder." 
    164         msg += """ "%s" does not exist""" % dir 
     168        msg = "SasView couldn't locate Model plugin folder %r." % dir 
    165169        logging.warning(msg) 
    166         return plugins 
    167     else: 
    168         log("looking for models in: %s" % str(dir)) 
    169         compile_file(dir) 
    170         logging.info("plugin model dir: %s" % str(dir)) 
    171     try: 
    172         list = os.listdir(dir) 
    173         for item in list: 
    174             toks = os.path.splitext(os.path.basename(item)) 
    175             if toks[1] == '.py' and not toks[0] == '__init__': 
    176                 name = toks[0] 
    177                 path = [os.path.abspath(dir)] 
    178                 file = None 
    179                 try: 
    180                     (file, path, info) = imp.find_module(name, path) 
    181                     module = imp.load_module(name, file, item, info) 
    182                     if hasattr(module, "Model"): 
    183                         try: 
    184                             if _check_plugin(module.Model, name) != None: 
    185                                 plugins[name] = module.Model 
    186                         except: 
    187                             msg = "Error accessing Model" 
    188                             msg += "in %s\n  %s %s\n" % (name, 
    189                                                          str(sys.exc_type), 
    190                                                          sys.exc_info()[1]) 
    191                             log(msg) 
    192                     else: 
    193                         filename = os.path.join(dir, item) 
    194                         plugins[name] = load_custom_model(filename) 
    195  
    196                 except: 
    197                     msg = "Error accessing Model" 
    198                     msg += " in %s\n  %s %s \n" % (name, 
    199                                                    str(sys.exc_type), 
    200                                                    sys.exc_info()[1]) 
    201                     log(msg) 
    202                 finally: 
    203  
    204                     if not file == None: 
    205                         file.close() 
    206     except: 
    207         # Don't deal with bad plug-in imports. Just skip. 
    208         msg = "Could not import model plugin: %s" % sys.exc_info()[1] 
    209         log(msg) 
    210  
     170        return {} 
     171 
     172    plugin_log("looking for models in: %s" % str(dir)) 
     173    #compile_file(dir)  #always recompile the folder plugin 
     174    logging.info("plugin model dir: %s" % str(dir)) 
     175 
     176    plugins = {} 
     177    for filename in os.listdir(dir): 
     178        name, ext = os.path.splitext(filename) 
     179        if ext == '.py' and not name == '__init__': 
     180            path = os.path.abspath(os.path.join(dir, filename)) 
     181            try: 
     182                model = load_custom_model(path) 
     183                plugins[model.name] = model 
     184            except Exception: 
     185                msg = traceback.format_exc() 
     186                msg += "\nwhile accessing model in %r" % path 
     187                plugin_log(msg) 
     188                logging.warning("Failed to load plugin %r. See %s for details" 
     189                                % (path, PLUGIN_LOG)) 
     190             
    211191    return plugins 
    212192 
Note: See TracChangeset for help on using the changeset viewer.