Example #1
0
 def new_naming_method(self, instance):
     if not instance.focus:
         if not naming(instance.text, title=''):
             self.naming_method = self.last_naming_method
             instance.text = self.last_naming_method
         else:
             self.last_naming_method = instance.text
             self.naming_method = instance.text
             self.naming_example = naming(self.naming_method)
             self.update_preset()
    def new_title(self, title_editor):
        """Called when the title field of the currently selected folder is edited.
        Update internal variables to match.
        Argument:
            title_editor: The input box that has been edited.
        """

        title = title_editor.text
        if self.type == 'folder':
            self.folders[self.selected]['title'] = title
            folder_info = self.folders[self.selected]
            self.update_treeview()
            folder_name = self.ids['folderName']
            folder_name.text = naming(self.naming_method,
                                      title=folder_info['title'],
                                      year=folder_info['year'],
                                      month=folder_info['month'],
                                      day=folder_info['day'])
    def update_photolist(self):
        """Redraw the photo list view for the currently selected folder."""

        folder_name = self.ids['folderName']
        photos = []
        name = ''
        title_editor = self.ids['folderTitle']
        description_editor = self.ids['folderDescription']
        dragable = True

        # Viewing an input folder.
        if self.type == 'folder':
            if self.selected in self.folders:
                folder_info = self.folders[self.selected]
                title_editor.text = folder_info['title']
                title_editor.disabled = False
                description_editor.text = folder_info['description']
                description_editor.disabled = False
                photos = folder_info['photos']
                if folder_info['naming']:
                    name = naming(self.naming_method,
                                  title=folder_info['title'],
                                  year=folder_info['year'],
                                  month=folder_info['month'],
                                  day=folder_info['day'])
                else:
                    name = self.selected

        # Viewing a special sorting folder.
        else:
            title_editor.text = ''
            title_editor.disabled = True
            description_editor.text = ''
            description_editor.disabled = True
            if self.selected == 'unsorted':
                photos = self.unsorted
                name = 'Unsorted (Not Imported This Time)'
            elif self.selected == 'removed':
                photos = self.removed
                name = 'Removed (Never Scanned Again)'
            elif self.selected == 'duplicates':
                dragable = False
                photos = self.duplicates
                name = 'Already Imported (Never Import Again)'

        folder_name.text = name

        # Populate photo view
        photos_container = self.ids['photosContainer']
        datas = []
        for photo in photos:
            full_filename = os.path.join(photo.database_folder, photo.fullpath)
            fullpath = photo.fullpath
            database_folder = photo.database_folder
            video = os.path.splitext(full_filename)[1].lower() in movietypes
            data = {
                'fullpath': fullpath,
                'temporary': True,
                'photo': photo,
                'folder': self.selected,
                'database_folder': database_folder,
                'filename': full_filename,
                'target': self.selected,
                'type': self.type,
                'owner': self,
                'video': video,
                'photo_orientation': photo.orientation,
                'source': full_filename,
                'title': photo.owner,
                'selected': False,
                'selectable': True,
                'dragable': dragable
            }
            datas.append(data)
        photos_container.data = datas
        self.select_none()
    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)
    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())
Example #6
0
 def on_data(self, *_):
     import_preset = self.data
     naming_method = import_preset['naming_method']
     self.content = ImportPresetArea(index=self.index, title=import_preset['title'], import_to=import_preset['import_to'], naming_method=naming_method, naming_example=naming(naming_method), last_naming_method=naming_method, single_folder=import_preset['single_folder'], delete_originals=import_preset['delete_originals'], import_from=import_preset['import_from'], owner=self)