pivotalcli/config.py

33 lines
789 B
Python
Raw Normal View History

2018-04-03 10:56:07 +00:00
import configparser
import os
from typing import Dict
import appdirs
class _Config(type):
def __getitem__(cls, key: str) -> Dict[str, str]:
if key not in cls.config:
cls.config[key] = {}
return cls.config[key]
class Config(object, metaclass=_Config):
config = configparser.ConfigParser()
@staticmethod
def __get_filename() -> str:
2019-09-04 14:42:23 +00:00
data_dir = appdirs.user_data_dir("pivotalcli", "sijmen")
2018-04-03 10:56:07 +00:00
os.makedirs(data_dir, exist_ok=True)
2019-09-04 14:42:23 +00:00
return os.path.join(data_dir, "config.ini")
2018-04-03 10:56:07 +00:00
@classmethod
def read(cls) -> None:
cls.config.read(cls.__get_filename())
@classmethod
def write(cls) -> None:
2019-09-04 14:42:23 +00:00
with open(cls.__get_filename(), mode="w") as config_file:
2018-04-03 10:56:07 +00:00
cls.config.write(config_file)