def importing_process(self):
        """Function that actually imports the files."""

        raise ValueError(
            "should not call this function anymore because the load is done on preset selection"
        )

        app = App.get_running_app()
        folders = self.folders
        import_to = self.import_to
        total_size = self.total_size
        imported_size = 0
        self.scanningpopup.scanning_text = "Importing " + format_size(
            total_size) + '  0%'
        imported_folders = []
        imported_files = 0
        failed_files = 0

        if disk_usage:
            free_space = disk_usage(import_to)[2]
            if total_size > free_space:
                self.scanningpopup.dismiss()
                self.scanningpopup = None
                app.message("Not enough free drive space! Cancelled import.")
                Clock.schedule_once(lambda *dt: app.show_import())

        # Scan folders
        for folder_path in folders:
            if self.cancel_scanning:
                break
            folder = folders[folder_path]
            folder_name = folder['name']
            if folder['photos']:
                if folder['naming']:
                    folder_name = naming(self.naming_method,
                                         title=folder['title'],
                                         year=folder['year'],
                                         month=folder['month'],
                                         day=folder['day'])
                photos = folder['photos']
                parent = folder['parent']
                if parent:
                    path_string = []
                    while parent:
                        newfolder = folders[parent]
                        newfolder_name = newfolder['name']
                        if newfolder['naming']:
                            newfolder_name = naming(self.naming_method,
                                                    title=newfolder['title'],
                                                    year=newfolder['year'],
                                                    month=newfolder['month'],
                                                    day=newfolder['day'])
                        path_string.append(newfolder_name)
                        parent = newfolder['parent']
                    for path in path_string:
                        folder_name = os.path.join(path, folder_name)
                folderinfo = [
                    folder_name, folder['title'], folder['description']
                ]
                path = os.path.join(import_to, folder_name)
                if not os.path.isdir(path):
                    os.makedirs(path)
                if not app.Folder.exist(folderinfo[0]):
                    app.Folder.insert(folderinfo)
                else:
                    if folderinfo[1]:
                        app.Folder.update_title(folderinfo[0],
                                                folderinfo[1]).commit()
                    if folderinfo[2]:
                        app.Folder.update_description(path, description)(
                            folderinfo[0], folderinfo[2])

                # Scan and import photos in folder
                for photo in photos:
                    if self.cancel_scanning:
                        break
                    completed = (imported_size / total_size)
                    remaining = 1 - completed
                    self.percent_completed = 100 * completed
                    self.scanningpopup.scanning_percentage = self.percent_completed

                    seconds_elapsed = time.time() - self.start_time
                    time_elapsed = '  Time: ' + str(
                        datetime.timedelta(seconds=int(seconds_elapsed)))
                    if self.percent_completed > 0:
                        seconds_remain = (seconds_elapsed *
                                          remaining) / completed
                        time_remain = '  Remaining: ' + str(
                            datetime.timedelta(seconds=int(seconds_remain)))
                    else:
                        time_remain = ''
                    self.scanningpopup.scanning_text = "Importing " + format_size(
                        total_size) + '  ' + str(
                            int(self.percent_completed
                                )) + '%  ' + time_elapsed + time_remain
                    old_full_filename = os.path.join(photo[2], photo[0])
                    new_photo_fullpath = os.path.join(folder_name, photo[10])
                    new_full_filename = os.path.join(import_to,
                                                     new_photo_fullpath)
                    thumbnail_data = app.Photo.thumbnail(photo[2],
                                                         temporary=True)
                    if not app.Photo.exist(new_photo_fullpath):
                        photo[0] = new_photo_fullpath
                        photo[1] = folder_name
                        photo[2] = import_to
                        photo[6] = int(time.time())

                        try:
                            copy2(old_full_filename, new_full_filename)
                        except:
                            failed_files = failed_files + 1
                            imported_size = imported_size + photo[4]
                        else:
                            if self.delete_originals:
                                if os.path.isfile(new_full_filename):
                                    if os.path.getsize(new_full_filename
                                                       ) == os.path.getsize(
                                                           old_full_filename):
                                        os.remove(old_full_filename)
                            app.Photo.add(photo)
                            # app.database_imported_add(photo[0], photo[10], photo[3])
                            if thumbnail_data:
                                thumbnail = thumbnail_data[2]
                                app.Photo.thumbnail_write(
                                    photo[0], int(time.time()), thumbnail,
                                    photo[13])
                            imported_size = imported_size + photo[4]
                            imported_files = imported_files + 1
                    else:
                        failed_files = failed_files + 1
                        imported_size = imported_size + photo[4]
                """
                imported_folders.append(folder_name)
        """

        raise ValueError('muste complete import')

        app.update_photoinfo(folders=imported_folders)
        self.scanningpopup.dismiss()
        if failed_files:
            failed = ' Could not import ' + str(failed_files) + ' files.'
        else:
            failed = ''
        if not self.cancel_scanning:
            if imported_files:
                app.message("Finished importing " + str(imported_files) +
                            " files." + failed)
        else:
            if imported_files:
                app.message("Canceled importing, " + str(imported_files) +
                            " files were imported." + failed)
            else:
                app.message("Canceled importing, no files were imported.")
        self.scanningpopup = None
        self.import_scanning = False
        Clock.schedule_once(lambda *dt: app.show_database())
    def update_treeview(self):
        """Clears and repopulates the left-side folder list."""

        folder_list = self.ids['folders']

        # Clear the treeview list
        nodes = list(folder_list.iterate_all_nodes())
        for node in nodes:
            folder_list.remove_node(node)
        selected_node = None

        # folder_item = TreeViewButton(target='removed', type='extra', owner=self, view_album=False)
        # folder_item.folder_name = 'Removed (Never Scan Again)'
        # total_photos = len(self.removed)
        # folder_item.total_photos_numeric = total_photos
        # if total_photos > 0:
        #    folder_item.total_photos = '('+str(total_photos)+')'
        # folder_list.add_node(folder_item)
        # if self.selected == 'removed' and self.type == 'extra':
        #    selected_node = folder_item

        # Populate the 'Already Imported' folder
        folder_item = TreeViewButton(target='duplicates',
                                     type='extra',
                                     owner=self,
                                     view_album=False)
        folder_item.folder_name = 'Already Imported (Never Import Again)'
        total_photos = len(self.duplicates)
        folder_item.total_photos_numeric = total_photos
        if total_photos > 0:
            folder_item.total_photos = '(' + str(total_photos) + ')'
        folder_list.add_node(folder_item)
        if self.selected == 'duplicates' and self.type == 'extra':
            selected_node = folder_item

        # Populate the 'Unsorted' folder
        folder_item = TreeViewButton(target='unsorted',
                                     type='extra',
                                     owner=self,
                                     view_album=False)
        folder_item.folder_name = 'Unsorted (Not Imported This Time)'
        total_photos = len(self.unsorted)
        folder_item.total_photos_numeric = total_photos
        if total_photos > 0:
            folder_item.total_photos = '(' + str(total_photos) + ')'
        folder_list.add_node(folder_item)
        if self.selected == 'unsorted' and self.type == 'extra':
            selected_node = folder_item

        # Populate the importing folders
        sorted_folders = sorted(self.folders)
        self.total_size = 0
        to_parent = []
        added_nodes = {}
        for folder_date in sorted_folders:
            folder_info = self.folders[folder_date]
            target = folder_date
            folder_item = TreeViewButton(is_open=True,
                                         fullpath=target,
                                         dragable=True,
                                         target=target,
                                         type='folder',
                                         owner=self,
                                         view_album=False)
            if folder_info['naming']:
                folder_item.folder_name = naming(self.naming_method,
                                                 title=folder_info['title'],
                                                 year=folder_info['year'],
                                                 month=folder_info['month'],
                                                 day=folder_info['day'])
            else:
                folder_item.folder_name = folder_info['name']
            added_nodes[folder_date] = folder_item
            photos = folder_info['photos']
            for photo in photos:
                self.total_size = self.total_size + photo[4]
            total_photos = len(photos)
            folder_item.total_photos_numeric = total_photos
            if total_photos > 0:
                folder_item.total_photos = '(' + str(total_photos) + ')'
            if folder_info['parent']:
                to_parent.append([folder_item, folder_info['parent']])
            else:
                folder_list.add_node(folder_item)
            if self.selected == target and self.type == 'folder':
                selected_node = folder_item
        for item in to_parent:
            node = item[0]
            parent_name = item[1]
            if parent_name in added_nodes.keys():
                folder_list.add_node(node, parent=added_nodes[parent_name])
            else:
                folder_list.add_node(node)
        if selected_node:
            folder_list.select_node(selected_node)
        size_display = self.ids['totalSize']
        size_display.text = 'Total Size: ' + format_size(self.total_size)
