use python configuration file now

This commit is contained in:
Maximilian Keßler 2022-07-26 20:02:57 +02:00
parent 231b57df91
commit 693c9d37e0
11 changed files with 43 additions and 66 deletions

30
scripts/config_loader.py Normal file
View File

@ -0,0 +1,30 @@
import shutil
from pathlib import Path
import importlib.util
import os
import sys
# 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_dir() -> str:
if 'XDG_CONFIG_HOME' in os.environ.keys():
xdg_config_home = Path(os.environ['XDG_CONFIG_HOME']).resolve()
else:
xdg_config_home = Path('~/.config').expanduser().resolve()
config_file = xdg_config_home / 'university-setup' / 'config.py'
if not config_file.exists():
config_file.parent.mkdir(exist_ok=True, parents=True)
shutil.copy(Path(__file__).with_name('default_config.py'), config_file)
print(f'Initialized default config file at {str(config_file)}.')
return str(config_file.parent.absolute())
sys.path.append(get_config_dir())
# Note that IDEs will probably complain about this, since they cannot find the module right now
# This is because the module is in fact user-supplied
from config import *
# future: potentially check that config in fact defines all symbols that we need

View File

@ -4,7 +4,7 @@ import warnings
import yaml import yaml
from typing import List from typing import List
from config import ROOT, CURRENT_COURSE_ROOT, CURRENT_COURSE_SYMLINK, CURRENT_COURSE_WATCH_FILE, COURSE_IGNORE_FILE, \ from config_loader import ROOT, CURRENT_COURSE_ROOT, CURRENT_COURSE_SYMLINK, CURRENT_COURSE_WATCH_FILE, COURSE_IGNORE_FILE, \
COURSE_INFO_FILE_NAME, FALLBACK_COURSE_INFO_FILE COURSE_INFO_FILE_NAME, FALLBACK_COURSE_INFO_FILE
from notes import Notes from notes import Notes
from links import Links from links import Links

View File

@ -1,5 +1,6 @@
from pathlib import Path from pathlib import Path
import pytz import pytz
# default is 'primary', if you are using a separate calendar for your course schedule, # default is 'primary', if you are using a separate calendar for your course schedule,
# your calendarId (which you can find by going to your Google Calendar settings, selecting # your calendarId (which you can find by going to your Google Calendar settings, selecting
# the relevant calendar and scrolling down to Calendar ID) probably looks like # the relevant calendar and scrolling down to Calendar ID) probably looks like
@ -10,7 +11,7 @@ USERCALENDARID = 'primary'
CURRENT_COURSE_SYMLINK = Path('~/current_course').expanduser() CURRENT_COURSE_SYMLINK = Path('~/current_course').expanduser()
CURRENT_COURSE_ROOT = CURRENT_COURSE_SYMLINK.resolve() CURRENT_COURSE_ROOT = CURRENT_COURSE_SYMLINK.resolve()
CURRENT_COURSE_WATCH_FILE = Path('/tmp/current_course').resolve() CURRENT_COURSE_WATCH_FILE = Path('/tmp/current_course').resolve()
ROOT = Path('~/Uni/semester-6').expanduser() ROOT = Path('~/uni/semester-6').expanduser()
DATE_FORMAT = '%a %d %b %Y' DATE_FORMAT = '%a %d %b %Y'
LOCALE = "de_DE.utf8" LOCALE = "de_DE.utf8"
COURSE_IGNORE_FILE = '.courseignore' COURSE_IGNORE_FILE = '.courseignore'
@ -26,6 +27,8 @@ FALLBACK_COURSE_INFO_FILE = Path(__file__).parent.resolve() / 'fallback.yaml'
TIMEZONE = pytz.timezone('CET') TIMEZONE = pytz.timezone('CET')
SCHEDULER_DELAY = 60 SCHEDULER_DELAY = 60
DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION = '.cnt' DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION = '.cnt'
TERMINAL = 'i3-sensible-terminal'
EDITOR = 'vim'
NEW_EXERCISE_SHEET_HEADER = '\n'.join([ NEW_EXERCISE_SHEET_HEADER = '\n'.join([
r"%! TEX root = ./*.tex", r"%! TEX root = ./*.tex",

View File

@ -2,7 +2,7 @@ from file_list import Files, FileHandle, FileType
from pathlib import Path from pathlib import Path
from typing import Dict from typing import Dict
from utils import normalize from utils import normalize
from config import NEW_EXERCISE_SHEET_HEADER from config_loader import NEW_EXERCISE_SHEET_HEADER
class ExerciseWriteUp(FileHandle): class ExerciseWriteUp(FileHandle):

View File

@ -4,7 +4,7 @@ import subprocess
from pathlib import Path from pathlib import Path
from typing import Dict from typing import Dict
from config import LECTURE_START_MARKER, LECTURE_END_MARKER, DEFAULT_IMPORT_INDENTATION, \ from config_loader import LECTURE_START_MARKER, LECTURE_END_MARKER, DEFAULT_IMPORT_INDENTATION, \
DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION
from window_subprocess import edit from window_subprocess import edit
from lectures import Lectures, number2filename from lectures import Lectures, number2filename

View File

@ -1,55 +0,0 @@
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:
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'
'terminal = i3-sensible-terminal\n'
'editor = vim\n'
''
)
print(f'Initialized default config file at {str(config_file)}.')
print(config_file)
return config_file
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'
def editor() -> str:
d = get_config('Subprocess')
if 'editor' in d.keys():
return d['editor']
else:
return 'vim'

View File

@ -2,7 +2,7 @@
import re import re
from pathlib import Path from pathlib import Path
from typing import Dict from typing import Dict
from config import DEFAULT_IMPORT_INDENTATION from config_loader import DEFAULT_IMPORT_INDENTATION
def parse_counters(filepath: Path, break_point: Dict) -> Dict: def parse_counters(filepath: Path, break_point: Dict) -> Dict:

View File

@ -2,7 +2,6 @@
from courses import Courses from courses import Courses
from exercises import Exercises from exercises import Exercises
from rofi import rofi from rofi import rofi
from config import MAX_LEN
import sys import sys

View File

@ -2,7 +2,7 @@
from courses import Courses from courses import Courses
from rofi import rofi from rofi import rofi
from utils import generate_short_title from utils import generate_short_title
from config import MAX_LEN from config_loader import MAX_LEN
notes = Courses().current.notes notes = Courses().current.notes
lectures = notes.lectures lectures = notes.lectures

View File

@ -3,7 +3,7 @@ from datetime import datetime
from typing import Dict from typing import Dict
import warnings import warnings
from config import MAX_LEN from config_loader import MAX_LEN
def beautify(string): def beautify(string):

View File

@ -4,16 +4,16 @@ import subprocess
from pathlib import Path from pathlib import Path
import os import os
from parse_config import terminal, editor from config_loader import TERMINAL, EDITOR
def edit(filepath: Path, rootpath: Path = None, env=os.environ, servername='tex lecture'): def edit(filepath: Path, rootpath: Path = None, env=os.environ, servername='tex lecture'):
if not rootpath: if not rootpath:
rootpath = filepath.root rootpath = filepath.root
subprocess.Popen([ subprocess.Popen([
terminal(), TERMINAL,
"-e", "-e",
editor(), EDITOR,
f"--servername {servername}", f"--servername {servername}",
f"--remote-silent", f"--remote-silent",
f"{str(filepath)}" f"{str(filepath)}"