source: trunk/packages/xen-common/xen-common/tools/python/xen/util/dictio.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 1.8 KB
Line 
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#============================================================================
18
19
20def dict_read(dictname, filename):
21    """Loads <filename> and returns the dictionary named <dictname> from
22       the file.
23    """
24    dict = {}
25
26    # read in the config file
27    globs = {}
28    locs = {}
29    execfile(filename, globs, locs)
30
31    for (k, v) in locs.items():
32        if k == dictname:
33            dict = v
34            break
35
36    return dict
37
38def 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()
Note: See TracBrowser for help on using the repository browser.