Skip to content

CODECS with Temporary files using tempfile.mkdtemp() enabling multiple processing. #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/cq_cli/cqcodecs/codec_helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
from contextlib import contextmanager, redirect_stderr, redirect_stdout
from os import devnull

import tempfile
import shutil
import logging

@contextmanager
def suppress_stdout_stderr():
"""A context manager that redirects stdout and stderr to devnull"""
logging.info("STDOUT and STDERR are send to devnull.")
with open(devnull, "w") as fnull:
with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out:
yield (err, out)


class temp_dir():
def __init__(self):
"""
A function to create temp codec files inside temporary directories, and
enable multiple codec processing, preventing file writing racing conditions.
Returns
-------
str, Temporary directory path.
"""
self.path = tempfile.mkdtemp()
logging.debug(f"temp_dir: {self.path}")

def dlt(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is dlt called anywhere? Maybe you want __del__ instead so it's called when the temp_dir is deconstructed?

"""
Safe temp_dir deletion and path invalidation.
Returns
-------
None.

"""
if self.path:
shutil.rmtree(self.path)
self.path = None
logging.debug("temp_dir.path: deleted")
return True
else:
logging.error("temp_dir.path: does not exist.")
return False
6 changes: 3 additions & 3 deletions src/cq_cli/cqcodecs/cq_codec_dxf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os, tempfile
import os
from cadquery import exporters
import cq_cli.cqcodecs.codec_helpers as helpers


def convert(build_result, output_file=None, error_file=None, output_opts=None):
# Create a temporary file to put the STL output into
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_dxf.dxf")
temp_dir = helpers.temp_dir()
temp_file = os.path.join(temp_dir.path, "temp_dxf.dxf")

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
Expand Down
6 changes: 3 additions & 3 deletions src/cq_cli/cqcodecs/cq_codec_glb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os, tempfile
import os
import cq_cli.cqcodecs.codec_helpers as helpers


def convert(build_result, output_file=None, error_file=None, output_opts=None):
# Create a temporary file to put the STL output into
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_glb.glb")
temp_dir = helpers.temp_dir()
temp_file = os.path.join(temp_dir.path, "temp_glb.glb")

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
Expand Down
6 changes: 3 additions & 3 deletions src/cq_cli/cqcodecs/cq_codec_gltf.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os, tempfile
import os
import cq_cli.cqcodecs.codec_helpers as helpers


def convert(build_result, output_file=None, error_file=None, output_opts=None):
# Create a temporary file to put the STL output into
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_gltf.gltf")
temp_dir = helpers.temp_dir()
temp_file = os.path.join(temp_dir.path, "temp_gltf.gltf")

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
Expand Down
7 changes: 3 additions & 4 deletions src/cq_cli/cqcodecs/cq_codec_step.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os, tempfile
import os
from cadquery import exporters
import cadquery as cq
import cq_cli.cqcodecs.codec_helpers as helpers


def convert(build_result, output_file=None, error_file=None, output_opts=None):
# Create a temporary file to put the STL output into
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_step.step")
temp_dir = helpers.temp_dir()
temp_file = os.path.join(temp_dir.path, "temp_step.step")

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
Expand Down
8 changes: 3 additions & 5 deletions src/cq_cli/cqcodecs/cq_codec_stl.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import os, tempfile
from cadquery import exporters
import cadquery as cq
import os
import cq_cli.cqcodecs.codec_helpers as helpers


def convert(build_result, output_file=None, error_file=None, output_opts=None):
# Create a temporary file to put the STL output into
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_stl.stl")
temp_dir = helpers.temp_dir()
temp_file = os.path.join(temp_dir.path, "temp_stl.stl")

linearDeflection = 0.1
angularDeflection = 0.1
Expand Down
6 changes: 3 additions & 3 deletions src/cq_cli/cqcodecs/cq_codec_svg.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os, tempfile
import os
from cadquery import exporters
import cq_cli.cqcodecs.codec_helpers as helpers


def convert(build_result, output_file=None, error_file=None, output_opts=None):
# Create a temporary file to put the STL output into
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_svg.svg")
temp_dir = helpers.temp_dir()
temp_file = os.path.join(temp_dir.path, "temp_svg.svg")

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
Expand Down
7 changes: 3 additions & 4 deletions src/cq_cli/cqcodecs/cq_codec_threejs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os, tempfile
import os
from cadquery import exporters
import cadquery as cq
import cq_cli.cqcodecs.codec_helpers as helpers


def convert(build_result, output_file=None, error_file=None, output_opts=None):
# Create a temporary file to put the STL output into
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_threejs.json")
temp_dir = helpers.temp_dir()
temp_file = os.path.join(temp_dir.path, "temp_threejs.json")

# The exporters will add extra output that we do not want, so suppress it
with helpers.suppress_stdout_stderr():
Expand Down
Loading