7
7
import shutil
8
8
import subprocess
9
9
10
- from typing import List
11
- from typing import Optional
10
+ from typing import (
11
+ List ,
12
+ Optional ,
13
+ )
12
14
13
15
import toml
14
16
@@ -23,7 +25,6 @@ class Configuration:
23
25
config_dir = os .path .expanduser ("~/.config/cloe/launcher/" )
24
26
25
27
config_file = os .path .expanduser ("~/.config/cloe/launcher/conf.toml" )
26
- profiles_dir = os .path .join (config_dir , "profiles" )
27
28
runtime_dir = os .path .expanduser ("~/.cache/cloe/launcher" )
28
29
29
30
conf_version = "1"
@@ -38,20 +39,13 @@ class Configuration:
38
39
},
39
40
}
40
41
41
- all_profiles : List [str ] = []
42
- default_profile : Optional [str ] = None
43
- current_profile = None
44
-
45
- def __init__ (self , profile : str = None ):
42
+ def __init__ (self ):
46
43
# Make configuration and runtime directories if needed:
47
44
if not os .path .exists (self .config_dir ):
48
- logging .info ("Create configuration directory:" , self .config_dir )
45
+ logging .info ("Create configuration directory: %s " , self .config_dir )
49
46
os .makedirs (self .config_dir )
50
- if not os .path .exists (self .profiles_dir ):
51
- logging .info ("Create profile directory:" , self .profiles_dir )
52
- os .makedirs (self .profiles_dir )
53
47
if not os .path .exists (self .runtime_dir ):
54
- logging .info ("Create runtime directory:" , self .runtime_dir )
48
+ logging .info ("Create runtime directory: %s " , self .runtime_dir )
55
49
os .makedirs (self .runtime_dir )
56
50
57
51
# Load configuration file:
@@ -63,76 +57,24 @@ def __init__(self, profile: str = None):
63
57
)
64
58
for k in conf .keys ():
65
59
self ._conf [k ] = conf [k ]
66
- self .default_profile = self ._conf ["default_profile" ]
67
-
68
- # Read all profile names from profile directory
69
- self .all_profiles = [
70
- f
71
- for f in os .listdir (self .profiles_dir )
72
- if os .path .isfile (self .profile_path (f ))
73
- ]
74
-
75
- # Set current profile
76
- if profile is not None :
77
- self .set_current (profile )
78
- elif self .default_profile is not None :
79
- self .set_current (self .default_profile )
80
-
81
- def profile_path (self , profile : str ) -> str :
82
- """Return the path to named profile."""
83
- return os .path .join (self .profiles_dir , profile )
84
60
85
- def profile_runtime (self , profile : str ) -> str :
61
+ def profile_runtime (self , hash : str ) -> str :
86
62
"""Return the path to the runtime directory of the profile."""
87
- return os .path .join (self .runtime_dir , profile )
63
+ return os .path .join (self .runtime_dir , hash )
88
64
89
- def set_current (self , profile : str ) -> None :
90
- """Set the current profile and make sure it exists."""
91
- self .current_profile = profile
92
-
93
- def set_default (self , profile : str ) -> None :
94
- """Set the default profile and write it to the configuration."""
95
- if profile is not None and profile not in self .all_profiles :
96
- raise ConfigurationError (f"profile { profile } does not exist" )
97
- self .default_profile = profile
98
- self ._conf ["default_profile" ] = profile
65
+ def write (self ) -> None :
66
+ """Write current configuration to the disk."""
99
67
logging .info (f"Write configuration to { self .config_file } :\n { self ._conf } " )
100
- with open (self .config_file , "w" ) as file :
68
+ with open (self .config_file , "w" , encoding = "utf-8" ) as file :
101
69
toml .dump (self ._conf , file )
102
70
103
- def read (self , profile : str ) -> str :
104
- """Read the specified profile."""
105
- logging .info ("Open:" , self .profile_path (profile ))
106
- with open (self .profile_path (profile )) as file :
107
- return file .read ()
108
-
109
- def edit (self , profile : str , create : bool = False ) -> None :
110
- """Open the specified profile in the user's editor."""
71
+ def edit (self , create : bool = False ) -> None :
72
+ """Open the configuration in the user's editor."""
111
73
editor = os .getenv ("EDITOR" )
112
74
if editor is None :
113
75
raise ConfigurationError ("environment variable EDITOR is unset" )
114
- if not create and not os .path .exists (self .profile_path ( profile ) ):
115
- raise ConfigurationError (f"profile { profile } does not exist" )
116
- cmd = [editor , self .profile_path ( profile ) ]
117
- logging .info ("Exec:" , " " .join (cmd ))
76
+ if not create and not os .path .exists (self .config_file ):
77
+ raise ConfigurationError (f"configuration { self . config_file } does not exist" )
78
+ cmd = [editor , self .config_file ]
79
+ logging .info ("Exec: %s " , " " .join (cmd ))
118
80
subprocess .call (cmd )
119
-
120
- def add (self , profile : str , file : str , force : bool = False ) -> None :
121
- """Add the file as a profile."""
122
- if profile in self .all_profiles and not force :
123
- raise ConfigurationError (
124
- f"cannot overwrite profile { profile } unless forced"
125
- )
126
- logging .debug ("Copy: {} -> {}" .format (file , self .profile_path (profile )))
127
- shutil .copyfile (file , self .profile_path (profile ))
128
- if profile not in self .all_profiles :
129
- self .all_profiles .append (profile )
130
-
131
- def remove (self , profile : str ) -> None :
132
- """Remove the profile, if it exists."""
133
- file = os .path .join (self .profiles_dir , profile )
134
- if os .path .exists (file ):
135
- logging .info ("Remove:" , file )
136
- os .remove (file )
137
- if self .default_profile == profile :
138
- self .set_default (None )
0 commit comments