Commit a2996a35 authored by Jenda's avatar Jenda

confreader: sane defaults

parent 7d91ff0b
import configparser
from ClientStructures import Mode
from libutil import cfg_safe
class ConfReader():
""" Read config from file in path """
""" Read Kukuruku gui config from file in path """
def __init__(self, path):
rc = configparser.ConfigParser()
rc.read(path)
section = "Main"
self.HOST = rc.get(section, 'host')
self.PORT = rc.getint(section, 'port')
self.mousechangedelay = rc.getint(section, 'mousechangedelay')
self.histooffs = rc.getint(section, 'histooffs')
self.histow = rc.getint(section, 'histow')
self.histobars = rc.getint(section, 'histobars')
self.drawingheight = rc.getint(section, 'drawingheight')
self.borderleft = rc.getint(section, 'borderleft')
self.areabottom = rc.getint(section, 'areabottom')
self.fontsize = rc.getint(section, 'fontsize')
self.antialias = rc.getboolean(section, 'antialias')
self.spectrumscale = rc.getint(section, 'spectrumscale')
self.spectrumoffset = rc.getint(section, 'spectrumoffset')
self.sqltrim = rc.getfloat(section, 'sqltrim')
self.sqldelta = rc.getfloat(section, 'sqldelta')
self.afcdecim = rc.getfloat(section, 'afcdecim')
self.afcmult = rc.getfloat(section, 'afcmult')
self.modepath = rc.get(section, 'modepath')
self.preferformat = rc.get(section, 'preferformat')
cfpath = path
self.HOST = cfg_safe(rc.get, section, 'host', "localhost", cfpath)
self.PORT = cfg_safe(rc.getint, section, 'port', 4444, cfpath)
self.mousechangedelay = cfg_safe(rc.getint, section, 'mousechangedelay', 1, cfpath)
self.histooffs = cfg_safe(rc.getint, section, 'histooffs', 1, cfpath)
self.histow = cfg_safe(rc.getint, section, 'histow', 50, cfpath)
self.histobars = cfg_safe(rc.getint, section, 'histobars', 256, cfpath)
self.drawingheight = cfg_safe(rc.getint, section, 'drawingheight', 540, cfpath)
self.borderleft = cfg_safe(rc.getint, section, 'borderleft', 80, cfpath)
self.areabottom = cfg_safe(rc.getint, section, 'areabottom', 120, cfpath)
self.fontsize = cfg_safe(rc.getint, section, 'fontsize', 12, cfpath)
self.antialias = cfg_safe(rc.getboolean, section, 'antialias', False, cfpath)
self.spectrumscale = cfg_safe(rc.getint, section, 'spectrumscale', 50, cfpath)
self.spectrumoffset = cfg_safe(rc.getint, section, 'spectrumoffset', 6, cfpath)
self.sqltrim = cfg_safe(rc.getfloat, section, 'sqltrim', 0.3, cfpath)
self.sqldelta = cfg_safe(rc.getfloat, section, 'sqldelta', 10, cfpath)
self.afcdecim = cfg_safe(rc.getfloat, section, 'afcdecim', 5, cfpath)
self.afcmult = cfg_safe(rc.getfloat, section, 'afcmult', 0.1, cfpath)
self.modepath = cfg_safe(rc.get, section, 'modepath', "./modes", cfpath)
self.preferformat = cfg_safe(rc.get, section, 'preferformat', "F32", cfpath)
self.fftw = None
def read_modes(path):
......
......@@ -17,7 +17,14 @@ def safe_cast(val, to_type, default=None):
return default
def engnum(s):
""" Convert string s containing number with postfix "k" or "M" to float """
""" Convert string s containing number with postfix k, K, M, m, G, g to float.
We regard "m" as Mega because we don't want to bother the user with pressing shift
and there is really no use for milli here.
"""
if isinstance(s, (int, long, float)):
return s
i = safe_cast(s, int)
if s[-1] == "k" or s[-1] == "K":
s = s[:-1]
......@@ -31,5 +38,27 @@ def engnum(s):
if i is not None:
i *= 1000*1000
if s[-1] == "g" or s[-1] == "G":
s = s[:-1]
i = safe_cast(s, float)
if i is not None:
i *= 1000*1000
return i
def cfg_safe(func, section, key, default, f = None):
""" Try reading key from section with configparser func.
If it is not there, return default, and if f is set, print warning with this
as a file name where it is missing.
The configparser has some default dictionary, but it somehow didn't work for
anything other than strings.
"""
try:
x = func(section, key)
except:
x = default
if f is not None:
print "Can't get configuration key %s from section %s in %s. Using default %s."%(key, section, f, default)
return x
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment