gstlal 1.13.0
Loading...
Searching...
No Matches
__init__.py
1import fnmatch
2import os
3import shutil
4import pkg_resources
5
6import yaml
7
8
9def get_profile_path():
10 """Get the default location for site profiles.
11
12 """
13 return os.path.join(os.getenv("HOME"), ".config", "gstlal")
14
15
16def installed_profiles():
17 return [f[:-4] for f in os.listdir(get_profile_path()) if fnmatch.fnmatch(f, '*.yml')]
18
19
20def current_profile():
21 current_profile = os.path.join(get_profile_path(), "current.txt")
22 if not os.path.exists(current_profile):
23 raise ValueError("no current profile selected")
24 with open(current_profile, "r") as f:
25 return f.read()
26
27
28def load_profile(profile=None):
29 if not profile:
30 profile = current_profile()
31 profile_loc = os.path.join(get_profile_path(), f'{profile}.yml')
32 try:
33 with open(profile_loc, "r") as f:
34 return yaml.safe_load(f)
35 except FileNotFoundError as e:
36 raise FileNotFoundError(f'%s\n{profile}.yaml is not installed. Run "gstlal_grid_profile install".' % e)
37
38
39def install_profiles(args):
40 """Install site profiles in a central config location.
41
42 """
43 os.makedirs(get_profile_path(), exist_ok=True)
44 if args.profile:
45 profiles = [f for f in args.profile if fnmatch.fnmatch(f, '*.yml')]
46 else:
47 files = pkg_resources.resource_listdir('gstlal.dags', 'profiles')
48 profiles = [f for f in files if fnmatch.fnmatch(f, '*.yml')]
49 profiles = [pkg_resources.resource_filename('gstlal.dags', os.path.join('profiles', f)) for f in profiles]
50 for profile in profiles:
51 shutil.copy2(profile, get_profile_path())
52
53
54def list_profiles(args):
55 """List currently installed site profiles.
56
57 """
58 print("installed site profiles:")
59 print("\t" + " ".join(installed_profiles()))
60
61
62def get_profile(args):
63 """Display currently selected site profile.
64
65 """
66 try:
67 current = current_profile()
68 except ValueError:
69 print("no current profile selected")
70 else:
71 print(f"current profile: {current}")
72
73
74def set_profile(args):
75 if args.profile not in installed_profiles():
76 print("invalid profile selection, run gstlal_grid_profile list to get a list of valid profiles")
77 with open(os.path.join(get_profile_path(), "current.txt"), "w") as f:
78 f.write(args.profile)