6
6
import os
7
7
import shutil
8
8
import subprocess
9
+ import sys
10
+
11
+ from pathlib import Path
9
12
10
13
from typing import (
11
14
List ,
@@ -22,10 +25,9 @@ class ConfigurationError(Exception):
22
25
class Configuration :
23
26
"""Configuration contains the launcher configuration as read from file."""
24
27
25
- config_dir = os .path .expanduser ("~/.config/cloe/launcher/" )
26
-
27
- config_file = os .path .expanduser ("~/.config/cloe/launcher/conf.toml" )
28
- runtime_dir = os .path .expanduser ("~/.cache/cloe/launcher" )
28
+ config_dir = Path ("~/.config/cloe/launcher/" ).expanduser ()
29
+ config_file = Path ("~/.config/cloe/launcher/conf.toml" ).expanduser ()
30
+ runtime_dir = Path ("~/.cache/cloe/launcher" ).expanduser ()
29
31
30
32
conf_version = "1"
31
33
_conf = {
@@ -41,15 +43,17 @@ class Configuration:
41
43
42
44
def __init__ (self ):
43
45
# Make configuration and runtime directories if needed:
44
- if not os . path .exists (self . config_dir ):
46
+ if not self . config_dir .exists ():
45
47
logging .info ("Create configuration directory: %s" , self .config_dir )
46
- os . makedirs ( self .config_dir )
47
- if not os . path .exists (self . runtime_dir ):
48
+ self .config_dir . mkdir ( parents = True )
49
+ if not self . runtime_dir .exists ():
48
50
logging .info ("Create runtime directory: %s" , self .runtime_dir )
49
- os . makedirs ( self .runtime_dir )
51
+ self .runtime_dir . mkdir ( parents = True )
50
52
51
53
# Load configuration file:
52
- if os .path .exists (self .config_file ):
54
+ if self .config_file .exists ():
55
+ if not self .config_file .is_file ():
56
+ raise ConfigurationError ("configuration file not readable" )
53
57
conf = toml .load (self .config_file )
54
58
if "version" not in conf :
55
59
raise ConfigurationError (
@@ -58,23 +62,26 @@ def __init__(self):
58
62
for k in conf .keys ():
59
63
self ._conf [k ] = conf [k ]
60
64
61
- def profile_runtime (self , hash : str ) -> str :
65
+ def print (self ):
66
+ toml .dump (self ._conf , sys .stdout )
67
+
68
+ def profile_runtime (self , hash : str ) -> Path :
62
69
"""Return the path to the runtime directory of the profile."""
63
- return os . path . join ( self .runtime_dir , hash )
70
+ return self .runtime_dir / hash
64
71
65
72
def write (self ) -> None :
66
73
"""Write current configuration to the disk."""
67
74
logging .info (f"Write configuration to { self .config_file } :\n { self ._conf } " )
68
75
with open (self .config_file , "w" , encoding = "utf-8" ) as file :
69
76
toml .dump (self ._conf , file )
70
77
71
- def edit (self , create : bool = False ) -> None :
78
+ def edit (self , create : bool = False ) -> int :
72
79
"""Open the configuration in the user's editor."""
73
80
editor = os .getenv ("EDITOR" )
74
81
if editor is None :
75
82
raise ConfigurationError ("environment variable EDITOR is unset" )
76
- if not create and not os . path .exists (self . config_file ):
83
+ if not create and not self . config_file .exists ():
77
84
raise ConfigurationError (f"configuration { self .config_file } does not exist" )
78
- cmd = [ editor , self .config_file ]
85
+ cmd = editor . split ( ' ' ) + [ str ( self .config_file ) ]
79
86
logging .info ("Exec: %s" , " " .join (cmd ))
80
- subprocess .call (cmd )
87
+ return subprocess .call (cmd )
0 commit comments