def inifile(self, config, tmp_path):
     ini = IniFile(str(tmp_path / 'test.ini'))
     for key, value in config.items():
         ini['default.' + key] = value
     ini.save()
     return ini
Exemplo n.º 2
0
from inifile import IniFile

i = IniFile('hello.ini')
i['foo.bar'] = 'testing more stuff'
i['foo.mup'] = 'aha!'
print i.get_updated_lines()
i.save()
class SystemInfoReport(object):
    _plain_arrays = ['OpenGLExtensions/Extension']
    _tuples = {
        'System/Path' : ['Dir', 'Ok'],
        'Process/Module': ['Path', 'BaseAddress', 'Size', 'EntryPoint', 'FileVersion', 'ProductVersion', 'Timestamp', 'TimestampUtc'],
        'Windows/hotfix': ['id'],
        'Network/Interface': ['index', 'name', 'description', 'hwaddr', 'loopback', 'up', 'running', 'wireless', 'pointtopoint', 'multicast', 'broadcast', 'addr'],

        'terra3d-dirs': _Terra3DDirectories,
        'CPU': _SystemInfoCPU,
        'locale': _SystemInfoLocale,
        'Network': _SystemInfoNetwork,
        }
    _dicts = ['Environment']

    class SystemInfoReportException(Exception):
        def __init__(self, report, message):
            super(SystemInfoReport.SystemInfoReportException, self).__init__(message)
            self._message = message
            self.report = report

        def __str__(self):
            return '%s(%s): %s' % (type(self).__name__, self.report._filename, self._message)


    class SystemInfoReportIOError(SystemInfoReportException):
        def __init__(self, report, message):
            super(SystemInfoReport.SystemInfoReportIOError, self).__init__(report, message)

    class SystemInfoReportParserError(SystemInfoReportException):
        def __init__(self, report, message):
            super(SystemInfoReport.SystemInfoReportParserError, self).__init__(report, message)

    def __init__(self, filename=None, xmlreport=None):
        self._filename = None
        self._xmlreport = None
        self._ini = None
        self.platform_type = None
        self.is_64_bit = True

        if filename is not None:
            self.open(filename=filename)
        elif xmlreport is not None:
            self.open(xmlreport=xmlreport)

    def clear(self):
        self._filename = None
        self._ini = None

    def _get_as_plain_array(self, section, key, default_value):
        ret = []
        got_value = False
        num = 0
        while 1:
            value = self._ini.get(section, key + '%i' % num, None)
            if value is None:
                break
            got_value = True
            ret.append(value)
            num = num + 1
        if got_value:
            return ret
        else:
            return default_value

    def _get_tuples(self, section, key_path, names, default_value=None):
        if isinstance(names, type):
            ini_section = self._ini.section(section)
            if ini_section is not None:
                ret = names(self, ini_section, key_path, default_value)
            else:
                ret = default_value
        else:
            size = self._ini.getAsInteger(section, key_path + '\\size', None)
            if size is None:
                return default_value
            else:
                ret = []
                for i in range(1, size):
                    elem = {}
                    for n in names:
                        elem[n] = self._ini.get(section, key_path + '\\%i\\%s' % (i, n), None)
                    ret.append(elem)
        return ret

    def _get_dicts(self, section, key_path, names, default_value=None):
        ret = default_value
        if not key_path:
            ini_section = self._ini.section(section)
            if ini_section:
                ret = ini_section.get_all_as_dict()
        return ret

    def get(self, key, default_value=None):
        if self._ini is None:
            return default_value
        if '/' in key:
            section, key_path = key.split('/', 1)
            key_path = key_path.replace('/', '\\')
        else:
            section = key
            key_path = None
        if key in SystemInfoReport._plain_arrays:
            return self._get_as_plain_array(section, key_path, default_value)
        elif key in SystemInfoReport._tuples:
            return self._get_tuples(section, key_path, SystemInfoReport._tuples[key], default_value)
        elif key in SystemInfoReport._dicts:
            return self._get_dicts(section, key_path, default_value)
        elif isinstance(default_value, list):
            return self._ini.getAsArray(section, key_path, default_value)
        else:
            return self._ini.get(section, key_path, default_value)

    def __getitem__(self, name):
        return self.get(name)

    def _post_open(self):
        if self.platform_type is None:
            pass

    @property
    def is_platform_windows(self):
        return self.platform_type == 'Win32' or self.platform_type == 'Windows NT'

    def open(self, filename=None, xmlreport=None, minidump=None):
        if filename:
            try:
                self._ini = IniFile(filename, commentPrefix=';', keyValueSeperator='=', qt=True)
                self._filename = filename
            except IOError as e:
                raise SystemInfoReport.SystemInfoReportIOError(self, str(e))
        elif xmlreport is not None:
            self._xmlreport = xmlreport
            if xmlreport.fast_protect_system_info is None:
                raise SystemInfoReport.SystemInfoReportIOError(self, 'No system info include in XMLReport %s' % (xmlreport.filename))
            if sys.version_info[0] > 2:
                from io import StringIO
                stream = StringIO(xmlreport.fast_protect_system_info.rawdata.raw.decode('utf8'))
            else:
                from StringIO import StringIO
                stream = StringIO(xmlreport.fast_protect_system_info.rawdata.raw)
            self._ini = IniFile(filename=None, commentPrefix=';', keyValueSeperator='=', qt=True)
            self._ini.open(stream)
            self.platform_type = xmlreport.platform_type
            self.is_64_bit = xmlreport.is_64_bit
            #print('platform=%s' % (self.platform_type))
        elif minidump:
            raise SystemInfoReport.SystemInfoReportIOError(self, 'Not yet implemented')
        self._post_open()

    def save(self, filename):
        self._ini.save(filename)