| #-*- coding: utf-8 |
| import fileinput |
| import os |
| |
| |
| def parse_config_file(config_file, configs, encoding='utf-8'): |
| if not os.path.exists(config_file): |
| print u"Config file <{0}> not exists".format(config_file) |
| return False |
| if isinstance(config_file, str): |
| config_file = config_file.decode(encoding) |
| |
| lineno = 0 |
| for line in fileinput.input(config_file): |
| lineno += 1 |
| line = line.strip(' ').rstrip('\n').rstrip('\r') |
| line = line.decode(encoding) |
| if not line: |
| continue |
| if line.startswith('#'): |
| # is comment |
| continue |
| cfg = line.partition('=') |
| if not cfg[1]: |
| raise ValueError(u"Config file line %d error" % lineno) |
| configs[cfg[0].strip(' ')] = cfg[2].lstrip(' ') |
| return True |