[493c90c] | 1 | # Setup and find Custom config dir |
---|
| 2 | import sys |
---|
| 3 | import os.path |
---|
| 4 | import shutil |
---|
[ea5fa58] | 5 | from sans.guiframe.CategoryInstaller import CategoryInstaller |
---|
[74b5523] | 6 | |
---|
[493c90c] | 7 | CONF_DIR = 'config' |
---|
[94d6752] | 8 | APPLICATION_NAME = 'sasview' |
---|
[493c90c] | 9 | |
---|
[ea5fa58] | 10 | def _find_usersasview_dir(): |
---|
| 11 | """ |
---|
| 12 | Find and return user/.sasview dir |
---|
| 13 | """ |
---|
| 14 | dir = os.path.join(os.path.expanduser("~"), |
---|
| 15 | ("." + APPLICATION_NAME)) |
---|
| 16 | return dir |
---|
| 17 | |
---|
[493c90c] | 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 | """ |
---|
[ea5fa58] | 23 | u_dir = _find_usersasview_dir() |
---|
| 24 | dir = os.path.join(u_dir, CONF_DIR) |
---|
[493c90c] | 25 | |
---|
| 26 | return dir |
---|
| 27 | |
---|
| 28 | def _setup_conf_dir(path): |
---|
| 29 | """ |
---|
[ea5fa58] | 30 | Setup the custom config dir and cat file |
---|
[493c90c] | 31 | """ |
---|
| 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) |
---|
[f9505c30] | 36 | file = os.path.join(dir, "custom_config.py") |
---|
[ea5fa58] | 37 | u_dir = _find_usersasview_dir() |
---|
| 38 | cat_file = os.path.join(u_dir, "serialized_cat.p") |
---|
| 39 | # Place example user models as needed |
---|
| 40 | if not os.path.isfile(cat_file): |
---|
| 41 | try: |
---|
| 42 | default_cat_file = CategoryInstaller.get_default_file() |
---|
| 43 | if os.path.isfile(default_cat_file): |
---|
| 44 | shutil.copyfile(default_cat_file, cat_file) |
---|
| 45 | else: |
---|
| 46 | print "Unable to find/copy default cat file" |
---|
| 47 | except: |
---|
| 48 | print "Unable to copy default cat file to the user dir." |
---|
| 49 | |
---|
[493c90c] | 50 | # Place example user models as needed |
---|
[8ab3302] | 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 |
---|
[493c90c] | 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 | |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|