From a82be5eabc93e0dc677f31554362f9f9acc50647 Mon Sep 17 00:00:00 2001 From: Jelmer van der Linde Date: Fri, 11 Nov 2022 18:14:48 +0100 Subject: [PATCH] Lazy loading for opusfilter --- filters/opusfilter/opusfilter-ersatz.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/filters/opusfilter/opusfilter-ersatz.py b/filters/opusfilter/opusfilter-ersatz.py index 4da8650..45c3751 100755 --- a/filters/opusfilter/opusfilter-ersatz.py +++ b/filters/opusfilter/opusfilter-ersatz.py @@ -3,15 +3,29 @@ import os # Prevent searching for modules in the filters/ directory (like langid) -sys.path.remove(os.getcwd()) +del sys.path[0] import importlib +import importlib.util import yaml import itertools import argparse import logging import warnings +def lazy_import(name): + spec = importlib.util.find_spec(name) + if not spec: + raise ImportError(name) + loader = importlib.util.LazyLoader(spec.loader) + spec.loader = loader + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + loader.exec_module(module) + return module + +opusfilter = lazy_import('opusfilter') + parser = argparse.ArgumentParser() parser.add_argument('--quiet', '-q', action='store_true') parser.add_argument('filter', type=str) @@ -28,9 +42,6 @@ # BeautifulSoup (the HtmlTagFilter) warnings.filterwarnings('ignore', module='bs4') -# Delayed loading opusfilter to install warning filter -import opusfilter - module_path, class_name = args.filter.rsplit('.', maxsplit=1) config = yaml.safe_load(args.config)