Skip to content

Commit

Permalink
Tweak extractor API
Browse files Browse the repository at this point in the history
Only allow classes as extractors, and pass a config-dictionary to them. This allows for per-extension configuration.
  • Loading branch information
wichert committed Aug 14, 2014
1 parent 62c6fa4 commit a53055f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ in ``setup.py``::
"""
...)

Note - the registered extractor may be a class or an instance of
an ``Extractor`` deriving object.
Note - the registered extractor must be a class derived from the ``Extractor``
base class.

After installing ``mypackage`` the custom extractor gets executed.

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def run_tests(self):
[console_scripts]
polint = lingua.polint:main
pot-create = lingua.extract:main
[lingua.extractors]
python = lingua.extractors.python:PythonExtractor
xml = lingua.extractors.xml:XMLExtractor
Expand Down
4 changes: 3 additions & 1 deletion src/lingua/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ def read_config(filename):
file=sys.stderr)
sys.exit(1)
try:
EXTENSIONS[extension] = EXTRACTORS[plugin]
config = dict(config.items(section))
config.pop('plugin')
EXTENSIONS[extension] = EXTRACTORS[plugin](config)
except KeyError:
print('Unknown plugin %s. Check --list-plugins for available options' % plugin,
file=sys.stderr)
Expand Down
9 changes: 4 additions & 5 deletions src/lingua/extractors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pkg_resources import working_set
import abc
import collections
import inspect
import os
import re
import sys
Expand Down Expand Up @@ -115,6 +114,8 @@ def update_keywords(keywords, specs):

@add_metaclass(abc.ABCMeta)
class Extractor(object):
def __init__(self, config=None):
self.config = config if config else {}

@abc.abstractproperty
def extensions(self):
Expand All @@ -128,11 +129,9 @@ def __call__(self, filename, options):
def register_extractors():
for entry_point in working_set.iter_entry_points('lingua.extractors'):
extractor = entry_point.load(require=True)
if inspect.isclass(extractor):
extractor = extractor()
if not isinstance(extractor, Extractor):
if not issubclass(extractor, Extractor):
raise ValueError(
u'Registered extractor must derive from ``Extractor``')
EXTRACTORS[entry_point.name] = extractor
for extension in extractor.extensions:
EXTENSIONS[extension] = extractor
EXTENSIONS[extension] = extractor()
5 changes: 1 addition & 4 deletions src/lingua/extractors/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ class XMLExtractor(Extractor, ElementProgram):
extensions = ['.pt', '.zpt']
DEFAULT_NAMESPACES = MacroProgram.DEFAULT_NAMESPACES

def __init__(self):
pass

def __call__(self, filename, options):
self.options = options
self.filename = filename
Expand All @@ -86,7 +83,7 @@ def __call__(self, filename, options):
print('Aborting due to parse error in %s: %s' %
(self.filename, e), file=sys.stderr)
sys.exit(1)
super(XMLExtractor, self).__init__(source, filename=filename)
ElementProgram.__init__(self, source, filename=filename)
return self.messages

def visit(self, kind, args):
Expand Down

0 comments on commit a53055f

Please sign in to comment.