1 | # Setup and find Custom config dir |
---|
2 | import sys |
---|
3 | import os.path |
---|
4 | import shutil |
---|
5 | |
---|
6 | CONF_DIR = 'config' |
---|
7 | APPLICATION_NAME = 'sasview' |
---|
8 | |
---|
9 | def _find_customconf_dir(): |
---|
10 | """ |
---|
11 | Find path of the config directory. |
---|
12 | The plugin directory is located in the user's home directory. |
---|
13 | """ |
---|
14 | dir = os.path.join(os.path.expanduser("~"), |
---|
15 | ("." + APPLICATION_NAME), CONF_DIR) |
---|
16 | |
---|
17 | return dir |
---|
18 | |
---|
19 | def _setup_conf_dir(path): |
---|
20 | """ |
---|
21 | Setup the custom config dir |
---|
22 | """ |
---|
23 | dir = _find_customconf_dir() |
---|
24 | # If the plugin directory doesn't exist, create it |
---|
25 | if not os.path.isdir(dir): |
---|
26 | os.makedirs(dir) |
---|
27 | file = os.path.join(dir, "custom_config.py") |
---|
28 | # Place example user models as needed |
---|
29 | try: |
---|
30 | if not os.path.isfile(file): |
---|
31 | shutil.copyfile(os.path.join(path, "custom_config.py"), file) |
---|
32 | except: |
---|
33 | # Check for data path next to exe/zip file. |
---|
34 | #Look for maximum n_dir up of the current dir to find plugins dir |
---|
35 | n_dir = 12 |
---|
36 | is_dir = False |
---|
37 | f_dir = path |
---|
38 | for i in range(n_dir): |
---|
39 | if i > 1: |
---|
40 | f_dir, _ = os.path.split(f_dir) |
---|
41 | temp_path = os.path.join(f_dir, "custom_config.py") |
---|
42 | if os.path.isfile(temp_path): |
---|
43 | shutil.copyfile(temp_path, file) |
---|
44 | is_dir = True |
---|
45 | break |
---|
46 | if not is_dir: |
---|
47 | raise |
---|
48 | |
---|
49 | return dir |
---|
50 | |
---|
51 | |
---|
52 | class SetupCustom(object): |
---|
53 | """ |
---|
54 | implement custom config dir |
---|
55 | """ |
---|
56 | def find_dir(self): |
---|
57 | return _find_customconf_dir() |
---|
58 | |
---|
59 | def setup_dir(self, path): |
---|
60 | return _setup_conf_dir(path) |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | |
---|