Changeset efe730d in sasview for src/sas/sasgui/guiframe/customdir.py
- Timestamp:
- Jun 25, 2016 4:07:33 AM (8 years ago)
- 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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 899e084
- Parents:
- 9501661
- git-author:
- Paul Kienzle <pkienzle@…> (06/25/16 02:23:59)
- git-committer:
- Paul Kienzle <pkienzle@…> (06/25/16 04:07:33)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/guiframe/customdir.py
rd85c194 refe730d 1 1 # Setup and find Custom config dir 2 2 import sys 3 import imp 3 4 import os.path 5 import logging 4 6 import shutil 7 5 8 from sas.sasgui.guiframe.CategoryInstaller import CategoryInstaller 9 from sas.sasgui import get_user_dir, get_app_dir 6 10 7 CONF_DIR = 'config' 8 APPLICATION_NAME = 'sasview' 11 def get_custom_config_path(): 12 dirname = os.path.join(get_user_dir(), 'config') 13 # If the directory doesn't exist, create it 14 if not os.path.exists(dirname): 15 os.makedirs(dirname) 16 path = os.path.join(dirname, "custom_config.py") 17 return path 9 18 10 def _find_usersasview_dir():11 """12 Find and return user/.sasview dir13 """14 dir = os.path.join(os.path.expanduser("~"),15 ("." + APPLICATION_NAME))16 return dir17 19 18 def _find_customconf_dir(): 19 """ 20 Find path of the config directory. 21 The plugin directory is located in the user's home directory. 22 """ 23 u_dir = _find_usersasview_dir() 24 dir = os.path.join(u_dir, CONF_DIR) 25 26 return dir 27 28 def _setup_conf_dir(path): 20 _config_cache = None 21 def setup_custom_config(): 29 22 """ 30 23 Setup the custom config dir and cat file 31 24 """ 32 dir = _find_customconf_dir() 33 # If the plugin directory doesn't exist, create it 34 if not os.path.isdir(dir): 35 os.makedirs(dir) 36 file = os.path.join(dir, "custom_config.py") 25 global _config_cache 26 if not _config_cache: 27 _config_cache = _setup_custom_config() 28 return _config_cache 29 30 31 def _setup_custom_config(): 32 path = get_custom_config_path() 33 if not os.path.isfile(path): 34 # if the custom config file does not exist, copy the default from 35 # the app dir 36 try: 37 shutil.copyfile(os.path.join(get_app_dir(), "custom_config.py"), 38 path) 39 except Exception: 40 #import traceback; logging.error(traceback.format_exc()) 41 logging.error("Could not copy default custom config.") 42 43 custom_config = _load_config(path) 44 37 45 cat_file = CategoryInstaller.get_user_file() 38 46 # If the user category file doesn't exist copy the default to … … 44 52 shutil.copyfile(default_cat_file, cat_file) 45 53 else: 46 print "Unable to find/copy default cat file"47 except :48 print "Unable to copy default cat file to the user dir."54 logging.error("Unable to find/copy default cat file") 55 except Exception: 56 logging.error("Unable to copy default cat file to the user dir.") 49 57 50 # Place example user models as needed 51 try: 52 if not os.path.isfile(file): 53 shutil.copyfile(os.path.join(path, "custom_config.py"), file) 54 except: 55 # Check for data path next to exe/zip file. 56 #Look for maximum n_dir up of the current dir to find plugins dir 57 n_dir = 12 58 is_dir = False 59 f_dir = path 60 for i in range(n_dir): 61 if i > 1: 62 f_dir, _ = os.path.split(f_dir) 63 temp_path = os.path.join(f_dir, "custom_config.py") 64 if os.path.isfile(temp_path): 65 shutil.copyfile(temp_path, file) 66 is_dir = True 67 break 68 if not is_dir: 69 raise 70 71 return dir 72 73 74 class SetupCustom(object): 75 """ 76 implement custom config dir 77 """ 78 def find_dir(self): 79 return _find_customconf_dir() 80 81 def setup_dir(self, path): 82 return _setup_conf_dir(path) 83 58 return custom_config 84 59 85 86 87 60 61 def _load_config(path): 62 if os.path.exists(path): 63 try: 64 fObj = None 65 fObj, config_path, descr = imp.find_module('custom_config', [os.path.dirname(path)]) 66 custom_config = imp.load_module('custom_config', fObj, config_path, descr) 67 logging.info("GuiManager loaded %s" % config_path) 68 return custom_config 69 except Exception: 70 logging.error("Error loading %s: %s" % (path, sys.exc_value)) 71 finally: 72 if fObj is not None: 73 fObj.close() 74 from sas.sasview import custom_config 75 logging.info("GuiManager custom_config defaults to sas.sasview.custom_config") 76 return custom_config
Note: See TracChangeset
for help on using the changeset viewer.