Commit fd73732c authored by Joel Collins's avatar Joel Collins
Browse files

StreamingCamera cleans up StreamObject files automatically on close()

parent 360c8366
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -55,6 +55,9 @@ class StreamObject(object):
        # Keep on disk after close by default
        self.keep_on_disk = True

        # Log if created by context manager
        self.context_manager = False

        # Object lock
        self.locked = False  # Is the StreamObject locked for writing? (Handled by StreamingCamera)

@@ -62,6 +65,7 @@ class StreamObject(object):
        log("Entering context for {}.\
            Stored files will be cleaned up automatically.".format(self.id))
        self.keep_on_disk = False  # Flag file to be removed on close.
        self.context_manager = True  # Used in metadata
        return self

    def __exit__(self, *args):
@@ -128,6 +132,7 @@ class StreamObject(object):
            'locked': self.locked,
            'keep_on_disk': self.keep_on_disk,
            'path': self.file,
            'context_manager': self.context_manager,
        }

        # Get file path
@@ -211,7 +216,6 @@ class StreamObject(object):
            os.remove(self.file)
            return True
        else:
            log("File not found {}".format(self.file))
            return False

    def shunt(self):
+17 −0
Original line number Diff line number Diff line
@@ -97,6 +97,23 @@ class StreamingCamera(BaseCamera):
        """Run any initialisation code when the frame iterator starts."""
        pass
    
    # HANDLE CONTEXT MANAGER AND FILE CLOSING
    
    def close(self):
        # Close all StreamObjects
        for capture_list in [self.images, self.videos]:
            for stream_object in capture_list:
                stream_object.close()

    def __enter__(self):
        log("Entering context for {}.\
            Stored files will be cleaned up automatically.".format(self))
        return self

    def __exit__(self, *args):
        log("Cleaning up {}".format(self))
        self.close()

    # RETURNING CAPTURES

    @property