-
Notifications
You must be signed in to change notification settings - Fork 29
/
registry.py
121 lines (95 loc) · 3.34 KB
/
registry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
"""Registry for metadata services, formatters and cache."""
import logging
from pkg_resources import iter_entry_points
from . import _goob as goob
from . import _openl as openl
from . import _wiki as wiki
from . import NotValidDefaultServiceError
from . import NotValidDefaultFormatterError
from ._imcache import IMCache
from .config import options
from .dev._fmt import _fmtbib
LOGGER = logging.getLogger(__name__)
# SERVICES
services = {
'default': goob.query,
'goob': goob.query,
'openl': openl.query,
'wiki': wiki.query,
}
PROVIDERS = ()
def setdefaultservice(name):
"""Set the default service."""
global services
services['default'] = services[name.lower()]
if name != 'default' and name in services:
services['default'] = services[name.lower()]
else:
LOGGER.critical('Wrong default service')
raise NotValidDefaultServiceError(name)
def add_service(name, query): # pragma: no cover
"""Add a new service to services."""
global services
services[name.lower()] = query
# FORMATTERS
bibformatters = {
'default': lambda x: _fmtbib('labels', x),
'labels': lambda x: _fmtbib('labels', x),
'bibtex': lambda x: _fmtbib('bibtex', x),
'csl': lambda x: _fmtbib('csl', x),
'json': lambda x: _fmtbib('json', x),
'opf': lambda x: _fmtbib('opf', x),
'endnote': lambda x: _fmtbib('endnote', x),
'ris': lambda x: _fmtbib('ris', x),
'refworks': lambda x: _fmtbib('ris', x),
'msword': lambda x: _fmtbib('msword', x),
} # pragma: no cover
BIBFORMATS = ()
def setdefaultbibformatter(name):
"""Set the default formatter."""
global bibformatters
if name != 'default' and name in bibformatters:
bibformatters['default'] = bibformatters[name.lower()]
else:
LOGGER.critical('Wrong default bibformatter')
raise NotValidDefaultFormatterError(name)
def add_bibformatter(name, formatter): # pragma: no cover
"""Add a new formatter to formatters."""
global bibformatters
bibformatters[name] = formatter.lower()
# pylint: disable=broad-except
def load_plugins(): # pragma: no cover
"""Load plugins with groups: isbnlib.metadata & isbnlib.formatters."""
# get metadata plugins from entry_points
if options.get('LOAD_METADATA_PLUGINS', True):
try:
for entry in iter_entry_points(group='isbnlib.metadata'):
add_service(entry.name, entry.load())
except Exception:
LOGGER.critical('Some metadata plugins were not loaded!')
global PROVIDERS
_buf = list(services.keys())
_buf.remove('default')
PROVIDERS = tuple(sorted(_buf))
# get formatters from entry_points
if options.get('LOAD_FORMATTER_PLUGINS', True):
try:
for entry in iter_entry_points(group='isbnlib.formatters'):
add_bibformatter(entry.name, entry.load())
except Exception:
LOGGER.critical('Some formatters plugins were not loaded!')
global BIBFORMATS
_buf = list(bibformatters.keys())
_buf.remove('labels')
_buf.remove('default')
BIBFORMATS = tuple(sorted(_buf))
# load plugins on import
load_plugins()
del load_plugins
# CACHE
metadata_cache = IMCache() # should be an instance
def set_cache(cache): # pragma: no cover
"""Set cache for metadata."""
global metadata_cache
metadata_cache = cache