def __init__(self, filename=None, lines=None): if filename is not None: self._filename = os.path.expanduser(filename) if os.path.exists(self._filename): self.load(load_file(self._filename)) if lines is not None: self.load(lines=lines)
def load(self, lines=None): """Parses given lines into the config""" if not lines: return if type(lines) is not list: lines = lines.split("\n") assert (type(lines) is list) possible_continuation = False last_option = None for line in lines: if len(line.strip()) == 0: last_option = None possible_continuation = False continue # We can continue only if this line starts with space and # the prior line ended in a continuation character (\ or ,) if possible_continuation and not line[0].isspace(): possible_continuation = False last_option = None line = line.split('#')[0].strip() # Check for possible continuation to the next line if line.endswith('\\'): possible_continuation = True line = line[:-1].strip() elif line.endswith(','): possible_continuation = True # Add the option to ourself if '=' in line: option, value = line.split('=', 1) option = option.strip() if option: last_option = option self.set(option, value.strip()) # Line continues from previous, just append to prior value elif possible_continuation and last_option: old_value = self.get(last_option) if type(old_value) is list: old_value.extend(to_list(line)) else: old_value += " " + line self.set(last_option, old_value) # Import another config file elif line[:8] == 'include ': filename = line[8:].strip() lines = load_file(filename) self.load(lines) possible_continuation = False last_option = None
def load(self, lines=None): """Parses given lines into the config""" if not lines: return if type(lines) is not list: lines = lines.split("\n") assert(type(lines) is list) possible_continuation = False last_option = None for line in lines: if len(line.strip()) == 0: last_option = None possible_continuation = False continue # We can continue only if this line starts with space and # the prior line ended in a continuation character (\ or ,) if possible_continuation and not line[0].isspace(): possible_continuation = False last_option = None line = line.split('#')[0].strip() # Check for possible continuation to the next line if line.endswith('\\'): possible_continuation = True line = line[:-1].strip() elif line.endswith(','): possible_continuation = True # Add the option to ourself if '=' in line: option, value = line.split('=', 1) option = option.strip() if option: last_option = option self.set(option, value.strip()) # Line continues from previous, just append to prior value elif possible_continuation and last_option: old_value = self.get(last_option) if type(old_value) is list: old_value.extend(to_list(line)) else: old_value += " " + line self.set(last_option, old_value) # Import another config file elif line[:8] == 'include ': filename = line[8:].strip() lines = load_file(filename) self.load(lines) possible_continuation = False last_option = None
def read(self): lines = load_file(self.filename) if not lines: return None json_data = jsonpickle.decode('\n'.join(lines)) return json_data