2020-12-29 17:16:16 +01:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
from dataclasses_json import dataclass_json, DataClassJsonMixin
|
|
|
|
|
|
|
|
config_path = Path("~/.config/paperlibrary.yaml").expanduser()
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass_json
|
|
|
|
@dataclass
|
|
|
|
class Config(DataClassJsonMixin):
|
|
|
|
url: str
|
|
|
|
auth_token: str
|
|
|
|
basedir: str
|
|
|
|
|
|
|
|
@property
|
|
|
|
def basedir_path(self):
|
|
|
|
return Path(self.basedir)
|
|
|
|
|
|
|
|
|
|
|
|
def get_config() -> Optional[Config]:
|
|
|
|
try:
|
|
|
|
with config_path.open() as f:
|
|
|
|
data = yaml.safe_load(f)
|
|
|
|
if not data:
|
|
|
|
raise ValueError("config file is empty")
|
|
|
|
return Config.from_dict(data)
|
|
|
|
except FileNotFoundError:
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2021-04-02 13:17:12 +02:00
|
|
|
def config_check(config: Config):
|
|
|
|
if not config:
|
|
|
|
print('please run "pap init" first to create a config file')
|
|
|
|
exit()
|
|
|
|
|
2020-12-29 17:16:16 +01:00
|
|
|
def save_config(config: Config) -> None:
|
|
|
|
with config_path.open("w") as f:
|
|
|
|
yaml.safe_dump(config.to_dict(), f)
|