From 693c9d37e03b2bea9e550f4b97b3f2215dae1468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 26 Jul 2022 20:02:57 +0200 Subject: [PATCH] use python configuration file now --- scripts/config_loader.py | 30 +++++++++++++ scripts/courses.py | 2 +- scripts/{config.py => default_config.py} | 5 ++- scripts/exercises.py | 2 +- scripts/notes.py | 2 +- scripts/parse_config.py | 55 ------------------------ scripts/parse_counters.py | 2 +- scripts/rofi-exercises.py | 1 - scripts/rofi-lectures.py | 2 +- scripts/utils.py | 2 +- scripts/window_subprocess.py | 6 +-- 11 files changed, 43 insertions(+), 66 deletions(-) create mode 100644 scripts/config_loader.py rename scripts/{config.py => default_config.py} (94%) delete mode 100644 scripts/parse_config.py diff --git a/scripts/config_loader.py b/scripts/config_loader.py new file mode 100644 index 0000000..4129f2e --- /dev/null +++ b/scripts/config_loader.py @@ -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 diff --git a/scripts/courses.py b/scripts/courses.py index f607cd3..0e7f5b2 100644 --- a/scripts/courses.py +++ b/scripts/courses.py @@ -4,7 +4,7 @@ import warnings import yaml 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 from notes import Notes from links import Links diff --git a/scripts/config.py b/scripts/default_config.py similarity index 94% rename from scripts/config.py rename to scripts/default_config.py index 3ee37d6..eea1f19 100644 --- a/scripts/config.py +++ b/scripts/default_config.py @@ -1,5 +1,6 @@ from pathlib import Path import pytz + # 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 # 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_ROOT = CURRENT_COURSE_SYMLINK.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' LOCALE = "de_DE.utf8" COURSE_IGNORE_FILE = '.courseignore' @@ -26,6 +27,8 @@ FALLBACK_COURSE_INFO_FILE = Path(__file__).parent.resolve() / 'fallback.yaml' TIMEZONE = pytz.timezone('CET') SCHEDULER_DELAY = 60 DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION = '.cnt' +TERMINAL = 'i3-sensible-terminal' +EDITOR = 'vim' NEW_EXERCISE_SHEET_HEADER = '\n'.join([ r"%! TEX root = ./*.tex", diff --git a/scripts/exercises.py b/scripts/exercises.py index db3dc7a..1d069d4 100644 --- a/scripts/exercises.py +++ b/scripts/exercises.py @@ -2,7 +2,7 @@ from file_list import Files, FileHandle, FileType from pathlib import Path from typing import Dict from utils import normalize -from config import NEW_EXERCISE_SHEET_HEADER +from config_loader import NEW_EXERCISE_SHEET_HEADER class ExerciseWriteUp(FileHandle): diff --git a/scripts/notes.py b/scripts/notes.py index abc02a1..4c472fc 100644 --- a/scripts/notes.py +++ b/scripts/notes.py @@ -4,7 +4,7 @@ import subprocess from pathlib import Path 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 from window_subprocess import edit from lectures import Lectures, number2filename diff --git a/scripts/parse_config.py b/scripts/parse_config.py deleted file mode 100644 index c3a383d..0000000 --- a/scripts/parse_config.py +++ /dev/null @@ -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' \ No newline at end of file diff --git a/scripts/parse_counters.py b/scripts/parse_counters.py index 6dcf009..c3b0cfe 100755 --- a/scripts/parse_counters.py +++ b/scripts/parse_counters.py @@ -2,7 +2,7 @@ import re from pathlib import Path 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: diff --git a/scripts/rofi-exercises.py b/scripts/rofi-exercises.py index 06dc5f9..446429c 100755 --- a/scripts/rofi-exercises.py +++ b/scripts/rofi-exercises.py @@ -2,7 +2,6 @@ from courses import Courses from exercises import Exercises from rofi import rofi -from config import MAX_LEN import sys diff --git a/scripts/rofi-lectures.py b/scripts/rofi-lectures.py index 3f4bbad..ca5330d 100755 --- a/scripts/rofi-lectures.py +++ b/scripts/rofi-lectures.py @@ -2,7 +2,7 @@ from courses import Courses from rofi import rofi from utils import generate_short_title -from config import MAX_LEN +from config_loader import MAX_LEN notes = Courses().current.notes lectures = notes.lectures diff --git a/scripts/utils.py b/scripts/utils.py index 29ac6d8..c363622 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -3,7 +3,7 @@ from datetime import datetime from typing import Dict import warnings -from config import MAX_LEN +from config_loader import MAX_LEN def beautify(string): diff --git a/scripts/window_subprocess.py b/scripts/window_subprocess.py index 44a184c..12e748b 100644 --- a/scripts/window_subprocess.py +++ b/scripts/window_subprocess.py @@ -4,16 +4,16 @@ import subprocess from pathlib import Path 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'): if not rootpath: rootpath = filepath.root subprocess.Popen([ - terminal(), + TERMINAL, "-e", - editor(), + EDITOR, f"--servername {servername}", f"--remote-silent", f"{str(filepath)}"