This repository was archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcodec_plugins.py
More file actions
87 lines (73 loc) · 2.62 KB
/
codec_plugins.py
File metadata and controls
87 lines (73 loc) · 2.62 KB
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
import pkg_resources
import collections
import coreapi
import os
def sorting_func(package_info):
"""
A sorting order for (package, codec_class) tuples. Example ordering:
application/coreapi+json (highest priority)
application/openapi+json (lower as not a coreapi package built-in)
application/json (lower as more general subtype)
text/* (lower as sub type is wildcard)
*/* (lowest as main type is wildcard)
"""
package, codec_class = package_info
media_type = getattr(codec_class, 'media_type')
main_type, _, sub_type = media_type.partition('/')
sub_type = sub_type.split(';')[0]
is_builtin = package.dist.project_name == 'coreapi'
return (
main_type == '*',
sub_type == '*',
'+' not in sub_type,
not is_builtin,
media_type
)
def instantiate_codec(cls):
if issubclass(cls, coreapi.codecs.DownloadCodec):
default_dir = os.path.join(os.path.expanduser('~'), '.coreapi')
config_dir = os.environ.get('COREAPI_CONFIG_DIR', default_dir)
download_dir = os.path.join(config_dir, 'downloads')
if not os.path.exists(config_dir):
os.mkdir(config_dir)
if not os.path.exists(download_dir):
os.mkdir(download_dir)
return cls(download_dir=download_dir)
return cls()
def get_codec_packages():
"""
Returns a list of (package, codec_class) tuples.
"""
packages = [
(package, package.load()) for package in
pkg_resources.iter_entry_points(group='coreapi.codecs')
]
packages = [
(package, instantiate_codec(cls)) for (package, cls) in packages
if issubclass(cls, coreapi.codecs.BaseCodec) or hasattr(cls, 'decode') or hasattr(cls, 'encode')
]
return sorted(packages, key=sorting_func)
def supports(codec):
"""
Return a list of strings indicating supported operations.
"""
if hasattr(codec, 'encode') and hasattr(codec, 'decode'):
return ['encoding', 'decoding']
elif hasattr(codec, 'encode'):
return ['encoding']
elif hasattr(codec, 'decode'):
return ['decoding']
# Fallback for pre-2.0 API.
return codec.supports
codec_packages = get_codec_packages()
codecs = collections.OrderedDict([
(package.name, codec) for (package, codec) in codec_packages
])
decoders = collections.OrderedDict([
(package.name, codec) for (package, codec) in codec_packages
if 'decoding' in supports(codec)
])
encoders = collections.OrderedDict([
(package.name, codec) for (package, codec) in codec_packages
if 'encoding' in supports(codec)
])