Coverage for lino/modlib/printing/choicelists.py : 44%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: UTF-8 -*- # Copyright 2009-2016 Luc Saffre # License: BSD (see file COPYING for details)
Choicelists for `lino.modlib.printing`.
"""
except ImportError: from django.template.loader import TemplateDoesNotExist
"""Base class for all build methods. A build method encapsulates the process of generating a "printable document" that inserts data from the database into a template, using a given combination of a template parser and post-processor.
"""
"""Whether this build method results is an editable file. For example, `.odt` files are considered editable while `.pdf` files aren't.
In that case the target will be in a webdav folder and the print action will respond `open_davlink_url` instead of the usual `open_url`, which extjs3 ui will implement by calling `Lino.davlink_open()` instead of the usual `window.open()`.
When :mod:`lino.modlib.davlink` is not installed, this setting still influences the target path of resulting files, but the clients will not automatically recognize them as webdav-editable URLs.
"""
name, self.__class__.__name__, name, **kwargs)
"used by `get_target_name`" # assert self.name is not None return MediaFile( self.use_webdav, self.cache_name, self.value, elem.filename_root() + self.target_ext)
return self.get_target(action, elem).name
"""Theoretically it is possible to write build methods which override this.
""" if self.default_template: return self.default_template return 'Default' + self.template_ext # return dd.plugins.printing.get_default_template(self, obj)
return self.get_target(action, elem).url
raise NotImplementedError
""" Using Django's templating engine. """
tpls = action.get_print_templates(self, elem) if len(tpls) == 0: raise Warning("No templates defined for %r" % elem) tpls2 = [] for i in tpls: for g in elem.get_template_groups(): tpls2.append(g+'/'+i) #~ logger.debug('make_pisa_html %s',tpls) # prefix = '/'.join(elem.get_template_groups()) # if prefix: # prefix += '/' # tpls = [prefix + tpl for tpl in tpls] try: return select_template(tpls2) except TemplateDoesNotExist as e: raise Warning("No template found for %s (%s)" % (e, tpls2)) except Exception as e: raise Exception( "Error while loading template for %s : %s" % (tpls2, e))
# ,MEDIA_URL=settings.MEDIA_URL): context.update( instance=elem, title=str(elem), MEDIA_URL=settings.MEDIA_ROOT.replace('\\', '/') + '/', ) return tpl.render(context)
""" Generates .pdf files from .html templates. Requires `pisa <https://pypi.python.org/pypi/pisa>`_. Usage example see :doc:`/tutorials/pisa/index`. """ # name = 'pisa'
import ho.pisa as pisa # pisa.showLogging() tpl = self.get_template(action, elem) filename = action.before_build(self, elem) if filename is None: return #~ html = self.render_template(elem,tpl,request=ar.request) html = self.render_template(elem, tpl, ar=ar) html = html.encode("utf-8") file(filename + '.html', 'w').write(html)
result = io.BytesIO() h = logging.FileHandler(filename + '.log', 'w') pisa.log.addHandler(h) pdf = pisa.pisaDocument( io.BytesIO(html), result, encoding='utf-8') pisa.log.removeHandler(h) h.close() fd = file(filename, 'wb') fd.write(result.getvalue()) fd.close() if pdf.err: raise Exception("pisa.pisaDocument.err is %r" % pdf.err) return os.path.getmtime(filename)
"""Base for build methods which use Lino's templating system (:meth:`find_config_file <lino.core.site.Site.find_config_file>`).
TODO: check whether this extension to Django's templating system is still needed.
"""
tpls = action.get_print_templates(self, elem) if len(tpls) != 1: raise Exception( "%s.get_print_templates() must return " "exactly 1 template (got %r)" % ( elem.__class__.__name__, tpls)) tpl_leaf = tpls[0] if not tpl_leaf.endswith(self.template_ext): raise Warning( "Invalid template '%s' configured for %s '%s' " "(expected filename ending with '%s')." % (tpl_leaf, elem.__class__.__name__, str(elem), self.template_ext))
lang = elem.get_print_language() \ or settings.SITE.DEFAULT_LANGUAGE.django_code if lang != settings.SITE.DEFAULT_LANGUAGE.django_code: name = tpl_leaf[:-len(self.template_ext)] + \ "_" + lang + self.template_ext if rt.find_config_file( name, *elem.get_template_groups()): return name return tpl_leaf
tpl_leaf = self.get_template_leaf(action, elem) groups = elem.get_template_groups() tplfile = settings.SITE.find_config_file(tpl_leaf, *groups) if not tplfile: raise Warning("No file %s in %s" % (tpl_leaf, groups)) return tplfile
# if elem is None: # return target = action.before_build(self, elem) if not target: return tplfile = self.get_template_file(ar, action, elem) return self.simple_build(ar, elem, tplfile, target)
raise NotImplementedError
""" Generates `.pdf` files from `.tex` templates. Not actively used. """
# context = dict(instance=elem) raise NotImplementedError
""" Generates .rtf files from .rtf templates. Not actively used. """
context = dict(instance=elem) t = pyratemp.Template(filename=tpl) try: result = t(**context) except pyratemp.TemplateRenderError as e: raise Exception(u"%s in %s" % (e, tpl)) fd = file(target, 'wb') fd.write(result) fd.close() return os.path.getmtime(target)
""" The choicelist of build methods offered on this site. """ # app_label = 'lino'
def get_system_default(cls): """Return the default build method to be used when really no default build method has been defined anywhere, even not in :attr:`default_build_method <lino.core.site.Site.default_build_method>`.
""" sc = settings.SITE.site_config if sc.default_build_method: return sc.default_build_method if settings.SITE.default_build_method: return cls.get_by_value( settings.SITE.default_build_method) return cls.pisa # hard-coded default
|