Skip to content

Commit

Permalink
code: move categoriesConfig to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
matiusz committed Jun 19, 2024
1 parent 6db2114 commit 2df7dd0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion songbook/src/flask/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 2 additions & 22 deletions songbook/src/songbookTeXmaker.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
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

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((".", "_")))

Expand Down Expand Up @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions songbook/src/tools/getCategoriesConfig.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2df7dd0

Please sign in to comment.