12
12
#####################################################################
13
13
import sys
14
14
import os
15
- import socket
16
- import subprocess
17
- import errno
18
15
import configparser
19
16
20
- from labscript_profile import LABSCRIPT_SUITE_PROFILE
21
- # Look for a 'labconfig' folder in the labscript install directory:
22
- if LABSCRIPT_SUITE_PROFILE is not None :
23
- config_prefix = os .path .join (LABSCRIPT_SUITE_PROFILE , 'labconfig' )
24
- else :
25
- # No labscript install directory found? Revert to system defaults
26
- if os .name == 'nt' :
27
- config_prefix = os .path .abspath (r'C:\labconfig' )
28
- else :
29
- config_prefix = os .path .join (os .getenv ('HOME' ),'labconfig' )
30
- if not os .path .exists (config_prefix ):
31
- config_prefix = '/etc/labconfig/'
17
+ from labscript_utils import dedent
18
+ from labscript_profile import default_labconfig_path , LABSCRIPT_SUITE_PROFILE
32
19
33
- if not os .path .exists (config_prefix ):
34
- message = (r"Couldn't find labconfig folder. Please ensure it exists. " +
35
- r"If the labscript suite is installed, labconfig must be <labscript_suite_profile>/labconfig/. " +
36
- r"If the labscript suite is not installed, then C:\labconfig\ is checked on Windows, " +
37
- r" and $HOME/labconfig/ then /etc/labconfig/ checked on unix." )
38
- raise IOError (message )
20
+ default_config_path = default_labconfig_path ()
39
21
40
- config_prefix = os .path .abspath (config_prefix )
41
-
42
- if sys .platform == 'darwin' :
43
- hostname = subprocess .check_output (['scutil' , '--get' , 'LocalHostName' ]).decode ('utf8' ).strip ()
44
- else :
45
- hostname = socket .gethostname ()
46
- default_config_path = os .path .join (config_prefix ,'%s.ini' % hostname )
47
-
48
-
49
- def mkdir_p (path ):
50
- try :
51
- os .makedirs (path )
52
- except OSError as exc :
53
- if exc .errno == errno .EEXIST and os .path .isdir (path ):
54
- pass
55
- else :
56
- raise
57
22
58
23
class EnvInterpolation (configparser .BasicInterpolation ):
59
24
"""Interpolation which expands environment variables in values,
@@ -63,69 +28,44 @@ def before_get(self, *args):
63
28
value = super (EnvInterpolation , self ).before_get (* args )
64
29
return os .path .expandvars (value )
65
30
31
+
66
32
class LabConfig (configparser .ConfigParser ):
67
33
NoOptionError = configparser .NoOptionError
68
34
NoSectionError = configparser .NoSectionError
69
35
70
- def __init__ (self ,config_path = default_config_path ,required_params = {},defaults = {}):
71
- if isinstance (config_path ,list ):
36
+ def __init__ (
37
+ self , config_path = default_config_path , required_params = None , defaults = None ,
38
+ ):
39
+ if required_params is None :
40
+ required_params = {}
41
+ if defaults is None :
42
+ defaults = {}
43
+ defaults ['labscript_suite' ] = LABSCRIPT_SUITE_PROFILE
44
+ if isinstance (config_path , list ):
72
45
self .config_path = config_path [0 ]
73
46
else :
74
47
self .config_path = config_path
75
48
76
49
self .file_format = ""
77
50
for section , options in required_params .items ():
78
- self .file_format += "[%s]\n " % section
51
+ self .file_format += "[%s]\n " % section
79
52
for option in options :
80
- self .file_format += "%s = <value>\n " % option
81
-
82
- # Ensure the folder exists:
83
- mkdir_p (os .path .dirname (self .config_path ))
84
-
85
- # If the file doesn't exist, create it
86
- if not os .path .exists (self .config_path ):
87
- with open (self .config_path ,'a+' ) as f :
88
- f .write (self .file_format )
53
+ self .file_format += "%s = <value>\n " % option
89
54
90
55
# Load the config file
91
- configparser .ConfigParser .__init__ (self , defaults = defaults , interpolation = EnvInterpolation ())
92
- self .read (config_path ) #read all files in the config path if it is a list (self.config_path only contains one string)
56
+ configparser .ConfigParser .__init__ (
57
+ self , defaults = defaults , interpolation = EnvInterpolation ()
58
+ )
59
+ # read all files in the config path if it is a list (self.config_path only
60
+ # contains one string):
61
+ self .read (config_path )
93
62
94
63
try :
95
64
for section , options in required_params .items ():
96
65
for option in options :
97
- self .get (section ,option )
98
-
99
- except configparser .NoOptionError as e :
100
- raise Exception ('The experiment configuration file located at %s does not have the required keys. Make sure the config file containes the following structure:\n %s' % (config_path , self .file_format ))
101
-
102
-
103
- # Overwrite the add_section method to only attempt to add a section if it doesn't
104
- # exist. We don't ever care whether a section exists or not, only that it does exist
105
- # when we try and save an attribute into it.
106
- def add_section (self ,section ):
107
- # Create the group if it doesn't exist
108
- if not section .lower () == 'default' and not self .has_section (section ):
109
- configparser .ConfigParser .add_section (self , section )
110
-
111
- # Overwrite the set method so that it adds the section if it doesn't exist,
112
- # and immediately saves the data to the file (to avoid data loss on program crash)
113
- def set (self , section , option , value ):
114
- self .add_section (section )
115
- configparser .ConfigParser .set (self ,section ,option ,value )
116
- self .save ()
117
-
118
- # Overwrite the remove section function so that it immediately saves the change to disk
119
- def remove_section (self ,section ):
120
- configparser .ConfigParser .remove_section (self ,section )
121
- self .save ()
122
-
123
- # Overwrite the remove option function so that it immediately saves the change to disk
124
- def remove_option (self ,section ,option ):
125
- configparser .ConfigParser .remove_option (self ,section ,option )
126
- self .save ()
127
-
128
- # Provide a convenience method to save the contents of the ConfigParser to disk
129
- def save (self ):
130
- with open (self .config_path , 'w+' ) as f :
131
- self .write (f )
66
+ self .get (section , option )
67
+ except configparser .NoOptionError :
68
+ msg = f"""The experiment configuration file located at { config_path } does
69
+ not have the required keys. Make sure the config file containes the
70
+ following structure:\n { self .file_format } """
71
+ raise Exception (dedent (msg ))
0 commit comments