1313
1414
1515class Config :
16+ default_section = "main"
17+
1618 def __init__ (self , path = None ):
17- config = CP ()
19+ self . _cp = CP (default_section = Config . default_section )
1820
1921 if path is None :
2022 xdg_config_home = os .environ .get ("XDG_CONFIG_HOME" ) or os .path .join (
@@ -33,39 +35,34 @@ def __init__(self, path=None):
3335 config_files = [path ]
3436
3537 logger .debug ("Looking for config files from " + ":" .join (config_files ))
36- files_read = config .read (config_files )
38+ files_read = self . _cp .read (config_files )
3739 logger .debug ("Found config files in " + ":" .join (files_read ))
3840 self ._path = files_read [- 1 ] if files_read else None
3941
40- self .build_root = self ._get_build_root (config )
41- self .cache_root = self ._get_cache_root (config )
42- self .library_root = self ._get_library_root (config )
43- self .ignored_dirs = self ._get_ignored_dirs (config )
44-
4542 os .makedirs (self .cache_root , exist_ok = True )
4643
4744 # Parse library sections
4845 libraries = []
49- library_sections = [x for x in config .sections () if x .startswith ("library" )]
46+ library_sections = [x for x in self . _cp .sections () if x .startswith ("library" )]
5047 for section in library_sections :
5148 name = section .partition ("." )[2 ]
5249 try :
53- location = config .get (section , "location" )
50+ location = self . _cp .get (section , "location" )
5451 except configparser .NoOptionError :
5552 location = os .path .join (self .library_root , name )
5653
5754 try :
58- auto_sync = config .getboolean (section , "auto-sync" )
55+ auto_sync = self . _cp .getboolean (section , "auto-sync" )
5956 except configparser .NoOptionError :
6057 auto_sync = True
6158 except ValueError as e :
6259 _s = "Error parsing auto-sync '{}'. Ignoring library '{}'"
6360 logger .warning (_s .format (str (e ), name ))
6461 continue
6562
66- sync_uri = config .get (section , "sync-uri" , fallback = None )
67- sync_version = config .get (section , "sync-version" , fallback = None )
68- sync_type = config .get (section , "sync-type" , fallback = None )
63+ sync_uri = self . _cp .get (section , "sync-uri" , fallback = None )
64+ sync_version = self . _cp .get (section , "sync-version" , fallback = None )
65+ sync_type = self . _cp .get (section , "sync-type" , fallback = None )
6966
7067 libraries .append (
7168 Library (name , location , sync_type , sync_uri , sync_version , auto_sync )
@@ -81,8 +78,14 @@ def __init__(self, path=None):
8178 logger .debug ("cache_root=" + self .cache_root )
8279 logger .debug ("library_root=" + self .library_root )
8380
81+ def __enter__ (self ):
82+ return self
83+
84+ def __exit__ (self , * args ):
85+ self .write ()
86+
8487 def _resolve_path_from_cfg (self , path ):
85- # We only call resolve_path_from_cfg if config .get(...) returned
88+ # We only call resolve_path_from_cfg if self._cp .get(...) returned
8689 # something. That, in turn, only happens if we actually managed to read
8790 # a config file, meaning that files_read will have been nonempty in the
8891 # constructor and self._path will not be None.
@@ -95,23 +98,23 @@ def _resolve_path_from_cfg(self, path):
9598 cfg_file_dir = os .path .dirname (self ._path )
9699 return os .path .join (cfg_file_dir , expanded )
97100
98- def _path_from_cfg (self , config , name ):
99- as_str = config . get ("main" , name , fallback = None )
101+ def _path_from_cfg (self , name ):
102+ as_str = self . _cp . get (Config . default_section , name , fallback = None )
100103 return self ._resolve_path_from_cfg (as_str ) if as_str is not None else None
101104
102- def _paths_from_cfg (self , config , name ):
103- paths = config . get ("main" , name , fallback = "" )
105+ def _paths_from_cfg (self , name ):
106+ paths = self . _cp . get (Config . default_section , name , fallback = "" )
104107 return [self ._resolve_path_from_cfg (p ) for p in paths .split ()]
105108
106- def _get_build_root (self , config ):
107- from_cfg = self ._path_from_cfg (config , "build_root" )
109+ def _get_build_root (self ):
110+ from_cfg = self ._path_from_cfg ("build_root" )
108111 if from_cfg is not None :
109112 return from_cfg
110113
111114 return os .path .abspath ("build" )
112115
113- def _get_cache_root (self , config ):
114- from_cfg = self ._path_from_cfg (config , "cache_root" )
116+ def _get_cache_root (self ):
117+ from_cfg = self ._path_from_cfg ("cache_root" )
115118 if from_cfg is not None :
116119 return from_cfg
117120
@@ -120,8 +123,8 @@ def _get_cache_root(self, config):
120123 )
121124 return os .path .join (xdg_cache_home , "fusesoc" )
122125
123- def _get_library_root (self , config ):
124- from_cfg = self ._path_from_cfg (config , "library_root" )
126+ def _get_library_root (self ):
127+ from_cfg = self ._path_from_cfg ("library_root" )
125128 if from_cfg is not None :
126129 return from_cfg
127130
@@ -130,40 +133,79 @@ def _get_library_root(self, config):
130133 )
131134 return os .path .join (xdg_data_home , "fusesoc" )
132135
133- def _get_ignored_dirs (self , config ):
134- return self ._paths_from_cfg (config , "ignored_dirs" )
136+ def _get_ignored_dirs (self ):
137+ return self ._paths_from_cfg ("ignored_dirs" )
138+
139+ def _set_default_section (self , name , val ):
140+ self ._cp .set (Config .default_section , name , str (val ))
141+
142+ @property
143+ def build_root (self ):
144+ return self ._get_build_root ()
145+
146+ @build_root .setter
147+ def build_root (self , val ):
148+ self ._set_default_section ("build_root" , val )
149+
150+ @property
151+ def cache_root (self ):
152+ return self ._get_cache_root ()
153+
154+ @cache_root .setter
155+ def cache_root (self , val ):
156+ self ._set_default_section ("cache_root" , val )
157+
158+ @property
159+ def library_root (self ):
160+ return self ._get_library_root ()
161+
162+ @library_root .setter
163+ def library_root (self , val ):
164+ self ._set_default_section ("library_root" , val )
165+
166+ @property
167+ def ignored_dirs (self ):
168+ return self ._get_ignored_dirs ()
169+
170+ @ignored_dirs .setter
171+ def ignored_dirs (self , val ):
172+ self ._set_default_section (
173+ "ignored_dirs" , " " .join (val ) if type (val ) == list else val
174+ )
175+
176+ def write (self ):
177+ if not hasattr (self , "_path" ):
178+ raise RuntimeError ("No FuseSoC config file found - can't write config" )
179+
180+ with open (self ._path , "w" ) as conf_file :
181+ self ._cp .write (conf_file )
135182
136183 def add_library (self , library ):
137184 from fusesoc .provider import get_provider
138185
139- if not hasattr (self , "_path" ):
140- raise RuntimeError ("No FuseSoC config file found - can't add library" )
141186 section_name = "library." + library .name
142187
143- config = CP ()
144- config .read (self ._path )
145-
146- if section_name in config .sections ():
188+ if section_name in self ._cp .sections ():
147189 logger .warning (
148190 "Not adding library. {} already exists in configuration file" .format (
149191 library .name
150192 )
151193 )
152194 return
153195
154- config .add_section (section_name )
196+ self . _cp .add_section (section_name )
155197
156- config .set (section_name , "location" , library .location )
198+ self . _cp .set (section_name , "location" , library .location )
157199
158200 if library .sync_type :
159- config .set (section_name , "sync-uri" , library .sync_uri )
201+ self . _cp .set (section_name , "sync-uri" , library .sync_uri )
160202
161203 if library .sync_version is not None :
162- config .set (section_name , "sync-version" , library .sync_version )
204+ self . _cp .set (section_name , "sync-version" , library .sync_version )
163205
164- config .set (section_name , "sync-type" , library .sync_type )
206+ self . _cp .set (section_name , "sync-type" , library .sync_type )
165207 _auto_sync = "true" if library .auto_sync else "false"
166- config .set (section_name , "auto-sync" , _auto_sync )
208+ self . _cp .set (section_name , "auto-sync" , _auto_sync )
167209
168210 try :
169211 provider = get_provider (library .sync_type )
@@ -172,5 +214,4 @@ def add_library(self, library):
172214
173215 provider .init_library (library )
174216
175- with open (self ._path , "w" ) as conf_file :
176- config .write (conf_file )
217+ self .write ()
0 commit comments