Esempio n. 1
0
    def __init__(self, stream_type, root_destination_dir, move=False, stream_db_type=None):
        """
        @param stream_type: a string representing what kind of stream this is, eg "video" or "image" or "gps"
                            should be a valid folder name in the filesystem

        @param root_destination_dir: path to the top level directory where this stream should be imported to
        @raise OSError:

        @raise NotADirectoryError:
        """

        self.stream_type = stream_type
        self.stream_db_type = stream_db_type
        self.root_destination_dir = root_destination_dir
        self.move = move

        #check if dir_path exists / create it if it doesn't
        try:
            fsutil.ensureDirectoryExists(self.root_destination_dir)
        except OSError:
            print self.root_destination_dir + " does not exist and could not be created."
            raise
        except fsutil.NotADirectoryError:
            print self.root_destination_dir + " is not a directory!"
            raise
Esempio n. 2
0
    def importFile(self, file_path):
        """
        imports file_path to self.root_destination_dir

        if move is set to true, files are moved not copied

        Files are organized hierarchically in following manner, based on the file's
        date of creation metadata:

        year (int)
            month (int)
                day (int)
                    stream_type (string)
                        X.<original file extension>

        Where X is an integer
        TODO: X should have some meaning, perhaps sort order by date of creation?
        """

        # make sure the file exists
        if not os.path.exists(file_path):
            raise NoSuchFileError(file_path)

        if self.move:
            copy_function = shutil.move
        else:
            copy_function = shutil.copy2

        # get the file's creation date
        # TODO: actually check creation date, not modified date
        start_stamp = os.path.getmtime(file_path)
        start_date = datetime.datetime.fromtimestamp(start_stamp)

        # figure out where to put the file
        dest_dir = fsutil.getStreamRawDataPath(start_date, self.stream_type)
        dest_dir = os.path.join(self.root_destination_dir, dest_dir)
        _, extension = os.path.splitext(file_path)
        filename = fsutil.generateUniqueFileName(dest_dir, extension, str(int(start_stamp)))
        dest_file = os.path.join(dest_dir, filename)

        # make sure dest_dir exists
        fsutil.ensureDirectoryExists(dest_dir)

        #copy the file
        copy_function(file_path, dest_file)

        if self.stream_db_type:
            self.stream_db_type(path=dest_file, creation=int(start_stamp))