BlinkenArea - GitList
Repositories
Blog
Wiki
pyetherpix
Code
Commits
Branches
Tags
Search
Tree:
bc491f5
Branches
Tags
master
pyetherpix
pyetherpix
display.py
begin of config file parsing
Stefan Schuermans
commited
bc491f5
at 2017-05-25 22:18:34
display.py
Blame
History
Raw
import msg import parse class Display(object): def __init__(self, config_file, msg_obj=None): """create a new EtherPix display config_file: name of config file to read msg_obj: message callback object or None""" self._msg = msg_obj if self._msg is None: # use default message callback if none given self._msg = msg.MsgDef() # default settings self._bind_addr = ("0.0.0.0", 0) # local network address to bind to self._size = (0, 0) # size of display # read config file if not self._proc_config_file(config_file): raise Exception("error(s) while reading config file") def _proc_config_file(self, config_file): """process config file config_file: name of config file to read returns True on success, False on error""" self._msg.msg(msg.Msg.INFO, "using config file \"%s\"" % config_file) # process all lines in config file okay = True try: with open(config_file, "r") as cfile: lineno = 1 for line in cfile: if not self._proc_config_line(line, lineno): okay = False lineno += 1 except (IOError, OSError) as e: self._msg.msg(msg.Msg.ERR, str(e)) okay = False return okay def _proc_config_line(self, line, lineno): """process line from config file line: line read from config file lineno: line number returns True on success, False on error""" # parse config file line "setting = value # comment" line_no_comment = line.split("#", 1)[0].strip() if line_no_comment == "": return True # ignore empty lines else: fields = line_no_comment.split("=", 1) if len(fields) < 2: self._msg.msg(msg.Msg.WARN, "invalid line %u in config file, ignored" % lineno) return True else: setting = fields[0].strip() value = fields[1].strip() return self._proc_config_setting(setting, value, lineno) def _proc_config_setting(self, setting, value, lineno): """process setting from config file setting: name of setting value: value of setting returns True on success, False on error""" # replace all whitespace sequences in setting with single spaces setting = " ".join(setting.split()) # process setting if setting == "bindAddr": addr = parse._parse_addr(value) if addr is None: self._msg.msg(msg.Msg.ERR, "invalid address \"%s\" for \"bindAddr\"" " in line %u in config file" % (value, lineno)) return False else: self._bind_addr = addr self._msg.msg(msg.Msg.INFO, "bind address \"%s:%u\"" % (addr[0], addr[1])) return True elif setting == "size": size = parse._parse_two_nos(value) if size is None: self._msg.msg(msg.Msg.ERR, "invalid address \"%s\" for \"size\"" " in line %u in config file" % (value, lineno)) return False else: self._size = size return True elif setting.startswith("distributor "): return True # TODO elif setting.startswith("distributorAddr "): return True # TODO elif setting.startswith("mapping "): return True # TODO elif setting.startswith("output "): return True # TODO else: self._msg.msg(msg.Msg.WARN, "unknown setting \"%s\" in line %u in config file," " ignored" % (setting, lineno)) return True