class ConfigWriter: def __init__(self): self._config_file = 'C:\\Program Files\\EqualizerAPO\\config\\config.txt' self._config_output = None self.headphone_mode_enabled = False self.active_headphone = None self.inactive_headphones = list() # Read current config. with open(self._config_file, 'r') as c: self._config_output = c.readlines() c.close() # Extract spec name from text output. for spec in self._config_output: if spec.startswith('# '): _spec = spec.replace('# ', '').removesuffix('\n').removesuffix('.txt').removeprefix('Include:') self.inactive_headphones.append(_spec.strip()) else: _spec = spec.removesuffix('\n').removesuffix('.txt').removeprefix('Include:') self.active_headphone = _spec.strip() if self.active_headphone is not None: self.headphone_mode_enabled = True def write_specs(self, disabled_specs, enabled_spec): formatted_disabled_specs = list() formatted_enabled_spec = self._format_spec(enabled_spec, False) if disabled_specs is not None: for s in disabled_specs: formatted_disabled_specs.append(self._format_spec(s, True)) with open(self._config_file, 'w+') as c: for i in formatted_disabled_specs: c.write(i + '\n') if enabled_spec is not None: c.write(formatted_enabled_spec) c.close() def _format_spec(self, entry: str, disable: bool): _entry = None if disable: _entry = '# Include: {}.txt'.format(entry) else: _entry = 'Include: {}.txt'.format(entry) return _entry