moviewyrm/bookwyrm/apps.py

55 lines
1.8 KiB
Python
Raw Normal View History

2022-01-25 09:04:46 +00:00
"""Do further startup configuration and initialization"""
import os
import urllib
import logging
from django.apps import AppConfig
from bookwyrm import settings
logger = logging.getLogger(__name__)
def download_file(url, destination):
2022-01-25 09:04:46 +00:00
"""Downloads a file to the given path"""
try:
# Ensure our destination directory exists
os.makedirs(os.path.dirname(destination))
2022-01-25 09:04:46 +00:00
with urllib.request.urlopen(url) as stream:
with open(destination, "b+w") as outfile:
outfile.write(stream.read())
except (urllib.error.HTTPError, urllib.error.URLError):
logger.info("Failed to download file %s", url)
except OSError:
logger.info("Couldn't open font file %s for writing", destination)
except: # pylint: disable=bare-except
logger.info("Unknown error in file download")
class BookwyrmConfig(AppConfig):
2022-01-25 09:04:46 +00:00
"""Handles additional configuration"""
name = "bookwyrm"
verbose_name = "BookWyrm"
2022-02-05 02:50:57 +00:00
# pylint: disable=no-self-use
def ready(self):
2022-02-05 02:50:57 +00:00
"""set up OTLP and preview image files, if desired"""
2022-02-05 02:34:17 +00:00
if settings.OTEL_EXPORTER_OTLP_ENDPOINT:
2022-02-05 02:50:57 +00:00
# pylint: disable=import-outside-toplevel
2022-02-05 02:34:17 +00:00
from bookwyrm.telemetry import open_telemetry
open_telemetry.instrumentDjango()
if settings.ENABLE_PREVIEW_IMAGES and settings.FONTS:
# Download any fonts that we don't have yet
logger.debug("Downloading fonts..")
for name, config in settings.FONTS.items():
font_path = os.path.join(
settings.FONT_DIR, config["directory"], config["filename"]
)
if "url" in config and not os.path.exists(font_path):
logger.info("Just a sec, downloading %s", name)
download_file(config["url"], font_path)