Skip to main content

Python publipost connector

This connector is meant to get used with the petit_publipost_gateway

It's a connector which can used in order to create a new publipost engine

As of now, it works using an HTTP interface, handled by an express server.

It implements the required endpoints needed to work with the petit_publipost_gateway

  • POST /publipost
  • POST /get_placeholders
  • GET /list
  • DELETE /remove_template
  • POST /load_templates
  • GET /live
  • POST /configure

It is used by :

It exposes 2 things:

  • make_connector
  • Template

Example:

# ...
import io
import re
from typing import Dict, List, Optional

import pdfkit
from jinja2 import Template as JinjaTemplate
from petit_python_publipost_connector import Template as BaseTemplate, make_connector

local_funcs: List[str] = []


def extract_variable(var: str):
    """Extracts variable and removes some stuff
    """
    # remove the '(' and ')'
    # in the case values in {{data + "test"}}
    # we want to get the 'data' part
    r = var.split('+')
    r = [
        i
        .replace('(', "")
        .replace(')', "")
        .strip()
        for i in r if '"' not in i
    ]
    return r


def get_placeholder(text: str, local_funcs: List[str]) -> List[str]:
    for name in local_funcs:
        text = text.replace(name, '')
    # finding between {{ }}
    res: List[str] = re.findall(
        r"\{{(.*?)\}}", text, re.MULTILINE
    )
    # finding between {% %}
    res2 = []
    for i in res:
        res2.extend(extract_variable(i.strip()))
    return res2


class BytesIO(io.BytesIO):
    @staticmethod
    def of(content: bytes):
        f = io.BytesIO()
        f.write(content)
        return f


class Template(BaseTemplate):

    def __init__(self, _file: io.BytesIO):
        self.fields: List[str] = list()
        self.content = _file.getvalue().decode('utf-8')
        self.template = JinjaTemplate(self.content)
        self.__load_fields()

    def __load_fields(self):
        self.fields = fields = get_placeholder(self.content, local_funcs)
        return fields

    def __apply_template(self, data: Dict[str, str]) -> str:
        """
        Applies the data to the template and returns a `Template`
        """
        return self.template.render(data)

    def render(self, data: Dict[str, object], options: Optional[List[str]]) -> io.BytesIO:
        rendered = self.__apply_template(data)
        # if need pdf conversion
        # if options is not None and 'pdf' in options:
        if True:
            # always true for now
            rendered = pdfkit.from_string(rendered, output_path=False)
        return BytesIO.of(rendered)



app = make_connector(Template)

Metadata

Release files for petit-python-publipost-connector 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for petit-python-publipost-connector 0.2.0
File Size Uploaded Provenance Actions
petit_python_publipost_connector-0.2.0.tar.gz 8.8 kB Without provenance Details for petit_python_publipost_connector-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for petit-python-publipost-connector 0.2.0
File Interpreter ABI Platform Actions
petit_python_publipost_connector-0.2.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for petit_python_publipost_connector-0.2.0-py2.py3-none-any.whl

Total release size: 19.7 kB

Release files / petit_python_publipost_connector-0.2.0.tar.gz

Download URL petit_python_publipost_connector-0.2.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f9cf7138edfbf7e96126290081857d50db0ce4f430cbb27d891f5dd65eec9b37
BLAKE2b-256 checksum
How to use checksums
6e612f6012aeee35f2a9d8d574b4dac3ead1b0b511d343a3c560526ca50e89fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.6

Release files / petit_python_publipost_connector-0.2.0-py2.py3-none-any.whl

Download URL petit_python_publipost_connector-0.2.0-py2.py3-none-any.whl
Size 11.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d99c424c26a8f0920da7d1e24ca5f95ab56c07d36b94fc14d38b10c3eaaef70d
BLAKE2b-256 checksum
How to use checksums
2835a27ba795ca3fde72bf824892d96c4c4ba150327b8bcaaf8747bca7f6b439
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.6

Security for petit-python-publipost-connector 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page