This commit is contained in:
68
src/http_convert/config.py
Normal file
68
src/http_convert/config.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import yaml
|
||||
|
||||
|
||||
class Config:
|
||||
DEFAULT_PATH = Path.home() / ".http-convert.yaml"
|
||||
|
||||
def __init__(self, path: Optional[Path] = None):
|
||||
self.path = path or self.DEFAULT_PATH
|
||||
self.data = self._load()
|
||||
|
||||
def _load(self) -> dict:
|
||||
if self.path.exists():
|
||||
try:
|
||||
with open(self.path, 'r') as f:
|
||||
return yaml.safe_load(f) or {}
|
||||
except Exception:
|
||||
return {}
|
||||
return {}
|
||||
|
||||
def save(self) -> None:
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(self.path, 'w') as f:
|
||||
yaml.dump(self.data, f)
|
||||
|
||||
def get(self, key: str, default=None):
|
||||
return self.data.get(key, default)
|
||||
|
||||
def set(self, key: str, value) -> None:
|
||||
self.data[key] = value
|
||||
self.save()
|
||||
|
||||
@property
|
||||
def default_format(self) -> str:
|
||||
return self.get("default_format", "curl")
|
||||
|
||||
@default_format.setter
|
||||
def default_format(self, value: str) -> None:
|
||||
self.set("default_format", value)
|
||||
|
||||
@property
|
||||
def syntax_highlighting(self) -> bool:
|
||||
return self.get("syntax_highlighting", True)
|
||||
|
||||
@syntax_highlighting.setter
|
||||
def syntax_highlighting(self, value: bool) -> None:
|
||||
self.set("syntax_highlighting", value)
|
||||
|
||||
@property
|
||||
def compact_output(self) -> bool:
|
||||
return self.get("compact_output", False)
|
||||
|
||||
@compact_output.setter
|
||||
def compact_output(self, value: bool) -> None:
|
||||
self.set("compact_output", value)
|
||||
|
||||
@property
|
||||
def theme(self) -> str:
|
||||
return self.get("theme", "monokai")
|
||||
|
||||
@theme.setter
|
||||
def theme(self, value: str) -> None:
|
||||
self.set("theme", value)
|
||||
|
||||
def reset(self) -> None:
|
||||
self.data = {}
|
||||
self.save()
|
||||
Reference in New Issue
Block a user