Bug 1592558, add support for extension parsers
This allows us to implement support for niche formats without bothering about dependencies and m-c vendoring etc. I'm looking at you, .lang. Differential Revision: https://phabricator.services.mozilla.com/D51092
This commit is contained in:
Родитель
80d52e763d
Коммит
a1e377e1e6
|
@ -54,6 +54,14 @@ def getParser(path):
|
|||
for item in __constructors:
|
||||
if re.search(item[0], path):
|
||||
return item[1]
|
||||
try:
|
||||
from pkg_resources import iter_entry_points
|
||||
for entry_point in iter_entry_points('compare_locales.parsers'):
|
||||
p = entry_point.resolve()()
|
||||
if p.use(path):
|
||||
return p
|
||||
except (ImportError, IOError):
|
||||
pass
|
||||
raise UserWarning("Cannot find Parser")
|
||||
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
from __future__ import absolute_import
|
||||
import pkg_resources
|
||||
import shutil
|
||||
import tempfile
|
||||
import textwrap
|
||||
|
@ -87,3 +88,31 @@ class TestUniversalNewlines(unittest.TestCase):
|
|||
self.assertEqual(
|
||||
self.parser.ctx.contents,
|
||||
'one\ntwo\nthree\n')
|
||||
|
||||
|
||||
class TestPlugins(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.old_working_set_state = pkg_resources.working_set.__getstate__()
|
||||
distribution = pkg_resources.Distribution(__file__)
|
||||
entry_point = pkg_resources.EntryPoint.parse(
|
||||
'test_parser = compare_locales.tests.test_parser:DummyParser',
|
||||
dist=distribution
|
||||
)
|
||||
distribution._ep_map = {
|
||||
'compare_locales.parsers': {
|
||||
'test_parser': entry_point
|
||||
}
|
||||
}
|
||||
pkg_resources.working_set.add(distribution)
|
||||
|
||||
def tearDown(self):
|
||||
pkg_resources.working_set.__setstate__(self.old_working_set_state)
|
||||
|
||||
def test_dummy_parser(self):
|
||||
p = parser.getParser('some/weird/file.ext')
|
||||
self.assertIsInstance(p, DummyParser)
|
||||
|
||||
|
||||
class DummyParser(parser.Parser):
|
||||
def use(self, path):
|
||||
return path.endswith('weird/file.ext')
|
||||
|
|
Загрузка…
Ссылка в новой задаче