root/trunk/umitCore/UmitConfigParser.py @ 1306

Revision 1306, 3.8 kB (checked in by boltrix, 6 years ago)

More work on installers, umitCore.Paths and umitCore.Logging which now has been renamed to umitCore.UmitLogging?. Windows installers are working fine, yet, a script updating the Paths's configuration is need as well as more test on it. Also, Linux installers seens to be working fine. The Mac OS X App generator is half-way done, and have some issues while importing cairo yet. Also, hardcoded the paths to umitCore.Paths and made a basic test suite to ensure that it is working. More testing is needed, and probably some minor bugs shal raise.

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2005 Insecure.Com LLC.
5#
6# Author: Adriano Monteiro Marques <py.adriano@gmail.com>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22from os.path import exists
23from ConfigParser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError
24from umitCore.UmitLogging import log
25
26class UmitConfigParser(ConfigParser):
27    filenames = None
28    fp = None
29   
30    def __init__(self, *args):
31        ConfigParser.__init__(self, *args)
32
33    def set(self, section, option, value):
34        if not self.has_section(section):
35            self.add_section(section)
36       
37        ConfigParser.set(self, section, option, value)
38        self.save_changes()
39
40    def read(self, filename):
41        log.debug(">>> Trying to parse: %s" % filename)
42
43        self.filename = ConfigParser.read(self, filename)
44        return self.filename
45
46    def readfp(self, fp, filename=None):
47        ConfigParser.readfp(self, fp, filename)
48        self.fp = fp
49        self.filenames = filename
50
51    def save_changes(self):
52        if self.filenames:
53            filename = None
54            if type(self.filenames) == type(""):
55                filename = self.filenames
56            elif type(self.filenames) == type([]) and len(self.filenames) == 1:
57                filename = self.filenames[0]
58            else:
59                raise Exception("Wrong filename %s" % self.filenames)
60            self.write(open(filename, 'w'))
61        elif self.fp:
62            self.write(self.fp)
63
64    def write(self, fp):
65        '''Write alphabetically sorted config files'''
66        if self._defaults:
67            fp.write("[%s]\n" % DEFAULTSECT)
68           
69            items = self._defaults.items()
70            items.sort()
71           
72            for (key, value) in items:
73                fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
74            fp.write("\n")
75
76        sects = self._sections.keys()
77        sects.sort()
78       
79        for section in sects:
80            fp.write("[%s]\n" % section)
81            for (key, value) in self._sections[section].items():
82                if key != "__name__":
83                    fp.write("%s = %s\n" %
84                             (key, str(value).replace('\n', '\n\t')))
85            fp.write("\n")
86
87def test_umit_conf_content(filename):
88    parser = ConfigParser()
89    parser.read(filename)
90
91    # Paths section
92    section = "paths"
93    assert exists(get_or_false(parser, section, "config_file") or "")
94    assert exists(get_or_false(parser, section, "umit_icon") or "")
95    assert exists(get_or_false(parser, section, "locale_dir") or "")
96    assert exists(get_or_false(parser, section, "misc_dir") or "")
97    assert exists(get_or_false(parser, section, "icons_dir") or "")
98    assert exists(get_or_false(parser, section, "pixmaps_dir") or "")
99    assert exists(get_or_false(parser, section, "config_dir") or "")
100    assert exists(get_or_false(parser, section, "docs_dir") or "")
101    assert get_or_false(parser, section, "nmap_command_path")
102
103
104def get_or_false(parser, section, option):
105    try:
106        result = parser.get(section, option)
107        return result
108    except NoOptionError:
109        return False
110    except NoSectionError:
111        return False
Note: See TracBrowser for help on using the browser.