From 2df7dd0cc04add00d135046661ad835bcc2c0dd0 Mon Sep 17 00:00:00 2001 From: Mateusz Szwed Date: Wed, 19 Jun 2024 10:42:26 +0200 Subject: [PATCH] code: move categoriesConfig to separate module --- songbook/src/flask/flask_app.py | 2 +- songbook/src/songbookTeXmaker.py | 24 ++--------------------- songbook/src/tools/getCategoriesConfig.py | 24 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 songbook/src/tools/getCategoriesConfig.py diff --git a/songbook/src/flask/flask_app.py b/songbook/src/flask/flask_app.py index 827f6b2..de91dab 100644 --- a/songbook/src/flask/flask_app.py +++ b/songbook/src/flask/flask_app.py @@ -3,7 +3,7 @@ from ..obj.Config import config from ..obj.Songbook import Songbook from ..obj.Song import Song -from ..songbookTeXmaker import getCategoriesConfig +from ..tools.getCategoriesConfig import getCategoriesConfig import json import os import markdown2 diff --git a/songbook/src/songbookTeXmaker.py b/songbook/src/songbookTeXmaker.py index 3354692..dfbf884 100644 --- a/songbook/src/songbookTeXmaker.py +++ b/songbook/src/songbookTeXmaker.py @@ -1,16 +1,15 @@ import os -import json import re from ..src.tools.plAlphabetSort import plSortKey import asyncio import aiofiles -import emoji from . import headerconfig as headerconfig from .tools.chordShift import shiftChords -from .tools.codings import enUTF8, deUTF8 +from .tools.codings import enUTF8 +from .tools.getCategoriesConfig import getCategoriesConfig from .obj.Config import config from .obj.Song import Song @@ -18,12 +17,9 @@ from .tools.loggerSetup import logging - - logger = logging.getLogger(__name__) - def isSongCategoryDir(dirname): return os.path.isdir(os.path.join(config.dataFolder, dirname)) and not (dirname.startswith((".", "_"))) @@ -186,22 +182,6 @@ def makeSongbookDict(songs): return songbookDict -def remove_emojis(s): - return ''.join(c for c in s if c not in emoji.EMOJI_DATA) - -async def getCategoriesConfig(categoryDictFile, songbookDict, allowEmojis = False): - try: - async with aiofiles.open(categoryDictFile, "rb") as configFile: - configFileText = deUTF8(await configFile.read()) - if not allowEmojis: - configFileText = remove_emojis(configFileText) - cats_dict = json.loads(configFileText) - - except FileNotFoundError: - logger.warning("Category titles mapping file not found") - cats_dict = {cat: cat for cat in sorted( - songbookDict.keys(), key=plSortKey)} - return cats_dict async def processCategory(cat, songbookFile, ignoredSongs=None): diff --git a/songbook/src/tools/getCategoriesConfig.py b/songbook/src/tools/getCategoriesConfig.py new file mode 100644 index 0000000..540dd02 --- /dev/null +++ b/songbook/src/tools/getCategoriesConfig.py @@ -0,0 +1,24 @@ +import emoji +import aiofiles +import json + +from .codings import deUTF8 +from .loggerSetup import logging + +logger = logging.getLogger(__name__) +def remove_emojis(s): + return ''.join(c for c in s if c not in emoji.EMOJI_DATA) + +async def getCategoriesConfig(categoryDictFile, songbookDict, allowEmojis = False): + try: + async with aiofiles.open(categoryDictFile, "rb") as configFile: + configFileText = deUTF8(await configFile.read()) + if not allowEmojis: + configFileText = remove_emojis(configFileText) + cats_dict = json.loads(configFileText) + + except FileNotFoundError: + logger.warning("Category titles mapping file not found") + cats_dict = {cat: cat for cat in sorted( + songbookDict.keys())} + return cats_dict