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