Skip to content

Commit

Permalink
Inkscape plugin: Get data from stdout
Browse files Browse the repository at this point in the history
Don't use a temp file for the out put of the boxes utiltty bur read it
from its stdout. Remove no longer used Inkscape related code.
  • Loading branch information
florianfesti committed Feb 23, 2024
1 parent e1c2259 commit a49ac20
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 30 deletions.
6 changes: 0 additions & 6 deletions boxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from boxes import gears
from boxes import parts
from boxes import pulley
from boxes import svgutil
from boxes.Color import *
from boxes.vectors import kerf

Expand Down Expand Up @@ -298,7 +297,6 @@ def __init__(self) -> None:
description += "\n\n" + self.description
self.argparser = ArgumentParser(description=description)
self.edgesettings: dict[Any, Any] = {}
self.inkscapefile = None
self.non_default_args: dict[Any, Any] = {}
self.translations = gettext.NullTranslations()

Expand Down Expand Up @@ -536,10 +534,6 @@ def parseArgs(self, args=None):
"""
if args is None:
args = sys.argv[1:]
if len(args) > 1 and args[-1][0] != "-":
self.inkscapefile = args[-1]
del args[-1]
args = [a for a in args if not a.startswith('--tab=')]

def cliquote(s):
s = s.replace('\r', '')
Expand Down
7 changes: 0 additions & 7 deletions scripts/boxes
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ def run_generator(name, args):
else:
msg = f'Unknown generator \'{name}\'. Use boxes --list to get a list of available commands.\n'
sys.stderr.write(msg)
if box.inkscapefile:
try:
out = sys.stdout.buffer
except AttributeError:
out= sys.stdout
svgutil.svgMerge(box.output, box.inkscapefile, out)


def generator_groups():
generators = generators_by_name()
Expand Down
25 changes: 8 additions & 17 deletions scripts/boxes_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import inkex
import sys
import os
import subprocess
from lxml import etree
import tempfile
from shlex import quote


Expand All @@ -33,8 +33,6 @@ def add_arguments(self, pars):
pars.add_argument(key, default=key)

def generate(self):
f, box_file = tempfile.mkstemp(".svg", "boxes.py-inkscape")

cmd = "boxes" # boxes.exe in this local dir (or if present in %PATH%), or boxes from $PATH in linux
for arg in vars(self.options):
if arg in (
Expand All @@ -46,30 +44,23 @@ def generate(self):
if arg == "original" and str(getattr(self.options, arg)) == "false":
continue
cmd += f" --{arg} {quote(str(getattr(self.options, arg)))}"
cmd += f" --output {box_file} {box_file}" # we need to add box_file string twice in a row. Otherwise program executable throws an error
cmd += f" --output -"
cmd = cmd.replace("boxes --generator", "boxes")

# run boxes with the parameters provided
with os.popen(cmd, "r") as boxes:
result = boxes.read()
result = subprocess.run(cmd.split(), capture_output=True)

# check output existence
try:
stream = open(box_file)
except FileNotFoundError as e:
inkex.utils.debug("There was no " + box_file + " output generated. Cannot continue. Command was:")
if result.returncode:
inkex.utils.debug("Generating box svg failed. Cannot continue. Command was:")
inkex.utils.debug(str(cmd))
inkex.utils.debug(str(result.stderr))
exit(1)

# write the generated SVG into Inkscape's canvas
p = etree.XMLParser(huge_tree=True)
doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True))
stream.close()
if os.path.exists(box_file):
os.remove(box_file) # remove previously generated box file at the end too

doc = etree.fromstring(result.stdout, parser=etree.XMLParser(huge_tree=True))
group = inkex.Group(id="boxes.py")
for element in doc.getroot():
for element in doc:
group.append(element)
return group

Expand Down

0 comments on commit a49ac20

Please sign in to comment.