2022-05-01 20:41:26 +02:00
|
|
|
import configparser
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Dict
|
|
|
|
|
|
|
|
# We read a configuration file for university setup that is located
|
|
|
|
# in $XDG_CONFIG_HOME/university-setup/config.cfg
|
|
|
|
# We follow standard configparser procedure
|
|
|
|
|
|
|
|
|
|
|
|
def get_config_file():
|
|
|
|
if 'XDG_CONFIG_HOME' in os.environ.keys():
|
|
|
|
xdg_config_home = Path(os.environ['XDG_CONFIG_HOME']).resolve()
|
|
|
|
else:
|
2022-05-01 21:02:44 +02:00
|
|
|
xdg_config_home = Path('~/.config').expanduser().resolve()
|
|
|
|
config_file = xdg_config_home / 'university-setup' / 'config.cfg'
|
|
|
|
if not config_file.exists():
|
|
|
|
config_file.parent.mkdir(exist_ok=True, parents=True)
|
|
|
|
config_file.write_text(
|
|
|
|
'# Automatically generated default config file for university-setup\n'
|
|
|
|
'# Edit this as you wish\n'
|
|
|
|
'\n'
|
|
|
|
'[Subprocess]\n'
|
2022-05-01 21:07:15 +02:00
|
|
|
'terminal = i3-sensible-terminal\n'
|
|
|
|
'editor = vim\n'
|
|
|
|
''
|
2022-05-01 21:02:44 +02:00
|
|
|
)
|
|
|
|
print(f'Initialized default config file at {str(config_file)}.')
|
|
|
|
print(config_file)
|
|
|
|
return config_file
|
2022-05-01 20:41:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_config(section_name: str = 'Subprocess') -> Dict:
|
|
|
|
parser = configparser.RawConfigParser()
|
|
|
|
parser.read(get_config_file())
|
|
|
|
if section_name in parser:
|
|
|
|
return dict(parser[section_name])
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
def terminal() -> str:
|
|
|
|
d = get_config('Subprocess')
|
|
|
|
if 'terminal' in d.keys():
|
|
|
|
return d['terminal']
|
|
|
|
else:
|
|
|
|
return 'i3-sensible-terminal'
|
2022-05-01 21:07:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def editor() -> str:
|
|
|
|
d = get_config('Subprocess')
|
|
|
|
if 'editor' in d.keys():
|
|
|
|
return d['editor']
|
|
|
|
else:
|
|
|
|
return 'vim'
|