Skip to content

Commit 673d60e

Browse files
added methods for gettting commands, tests, ... from extension registry
1 parent ab2b74a commit 673d60e

5 files changed

Lines changed: 38 additions & 10 deletions

File tree

sifter/commands/require.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CommandRequire(Command):
2020
def evaluate(self, message: Message, state: EvaluationState) -> Optional[Actions]:
2121
ext_name_list = self.positional_args[0]
2222
for ext_name in ext_name_list: # type: ignore
23-
if not ExtensionRegistry.get('extension', ext_name):
23+
if not ExtensionRegistry.has_extension(ext_name):
2424
raise RuntimeError(
2525
"Required extension '%s' not supported"
2626
% ext_name

sifter/comparator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_match_fn(
3131
match_type = 'IS'
3232

3333
# TODO: support wildcard matching in comparator names (RFC 4790)
34-
cmp_handler = ExtensionRegistry.get('comparator', comparator)
34+
cmp_handler = ExtensionRegistry.get_comparator(comparator)
3535
if not cmp_handler:
3636
raise RuntimeError("Comparator not supported: %s" % comparator)
3737

sifter/extensions/__init__.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
)
1010
import pkg_resources
1111

12+
13+
from sifter.grammar.comparator import Comparator
14+
from sifter.grammar.rule import Rule
15+
from sifter.grammar.command import Command
16+
from sifter.grammar.test import Test
1217
if TYPE_CHECKING:
13-
from sifter.grammar.rule import Rule
14-
from sifter.grammar.comparator import Comparator
18+
from sifter.grammar.tag import Tag
1519

1620

1721
class ExtensionRegistry():
@@ -39,6 +43,33 @@ def register_extension(cls, extension_name: Text) -> None:
3943
def register_handler(cls, ext_cls: Union[Type['Comparator'], Type['Rule']]) -> None:
4044
cls.register(ext_cls.handler_type(), ext_cls.handler_id(), ext_cls)
4145

46+
@classmethod
47+
def get_comparator(cls, comparator: Union[Text, 'Tag']) -> Type['Comparator']:
48+
handler = cls.get('comparator', comparator)
49+
if not isinstance(handler, type) or not issubclass(handler, Comparator):
50+
raise ValueError('Wrong Comparator Type!')
51+
return handler
52+
53+
@classmethod
54+
def get_command(cls, commandname: Text) -> Type['Command']:
55+
handler = cls.get('command', commandname)
56+
if not isinstance(handler, type) or not issubclass(handler, Command):
57+
raise ValueError('Wrong Command Type!')
58+
return handler
59+
60+
@classmethod
61+
def get_test(cls, testname: Text) -> Type['Test']:
62+
handler = cls.get('test', testname)
63+
if not isinstance(handler, type) or not issubclass(handler, Test):
64+
raise ValueError('Wrong Test Type!')
65+
return handler
66+
67+
@classmethod
68+
def has_extension(cls, ext_name: Text) -> bool:
69+
if cls.get('extension', ext_name):
70+
return True
71+
return False
72+
4273
@classmethod
4374
def register(
4475
cls,

sifter/grammar/grammar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def p_command(self, p: 'YaccProduction') -> None:
7979
block = None
8080
if p[3] != ';':
8181
block = p[3]
82-
handler = ExtensionRegistry.get('command', p[1])
82+
handler = ExtensionRegistry.get_command(p[1])
8383
if handler is None:
8484
print("No handler registered for command '%s' on line %d" % (p[1], p.lineno(1)))
8585
raise SyntaxError
@@ -136,7 +136,7 @@ def p_test(self, p: 'YaccProduction') -> None:
136136
"""test : IDENTIFIER arguments"""
137137
# print("TEST:", p[1], p[2])
138138
tests = p[2].get('tests')
139-
handler = ExtensionRegistry.get('test', p[1])
139+
handler = ExtensionRegistry.get_test(p[1])
140140
if handler is None:
141141
print("No handler registered for test '%s' on line %d" % (p[1], p.lineno(1)))
142142
raise SyntaxError

sifter/validators/tag.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ def validate(
9797
"'%s' comparator is unknown/unsupported"
9898
% arg_list[starting_index + 1]
9999
)
100-
if not ExtensionRegistry.get(
101-
'comparator',
102-
val[0],
103-
):
100+
if not ExtensionRegistry.get_comparator(val[0]):
104101
raise RuleSyntaxError(
105102
"'%s' comparator is unknown/unsupported"
106103
% val[0]

0 commit comments

Comments
 (0)