Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. |
| 2 | # |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 4 | # |
| 5 | |
| 6 | import ConfigParser |
| 7 | import os |
Simon Glass | 5e0441d | 2014-09-05 19:00:15 -0600 | [diff] [blame] | 8 | import StringIO |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 9 | |
| 10 | |
| 11 | def 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 Glass | 5e0441d | 2014-09-05 19:00:15 -0600 | [diff] [blame] | 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 | |
| 28 | def AddFile(data): |
| 29 | settings.readfp(StringIO.StringIO(data)) |
Simon Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 30 | |
| 31 | def 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 Glass | c05694f | 2013-04-03 11:07:16 +0000 | [diff] [blame] | 43 | return [] |
| 44 | except: |
| 45 | raise |
Simon Glass | 7e803e1 | 2014-12-01 17:34:06 -0700 | [diff] [blame] | 46 | |
| 47 | def 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) |