Connect your templates to the petit_publipost gateway
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file petit_python_publipost_connector-0.2.0.tar.gz
.
File metadata
- Download URL: petit_python_publipost_connector-0.2.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9cf7138edfbf7e96126290081857d50db0ce4f430cbb27d891f5dd65eec9b37 |
|
MD5 | 7d76a22770644eb71b135e457210e928 |
|
BLAKE2b-256 | 6e612f6012aeee35f2a9d8d574b4dac3ead1b0b511d343a3c560526ca50e89fa |
File details
Details for the file petit_python_publipost_connector-0.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: petit_python_publipost_connector-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d99c424c26a8f0920da7d1e24ca5f95ab56c07d36b94fc14d38b10c3eaaef70d |
|
MD5 | e2d6ea4a83a01f1d31ddca03abd6ee78 |
|
BLAKE2b-256 | 2835a27ba795ca3fde72bf824892d96c4c4ba150327b8bcaaf8747bca7f6b439 |