Skip to content

Commit

Permalink
Split registry metadata from class definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
erik committed Dec 28, 2018
1 parent 243ee67 commit 21894e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
9 changes: 3 additions & 6 deletions squabble/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,14 @@ def show_rule(name):
}

try:
rule = rule.Registry.get_meta(name)
meta = rule.Registry.get_meta(name)
except squabble.UnknownRuleException:
print('{bold}Unknown rule:{reset} {name}'.format(**{
'name': name,
**color
}))
sys.exit(1)

meta = rule['meta']
print('{bold}{name}{reset} - {description}\n{help}'.format(**{
**meta,
**color
Expand All @@ -129,11 +128,9 @@ def list_rules():
'reset': Style.RESET_ALL,
}

all_rules = sorted(rule.Registry.all(), key=lambda r: r['meta']['name'])

for rule in all_rules:
meta = rule['meta']
all_rules = sorted(rule.Registry.all(), key=lambda r: r['name'])

for meta in all_rules:
print('{bold}{name: <32}{reset} {description}'.format(**{
**color,
**meta
Expand Down
4 changes: 1 addition & 3 deletions squabble/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def configure_rules(rule_config):
rules = []

for name, options in rule_config.items():
meta = Registry.get_meta(name)
cls = meta['class']

cls = Registry.get_class(name)
rules.append(cls(options))

return rules
Expand Down
31 changes: 22 additions & 9 deletions squabble/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def _load_builtin_rules():
modules = glob.glob(os.path.dirname(__file__) + '/rules/*.py')

for mod in modules:
print(mod)
mod_name = os.path.basename(mod)[:-3]

if not os.path.isfile(mod) or mod_name.startswith('__'):
Expand Down Expand Up @@ -114,28 +113,42 @@ def get_meta(name):
"""
Return metadata about a given rule in the registry.
If no rule exists in the registry named ``name``,
:class:`UnknownRuleException` will be thrown.
The returned dictionary will look something like this:
.. code-block:: python
{
'class': RuleClass,
'meta': {
'name': 'RuleClass',
'help': 'Some rule...',
# ...
}
'name': 'RuleClass',
'help': 'Some rule...',
# ...
}
"""
if name not in Registry._REGISTRY:
raise UnknownRuleException(name)

return Registry._REGISTRY[name]
return Registry._REGISTRY[name]['meta']

@staticmethod
def get_class(name):
"""
Return class for given rule name in the registry.
If no rule exists in the registry named ``name``,
:class:`UnknownRuleException` will be thrown.
"""
if name not in Registry._REGISTRY:
raise UnknownRuleException(name)

return Registry._REGISTRY[name]['class']

@staticmethod
def all():
"""
Return an iterator over all known rule metadata. Equivalent to calling
:func:`~Registry.get_meta()` for all registered rules.
"""
return Registry._REGISTRY.values()
for r in Registry._REGISTRY.values():
yield r['meta']

0 comments on commit 21894e9

Please sign in to comment.