blob: b36146918005b92ce6eafd5af88ba664b0200bfa [file] [log] [blame]
Simon Glassfc3fe1c2013-04-03 11:07:16 +00001# Copyright (c) 2012 The Chromium OS Authors.
2#
Wolfgang Denk1a459662013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glassfc3fe1c2013-04-03 11:07:16 +00004#
5
6import ConfigParser
7import os
Simon Glass8b985ee2014-09-05 19:00:15 -06008import StringIO
Simon Glassfc3fe1c2013-04-03 11:07:16 +00009
10
11def Setup(fname=''):
12 """Set up the buildman settings module by reading config files
13
14 Args:
15 config_fname: Config filename to read ('' for default)
16 """
17 global settings
18 global config_fname
19
20 settings = ConfigParser.SafeConfigParser()
Simon Glass8b985ee2014-09-05 19:00:15 -060021 if fname is not None:
22 config_fname = fname
23 if config_fname == '':
24 config_fname = '%s/.buildman' % os.getenv('HOME')
25 if config_fname:
26 settings.read(config_fname)
27
28def AddFile(data):
29 settings.readfp(StringIO.StringIO(data))
Simon Glassfc3fe1c2013-04-03 11:07:16 +000030
31def GetItems(section):
32 """Get the items from a section of the config.
33
34 Args:
35 section: name of section to retrieve
36
37 Returns:
38 List of (name, value) tuples for the section
39 """
40 try:
41 return settings.items(section)
42 except ConfigParser.NoSectionError as e:
Simon Glassfc3fe1c2013-04-03 11:07:16 +000043 return []
44 except:
45 raise
Simon Glass827e37b2014-12-01 17:34:06 -070046
47def SetItem(section, tag, value):
48 """Set an item and write it back to the settings file"""
49 global settings
50 global config_fname
51
52 settings.set(section, tag, value)
53 if config_fname is not None:
54 with open(config_fname, 'w') as fd:
55 settings.write(fd)