Refactor get_formatters_grouped

This commit is contained in:
Jakub Roztocil 2019-09-01 11:45:47 +02:00
parent 4dffac7a25
commit bd3208cf24
2 changed files with 7 additions and 6 deletions

View File

@ -89,6 +89,7 @@ class ConverterPlugin(BasePlugin):
class FormatterPlugin(BasePlugin): class FormatterPlugin(BasePlugin):
group_name = 'format'
def __init__(self, **kwargs): def __init__(self, **kwargs):
""" """

View File

@ -1,4 +1,5 @@
from itertools import groupby from itertools import groupby
from operator import attrgetter
from typing import Dict, List, Type from typing import Dict, List, Type
from pkg_resources import iter_entry_points from pkg_resources import iter_entry_points
@ -51,12 +52,11 @@ class PluginManager(list):
return self.filter(FormatterPlugin) return self.filter(FormatterPlugin)
def get_formatters_grouped(self) -> Dict[str, List[Type[FormatterPlugin]]]: def get_formatters_grouped(self) -> Dict[str, List[Type[FormatterPlugin]]]:
groups = {} return {
for group_name, group in groupby( group_name: list(group)
self.get_formatters(), for group_name, group
key=lambda p: getattr(p, 'group_name', 'format')): in groupby(self.get_formatters(), key=attrgetter('group_name'))
groups[group_name] = list(group) }
return groups
def get_converters(self) -> List[Type[ConverterPlugin]]: def get_converters(self) -> List[Type[ConverterPlugin]]:
return self.filter(ConverterPlugin) return self.filter(ConverterPlugin)