"""A file manager for PDF file."""
from __future__ import annotations
from os.path import join, dirname, exists
from pyramid.config import Configurator
from pyramid.request import Request
from chrysalio.helpers.literal import Literal
from ..lib.ciotype import CioType
from ..lib.ciopath import CioPath
from ..lib.warehouse import Warehouse
from ..lib.manager import Manager
from ..lib.i18n import _, translate
# =============================================================================
[docs]
def includeme(configurator: Configurator):
"""Function to include a PDF manager.
:type configurator: pyramid.config.Configurator
:param configurator:
Object used to do configuration declaration within the application.
"""
Manager.register(configurator, ManagerPdf)
# =============================================================================
[docs]
class ManagerPdf(Manager):
"""Class to manage a PDF file."""
ciotype = CioType('pdf')
label = _('Generic PDF handling')
viewings = ( # yapf: disable
{'name': 'pdf',
'label': _('Default'),
'template': 'ciowarehouse2:Templates/manager_pdf_view.pt',
'css': ('/ciowarehouse2/css/manager_pdf.css',)},)
# -------------------------------------------------------------------------
[docs]
def view(
self,
request: Request,
warehouse: Warehouse,
ciopath: CioPath,
ts_factory=None) -> str | None:
"""Return a string containing HTML to display the file.
See: :meth:`.lib.manager.Manager.view`
"""
abs_path = ciopath.absolute_path(warehouse.root)
if not abs_path or not exists(abs_path):
return None
viewer_manager = join(
dirname(__file__), '..', 'Static', 'Pdfjs', 'web',
'viewer_manager.html')
with open(viewer_manager, 'rb') as hdl:
body = hdl.read().decode('utf8')\
.replace('_CLOSE_', self._route_close(
request, ciopath.uid()))\
.replace("${_('Close')}", translate(
_('Close'), request=request))
return self._chameleon_render(
request, warehouse, ciopath, self.viewings[0], ts_factory or _,
{'content': Literal(body)})