AO2XP/ini.py

47 lines
1.2 KiB
Python
Raw Normal View History

2019-04-03 10:57:23 -04:00
from ConfigParser import ConfigParser
2019-07-12 00:57:53 -04:00
from PyQt4.QtCore import QString
2019-04-03 10:57:23 -04:00
def read_ini(file, section, value, default=""):
2019-07-12 00:57:53 -04:00
if isinstance(file, str) or isinstance(file, QString):
2019-04-03 10:57:23 -04:00
conf = ConfigParser()
2019-07-12 00:57:53 -04:00
conf.read(str(file))
2019-04-03 10:57:23 -04:00
else:
conf = file
values = conf.sections()
for val in values:
if val.lower() == section.lower():
for c in conf.options(val):
if c.lower() == value.lower():
return conf.get(val, c)
return default
def read_ini_bool(file, section, value, default=False):
2019-07-12 00:57:53 -04:00
if isinstance(file, str) or isinstance(file, QString):
2019-04-03 10:57:23 -04:00
conf = ConfigParser()
2019-07-12 00:57:53 -04:00
conf.read(str(file))
2019-04-03 10:57:23 -04:00
else:
conf = file
values = conf.sections()
for val in values:
if val.lower() == section.lower():
for c in conf.options(val):
if c.lower() == value.lower():
return conf.getboolean(val, c)
return default
def read_ini_int(file, section, value, default=0):
2019-07-12 00:57:53 -04:00
if isinstance(file, str) or isinstance(file, QString):
2019-04-03 10:57:23 -04:00
conf = ConfigParser()
2019-07-12 00:57:53 -04:00
conf.read(str(file))
2019-04-03 10:57:23 -04:00
else:
conf = file
values = conf.sections()
for val in values:
if val.lower() == section.lower():
for c in conf.options(val):
if c.lower() == value.lower():
return conf.getint(val, c)
return default