Skip to content

Commit

Permalink
Make Sphinx engine's module loader check for local engine config
Browse files Browse the repository at this point in the history
The module loader will now use a local engine 'config.py' file in
its directory if it exists and has all of the attributes set.

Warnings are printed if there is a config.py file without the
engine config attributes set. The loader will fallback on the
default engine config.py module in that case.
  • Loading branch information
drmfinlay committed Sep 23, 2018
1 parent b1365a1 commit 55643f7
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions dragonfly/examples/sphinx_module_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import os.path
import logging

from dragonfly import RecognitionObserver
from dragonfly.engines.backend_sphinx.engine import SphinxEngine

from dragonfly import RecognitionObserver, EngineError
from dragonfly.engines import get_engine

# --------------------------------------------------------------------------
Expand Down Expand Up @@ -157,6 +159,24 @@ def main():
__file__ = os.path.join(path, "sphinx_module_loader.py")

engine = get_engine("sphinx")
assert isinstance(engine, SphinxEngine)

# Try to import the local engine configuration object first. If there isn't one,
# use the default engine configuration.
log = logging.getLogger("config")
try:
import config
engine.config = config
log.info("Using local engine configuration module 'config.py'")
except ImportError:
pass
except EngineError as e:
# Log EngineErrors caught when setting the configuration.
log.warning(e.message)
log.warning("Falling back to using the default engine configuration "
"instead of 'config.py'")

# Call connect() now that the engine configuration is set.
engine.connect()

# Register a recognition observer
Expand All @@ -170,7 +190,6 @@ def main():
# TODO Change script to import all modules before loading the grammars into Pocket Sphinx

# Start the engine's main recognition loop
engine.connect()
engine.recognise_forever()


Expand Down

0 comments on commit 55643f7

Please sign in to comment.