Completed
Push — master ( a2eb28...52e99c )
by Satoru
03:28
created

list_processors_by_ext()   A

Complexity

Conditions 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 10
rs 9.2
1
#
2
# Copyright (C) 2018 Satoru SATOH <ssato @ redhat.com>
3
# License: MIT
4
#
5
r"""Abstract processor module.
6
7
.. versionadded:: 0.9.5
8
9
   - Add to abstract processors such like Parsers (loaders and dumpers). 
10
"""
11
from __future__ import absolute_import
12
13
import operator
14
import pkg_resources
15
16
import anyconfig.compat
17
18
19
class Processor(object):
20
    """
21
    Abstract processor class to provide basic implementation of some methods,
22
    interfaces and members.
23
24
    - _type: type indicates data types it can process
25
    - _priority: Priority to select it if there are others of same type
26
    - _extensions: File extensions of data type it can process
27
    """
28
    _type = None
29
    _priority = 0   # 0 (lowest priority) .. 99  (highest priority)
30
    _extensions = []
31
32
    @classmethod
33
    def type(cls):
34
        """Processors' type
35
        """
36
        return cls._type
37
38
    @classmethod
39
    def priority(cls):
40
        """Processors's priority
41
        """
42
        return cls._priority
43
44
    @classmethod
45
    def extensions(cls):
46
        """File extensions which this process can process
47
        """
48
        return cls._extensions
49
50
51
def _load_plugins_itr(pgroup, safe=True):
52
    """
53
    .. seealso:: the doc of :func:`load_plugins`
54
    """
55
    for res in pkg_resources.iter_entry_points(pgroup):
56
        try:
57
            yield res.load()
58
        except ImportError:
59
            if safe:
60
                continue
61
            raise
62
63
64
def load_plugins(pgroup, safe=True):
65
    """
66
    :param pgroup: A string represents plugin type, e.g. anyconfig_backends
67
    :param safe: Do not raise ImportError during load if True
68
    :raises: ImportError
69
    """
70
    return list(_load_plugins_itr(pgroup, safe=safe))
71
72
73
def list_processors_by_type(prs):
74
    """
75
    :param prs: A list of instances of :class:`Processor`
76
    :return: List (generator) of (processor_type, [processor_cls])
77
    """
78
    return ((t, sorted(ps, key=operator.methodcaller("priority"))) for t, ps
79
            in anyconfig.utils.groupby(prs, operator.methodcaller("type")))
80
81
82
def _ext_proc_tpls_to_procs(xps):
83
    """List processors by each priority.
84
85
    :param xps: A list of (file_extension, processor_cls)
86
    :return: List of [processor_cls]
87
    """
88
    return sorted((operator.itemgetter(1)(xp) for xp in xps),
89
                  key=operator.methodcaller("priority"))
90
91
92
def list_processors_by_ext(prs):
93
    """
94
    :param prs: A list of instances of :class:`Processor`
95
    :return: List (generator) of (file_extension, [processor_cls])
96
    """
97
    ps_by_ext = anyconfig.utils.concat(([(x, p) for x in p.extensions()] for p
98
                                        in prs))  # [(ext, proc_cls)]
99
100
    return ((x, _ext_proc_tpls_to_procs(xps)) for x, xps
101
            in anyconfig.utils.groupby(ps_by_ext, operator.itemgetter(0)))
102
103
104
def list_types(tps):
105
    """List types that any processors can process them are available.
106
107
    :param tps: A list (generator) of (processor_type, [processor_cls])
108
    :return: [processor_type]
109
    """
110
    return sorted(set(next(anyconfig.compat.izip(*tps))))
111
112
# vim:sw=4:ts=4:et:
113