debuggers.hg

view tools/python/xen/util/dictio.py @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children
line source
1 #===========================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2006 International Business Machines Corp.
16 # Author: Bryan D. Payne <bdpayne@us.ibm.com>
17 #============================================================================
20 def dict_read(dictname, filename):
21 """Loads <filename> and returns the dictionary named <dictname> from
22 the file.
23 """
24 dict = {}
26 # read in the config file
27 globs = {}
28 locs = {}
29 execfile(filename, globs, locs)
31 for (k, v) in locs.items():
32 if k == dictname:
33 dict = v
34 break
36 return dict
38 def dict_write(dict, dictname, filename):
39 """Writes <dict> to <filename> using the name <dictname>. If the file
40 contains any other data, it will be overwritten.
41 """
42 prefix = dictname + " = {\n"
43 suffix = "}\n"
44 fd = open(filename, "wb")
45 fd.write(prefix)
46 for key in dict:
47 line = " '" + str(key) + "': " + str(dict[key]) + ",\n"
48 fd.write(line)
49 fd.write(suffix)
50 fd.close()