1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
8 'application/mathml+xml': 'librarian.embeds.mathml.MathML',
9 'application/x-latex': 'librarian.embeds.latex.LaTeX',
15 def transforms_to(cls, mime_types, downgrade=False):
17 for name, method in cls.__dict__.iteritems():
18 if hasattr(method, "embed_converts_to"):
19 conv_type, conv_downgrade = method.embed_converts_to
20 if downgrade == conv_downgrade and conv_type in mime_types:
21 matches.add(conv_type)
24 def transform_to(self, mime_type, downgrade=False):
25 for name, method in type(self).__dict__.iteritems():
26 if hasattr(method, "embed_converts_to"):
27 conv_type, conv_downgrade = method.embed_converts_to
28 if downgrade == conv_downgrade and conv_type == mime_type:
32 class DataEmbed(Embed):
33 def __init__(self, data=None):
37 class TreeEmbed(Embed):
38 def __init__(self, tree=None):
39 if isinstance(tree, etree._Element):
40 tree = etree.ElementTree(tree)
44 def converts_to(mime_type, downgrade=False):
45 def decorator(method):
46 method.embed_converts_to = mime_type, downgrade
51 def downgrades_to(mime_type):
52 return converts_to(mime_type, True)
55 def create_embed(mime_type, tree=None, data=None):
56 embed = known_types.get(mime_type)
58 embed = DataEmbed if tree is None else TreeEmbed
60 mod_name, cls_name = embed.rsplit('.', 1)
61 mod = importlib.import_module(mod_name)
62 embed = getattr(mod, cls_name)
64 return embed(data if tree is None else tree)