| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| from contextlib import contextmanager |
| import logging |
|
|
| from ..errors import ImplementationNotAvailable |
| from ..plat import get_xslt_compile |
| from ..utils import hwp5_resources_path |
| from ..utils import mkstemp_open |
|
|
|
|
| logger = logging.getLogger(__name__) |
|
|
|
|
| class BaseTransform: |
|
|
| def __init__(self, xslt_compile=None, embedbin=False): |
| self.xslt_compile = xslt_compile or self.get_default_xslt_compile() |
| self.embedbin = embedbin |
|
|
| @classmethod |
| def get_default_xslt_compile(cls): |
| xslt_compile = get_xslt_compile() |
| if not xslt_compile: |
| raise ImplementationNotAvailable('xslt') |
| return xslt_compile |
|
|
| def make_transform_hwp5(self, transform_xhwp5): |
| def transform_hwp5(hwp5file, output): |
| with self.transformed_xhwp5_at_temp(hwp5file) as xhwp5path: |
| return transform_xhwp5(xhwp5path, output) |
| return transform_hwp5 |
|
|
| def make_xsl_transform(self, resource_path, **params): |
| with hwp5_resources_path(resource_path) as xsl_path: |
| return self.xslt_compile(xsl_path, **params) |
|
|
| @contextmanager |
| def transformed_xhwp5_at_temp(self, hwp5file): |
| with mkstemp_open() as (tmp_path, f): |
| hwp5file.xmlevents(embedbin=self.embedbin).dump(f) |
| yield tmp_path |
|
|