Exemple #3
0
    def refresh_folder(self, *_):
        file_list = self.ids['fileList']
        files = self.ids['files']
        files.selects = []
        data = []
        files = []
        dirs = []

        walk = os.walk
        for root, list_dirs, list_files in walk(self.path, topdown=True):
            dirs = list_dirs[:]
            list_dirs.clear()
            files = list_files

        self.folder_files = files
        if dirs or files:
            self.can_delete_folder = False
        else:
            self.can_delete_folder = True
        dirs = sorted(dirs, key=lambda s: s.lower())
        for directory in dirs:
            fullpath = os.path.join(self.path, directory)
            data.append({
                'text': directory,
                'fullpath': fullpath,
                'path': fullpath + os.path.sep,
                'type': 'folder',
                'file': '',
                'owner': self,
                'is_folder': True,
                'selected': False,
                'multiselect': self.multiselect,
                'selectable': self.directory_select,
                'file_size': '',
                'modified': ''
            })
        if not self.directory_select:
            if self.filters:
                filtered_files = []
                for item in self.filters:
                    filtered_files += fnmatch.filter(files, item)
                files = filtered_files
            #files = sorted(files, key=lambda s: s.lower())
            files = sort_nicely(files)
            for file in files:
                fullpath = os.path.join(self.path, file)
                file_size = int(os.path.getsize(fullpath))
                modified = int(os.path.getmtime(fullpath))
                data.append({
                    'text': file,
                    'fullpath': fullpath,
                    'path': self.path,
                    'type': file,
                    'file': file,
                    'owner': self,
                    'is_folder': False,
                    'selected': False,
                    'multiselect': self.multiselect,
                    'selectable': True,
                    'file_size': format_size(file_size),
                    'modified': datetime.datetime.fromtimestamp(modified).strftime('%Y-%m-%d, %I:%M%p')
                })

        file_list.data = data
        if not self.directory_select:
            if self.export_mode:
                if not self.file:
                    self.target_selected = False
                else:
                    self.target_selected = True
            else:
                self.file = ''
                self.filename = ''
                self.target_selected = False
        else:
            self.file = ''
            self.filename = ''
            self.target_selected = True

        self.reset_folder_position()
        if self.autoselect:
            self.toggle_select()