Skip to content

Commit

Permalink
Lazy loading for opusfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmervdl committed Nov 11, 2022
1 parent 379dca6 commit a82be5e
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions filters/opusfilter/opusfilter-ersatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit a82be5e

Please sign in to comment.