51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
from core.services.apps.font_families import PREFERRED_FONT_FAMILIES
|
|
from core.models.orm_models.ApplicationVersion import ApplicationVersion
|
|
from core.models.session.SessionProfile import SessionProfile
|
|
from core.models.Result import Result
|
|
|
|
# generic
|
|
from pathlib import Path
|
|
import os
|
|
import random
|
|
import shutil
|
|
|
|
|
|
def _select_random_font(items: list, preferred_items: list, limit: int):
|
|
"""Select a font from the available choices"""
|
|
|
|
available_preferred_items = [item for item in preferred_items if item in items]
|
|
selectable_items = [item for item in items if item not in preferred_items]
|
|
|
|
selected_items = random.sample(selectable_items, random.randint(0, min(limit, len(selectable_items))))
|
|
|
|
return selected_items + available_preferred_items
|
|
|
|
|
|
def prepare_fonts(
|
|
profile: SessionProfile,
|
|
version: ApplicationVersion,
|
|
persistent_state_path: str
|
|
) -> Result:
|
|
"""Write the fonts for this app version to the bash script's init"""
|
|
|
|
font_path = f'{profile.get_data_path()}/fonts'
|
|
|
|
if not os.path.isdir(persistent_state_path) or len(os.listdir(persistent_state_path)) == 0:
|
|
shutil.copytree(f'{version.get_installation_path()}/resources/initial-state', persistent_state_path)
|
|
|
|
if (not os.path.isdir(font_path) or len(os.listdir(font_path)) == 0) and os.path.isdir('/usr/share/fonts'):
|
|
|
|
if os.path.isdir('/usr/share/fonts/truetype'):
|
|
truetype_fonts_path = '/usr/share/fonts/truetype'
|
|
else:
|
|
truetype_fonts_path = '/usr/share/fonts'
|
|
|
|
font_families = [file.name for file in Path(truetype_fonts_path).iterdir() if file.is_dir()]
|
|
|
|
font_family_subset = _select_random_font(font_families, PREFERRED_FONT_FAMILIES, 12)
|
|
|
|
for font_family in font_family_subset:
|
|
shutil.copytree(f'{truetype_fonts_path}/{font_family}', f'{font_path}/{font_family}')
|
|
|
|
return Result(valid=True)
|
|
|