2022-01-25 09:04:46 +00:00
|
|
|
"""Do further startup configuration and initialization"""
|
2022-01-25 08:22:13 +00:00
|
|
|
import os
|
|
|
|
import urllib
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.apps import AppConfig
|
|
|
|
|
|
|
|
from bookwyrm import settings
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-01-25 08:37:05 +00:00
|
|
|
|
2022-01-25 08:22:13 +00:00
|
|
|
def download_file(url, destination):
|
2022-01-25 09:04:46 +00:00
|
|
|
"""Downloads a file to the given path"""
|
2022-01-25 08:22:13 +00:00
|
|
|
try:
|
2022-02-02 05:45:13 +00:00
|
|
|
# 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())
|
2022-01-25 08:22:13 +00:00
|
|
|
except (urllib.error.HTTPError, urllib.error.URLError):
|
2022-03-11 22:31:04 +00:00
|
|
|
logger.info("Failed to download file %s", url)
|
2022-02-02 05:45:13 +00:00
|
|
|
except OSError:
|
2022-03-11 22:31:04 +00:00
|
|
|
logger.info("Couldn't open font file %s for writing", destination)
|
2022-02-02 05:54:51 +00:00
|
|
|
except: # pylint: disable=bare-except
|
2022-03-11 22:31:04 +00:00
|
|
|
logger.info("Unknown error in file download")
|
2022-01-25 08:22:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BookwyrmConfig(AppConfig):
|
2022-01-25 09:04:46 +00:00
|
|
|
"""Handles additional configuration"""
|
|
|
|
|
2022-01-25 08:22:13 +00:00
|
|
|
name = "bookwyrm"
|
|
|
|
verbose_name = "BookWyrm"
|
|
|
|
|
2022-02-05 02:50:57 +00:00
|
|
|
# pylint: disable=no-self-use
|
2022-01-25 08:22:13 +00:00
|
|
|
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()
|
|
|
|
|
2022-01-25 08:22:13 +00:00
|
|
|
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)
|