config.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import toml
  2. from dataclasses import dataclass
  3. from pathlib import Path
  4. @dataclass
  5. class Configuration:
  6. buffer_size: int = 128
  7. config_dir = Path(Path.home() / ".config" / "lyrebird")
  8. config_path = Path(config_dir / "config.toml")
  9. presets_path = Path(config_dir / "presets.toml")
  10. presets_old_path = Path(config_dir / "presets.toml.old")
  11. def load_config():
  12. '''
  13. Loads the config file located at ~/.config/lyrebird/config.toml, parses it
  14. and returns a `Configuration` object.
  15. '''
  16. create_config()
  17. config = {}
  18. path = config_path
  19. with open(path, 'r') as f:
  20. config = toml.loads(f.read())['config'][0]
  21. return Configuration(buffer_size=int(config['buffer_size']))
  22. def create_config_dir():
  23. config_dir.mkdir(parents=True, exist_ok=True)
  24. CONFIG_CONTENTS = '''
  25. # Configuration file for Lyrebird
  26. # The following parameters are configurable
  27. # buffer_size = The buffer size to use for sox. Higher = better quality, at
  28. # the cost of higher latency. Default value is 128
  29. [[config]]
  30. buffer_size = 128
  31. '''
  32. def create_config():
  33. create_config_dir()
  34. if not config_path.exists():
  35. with open(config_path, 'w') as f:
  36. f.write(CONFIG_CONTENTS)