xref: /openbmc/u-boot/tools/buildman/bsettings.py (revision 9b03f802)
1# Copyright (c) 2012 The Chromium OS Authors.
2#
3# SPDX-License-Identifier:	GPL-2.0+
4#
5
6import ConfigParser
7import os
8import StringIO
9
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()
21    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))
30
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:
43        return []
44    except:
45        raise
46
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)
56