kopia lustrzana https://github.com/deathbeds/ipydrawio
update pypdf api
rodzic
a77c45e558
commit
10ded09e14
|
|
@ -14,6 +14,8 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import os
|
||||
|
||||
from ipydrawio.constants import IPYNB_METADATA
|
||||
|
||||
# the header to look for in PNG metadata
|
||||
|
|
@ -38,3 +40,7 @@ __all__ = [
|
|||
"ENV_IPYDRAWIO_DATA_DIR",
|
||||
"WORK_DIR",
|
||||
]
|
||||
|
||||
PUPPETEER_CACHE_DIR_ENV_VAR = "IPYDRAWIO_PUPPETEER_CACHE_DIR"
|
||||
PUPPETEER_CACHE_DIR = os.environ.get(PUPPETEER_CACHE_DIR_ENV_VAR)
|
||||
PUPPETEER_INSTALL = "install.js"
|
||||
|
|
|
|||
|
|
@ -44,6 +44,8 @@ from .constants import (
|
|||
ENV_IPYDRAWIO_DATA_DIR,
|
||||
ENV_JUPYTER_DATA_DIR,
|
||||
PNG_DRAWIO_INFO,
|
||||
PUPPETEER_CACHE_DIR,
|
||||
PUPPETEER_INSTALL,
|
||||
WORK_DIR,
|
||||
)
|
||||
|
||||
|
|
@ -133,11 +135,11 @@ class IPyDrawioExportManager(LoggingConfigurable):
|
|||
output_pdf.write_bytes(b64decode(pdf_text))
|
||||
final_pdf = tdp / "final.pdf"
|
||||
final = PdfWriter()
|
||||
final.appendPagesFromReader(PdfReader(str(output_pdf), "rb"))
|
||||
final.append_pages_from_reader(PdfReader(str(output_pdf), "rb"))
|
||||
xml = pdf_request["xml"]
|
||||
if hasattr(xml, "encode"):
|
||||
xml = xml.encode("utf-8")
|
||||
final.addAttachment(self.attachment_name, xml)
|
||||
final.add_attachment(self.attachment_name, xml)
|
||||
with final_pdf.open("wb") as fpt:
|
||||
final.write(fpt)
|
||||
|
||||
|
|
@ -169,9 +171,11 @@ class IPyDrawioExportManager(LoggingConfigurable):
|
|||
merger.write(str(output_pdf))
|
||||
composite_xml = E.tostring(tree).decode("utf-8")
|
||||
final = PdfWriter()
|
||||
final.appendPagesFromReader(PdfReader(str(output_pdf), "rb"))
|
||||
final.append_pages_from_reader(PdfReader(str(output_pdf), "rb"))
|
||||
if self.attach_xml:
|
||||
final.addAttachment(self.attachment_name, composite_xml.encode("utf-8"))
|
||||
final.add_attachment(
|
||||
self.attachment_name, composite_xml.encode("utf-8")
|
||||
)
|
||||
with final_pdf.open("wb") as fpt:
|
||||
final.write(fpt)
|
||||
return b64encode(final_pdf.read_bytes()).decode("utf-8")
|
||||
|
|
@ -337,6 +341,18 @@ class IPyDrawioExportManager(LoggingConfigurable):
|
|||
def drawio_export_node_modules(self):
|
||||
return self.drawio_export_app / "node_modules"
|
||||
|
||||
@property
|
||||
def drawio_export_chromium(self):
|
||||
return (
|
||||
self.drawio_export_app / ".chromium"
|
||||
if PUPPETEER_CACHE_DIR is None
|
||||
else Path(PUPPETEER_CACHE_DIR)
|
||||
)
|
||||
|
||||
@property
|
||||
def drawio_export_puppeteer(self):
|
||||
return self.drawio_export_node_modules / "puppeteer"
|
||||
|
||||
@property
|
||||
def drawio_export_integrity(self):
|
||||
return self.drawio_export_node_modules / ".yarn-integrity"
|
||||
|
|
@ -344,6 +360,8 @@ class IPyDrawioExportManager(LoggingConfigurable):
|
|||
@run_on_executor
|
||||
def provision(self, force=False): # pragma: no cover
|
||||
self.is_provisioning = True
|
||||
env = dict(**os.environ)
|
||||
env["PUPPETEER_CACHE_DIR"] = str(self.drawio_export_chromium)
|
||||
if not self.drawio_export_app.exists():
|
||||
if not self.drawio_export_app.parent.exists():
|
||||
self.drawio_export_app.parent.mkdir(parents=True)
|
||||
|
|
@ -364,8 +382,20 @@ class IPyDrawioExportManager(LoggingConfigurable):
|
|||
self.drawio_export_app,
|
||||
)
|
||||
subprocess.check_call(
|
||||
[str(JLPM), "--silent"],
|
||||
[str(JLPM), "--silent", "--ignore-scripts", "--no-optional"],
|
||||
cwd=str(self.drawio_export_app),
|
||||
env=env,
|
||||
)
|
||||
|
||||
if not self.drawio_export_chromium.exists() or force:
|
||||
self.log.info(
|
||||
"[ipydrawio-export] installing puppeteer %s",
|
||||
self.drawio_export_chromium,
|
||||
)
|
||||
subprocess.check_call(
|
||||
[str(NODE), PUPPETEER_INSTALL],
|
||||
cwd=str(self.drawio_export_puppeteer),
|
||||
env=env,
|
||||
)
|
||||
self.is_provisioning = False
|
||||
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue