File: registry.py

package info (click to toggle)
isbnlib 3.9.3-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 596 kB
  • sloc: python: 4,575; makefile: 4
file content (94 lines) | stat: -rw-r--r-- 2,357 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
"""Registry for metadata services, formatters and cache."""

from pkg_resources import iter_entry_points

from . import _goob as goob
from . import _openl as openl
from ._imcache import IMCache
from .dev._fmt import _fmtbib

# SERVICES

services = {
    'default': goob.query,
    'goob': goob.query,
    'openl': openl.query,
}


def setdefaultservice(name):  # pragma: no cover
    """Set the default service."""
    global services
    services['default'] = services[name]


def add_service(name, query):  # pragma: no cover
    """Add a new service to services."""
    global services
    services[name] = 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),
    'refworks': lambda x: _fmtbib('refworks', x),
    'msword': lambda x: _fmtbib('msword', x),
}  # pragma: no cover


def setdefaultbibformatter(name):  # pragma: no cover
    """Set the default formatter."""
    global bibformatters
    bibformatters['default'] = bibformatters[name]


def add_bibformatter(name, formatter):  # pragma: no cover
    """Add a new formatter to formatters."""
    global bibformatters
    bibformatters[name] = formatter


def load_plugins():  # pragma: no cover
    """Load plugins with groups: isbnlib.metadata & isbnlib.formatters."""
    # get metadata plugins from entry_points
    try:
        for entry in iter_entry_points(group='isbnlib.metadata'):
            add_service(entry.name, entry.load())
    except:
        pass
    # get formatters from entry_points
    try:
        for entry in iter_entry_points(group='isbnlib.formatters'):
            add_bibformatter(entry.name, entry.load())
    except:
        pass


# load plugins on import
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


custom_cache = None  # should be an instance


def set_custom_cache(cache):  # pragma: no cover
    """Set a 'spare' cache."""
    global custom_cache
    custom_cache = cache