コード例 #1
0
ファイル: app.py プロジェクト: darmstard/happypanda
	def dropEvent(self, event):
		acceptable = []
		unaccept = []
		for u in event.mimeData().urls():
			path = u.toLocalFile()
			if os.path.isdir(path) or path.endswith(utils.ARCHIVE_FILES):
				acceptable.append(path)
			else:
				unaccept.append(path)
		log_i('Acceptable dropped items: {}'.format(len(acceptable)))
		log_i('Unacceptable dropped items: {}'.format(len(unaccept)))
		log_d('Dropped items: {}\n{}'.format(acceptable, unaccept).encode(errors='ignore'))
		if acceptable:
			self.notification_bar.add_text('Adding dropped items...')
			log_i('Adding dropped items')
			l = len(acceptable) == 1
			f_item = acceptable[0]
			if f_item.endswith(utils.ARCHIVE_FILES):
				f_item = utils.check_archive(f_item)
			else:
				f_item = utils.recursive_gallery_check(f_item)
			f_item_l = len(f_item) < 2
			subfolder_as_c = not app_constants.SUBFOLDER_AS_GALLERY
			if l and subfolder_as_c or l and f_item_l:
				g_d = gallerydialog.GalleryDialog(self, acceptable[0])
				g_d.SERIES.connect(self.manga_list_view.gallery_model.addRows)
				g_d.show()
			else:
				self.gallery_populate(acceptable, True)
		else:
			text = 'File not supported' if len(unaccept) < 2 else 'Files not supported'
			self.notification_bar.add_text(text)

		if unaccept:
			self.notification_bar.add_text('Some unsupported files did not get added')
コード例 #2
0
 def choose_dir(self, mode):
     """
     Pass which mode to open the folder explorer in:
     'f': directory
     'a': files
     Or pass a predefined path
     """
     self.done.show()
     self.file_exists_lbl.hide()
     if mode == 'a':
         name = QFileDialog.getOpenFileName(self,
                                            'Choose archive',
                                            filter=utils.FILE_FILTER)
         name = name[0]
     elif mode == 'f':
         name = QFileDialog.getExistingDirectory(self, 'Choose folder')
     elif mode:
         if os.path.exists(mode):
             name = mode
         else:
             return None
     if not name:
         return
     head, tail = os.path.split(name)
     name = os.path.join(head, tail)
     parsed = utils.title_parser(tail)
     self.title_edit.setText(parsed['title'])
     self.author_edit.setText(parsed['artist'])
     self.path_lbl.setText(name)
     if not parsed['language']:
         parsed['language'] = app_constants.G_DEF_LANGUAGE
     l_i = self.lang_box.findText(parsed['language'])
     if l_i != -1:
         self.lang_box.setCurrentIndex(l_i)
     if gallerydb.GalleryDB.check_exists(name):
         self.file_exists_lbl.setText(
             '<font color="red">Gallery already exists.</font>')
         self.file_exists_lbl.show()
     # check galleries
     gs = 1
     if name.endswith(utils.ARCHIVE_FILES):
         gs = len(utils.check_archive(name))
     elif os.path.isdir(name):
         g_dirs, g_archs = utils.recursive_gallery_check(name)
         gs = len(g_dirs) + len(g_archs)
     if gs == 0:
         self.file_exists_lbl.setText(
             '<font color="red">Invalid gallery source.</font>')
         self.file_exists_lbl.show()
         self.done.hide()
     if app_constants.SUBFOLDER_AS_GALLERY:
         if gs > 1:
             self.file_exists_lbl.setText(
                 '<font color="red">More than one galleries detected in source! Use other methods to add.</font>'
             )
             self.file_exists_lbl.show()
             self.done.hide()
コード例 #3
0
	def choose_dir(self, mode):
		"""
		Pass which mode to open the folder explorer in:
		'f': directory
		'a': files
		Or pass a predefined path
		"""
		self.done.show()
		self.file_exists_lbl.hide()
		if mode == 'a':
			name = QFileDialog.getOpenFileName(self, 'Choose archive',
											  filter=utils.FILE_FILTER)
			name = name[0]
		elif mode == 'f':
			name = QFileDialog.getExistingDirectory(self, 'Choose folder')
		elif mode:
			if os.path.exists(mode):
				name = mode
			else:
				return None
		if not name:
			return
		head, tail = os.path.split(name)
		name = os.path.join(head, tail)
		parsed = utils.title_parser(tail)
		self.title_edit.setText(parsed['title'])
		self.author_edit.setText(parsed['artist'])
		self.path_lbl.setText(name)
		if not parsed['language']:
			parsed['language'] = app_constants.G_DEF_LANGUAGE
		l_i = self.lang_box.findText(parsed['language'])
		if l_i != -1:
			self.lang_box.setCurrentIndex(l_i)
		if gallerydb.GalleryDB.check_exists(name):
			self.file_exists_lbl.setText('<font color="red">Gallery already exists.</font>')
			self.file_exists_lbl.show()
		# check galleries
		gs = 1
		if name.endswith(utils.ARCHIVE_FILES):
			gs = len(utils.check_archive(name))
		elif os.path.isdir(name):
			g_dirs, g_archs = utils.recursive_gallery_check(name)
			gs = len(g_dirs) + len(g_archs)
		if gs == 0:
			self.file_exists_lbl.setText('<font color="red">Invalid gallery source.</font>')
			self.file_exists_lbl.show()
			self.done.hide()
		if app_constants.SUBFOLDER_AS_GALLERY:
			if gs > 1:
				self.file_exists_lbl.setText('<font color="red">More than one galleries detected in source! Use other methods to add.</font>')
				self.file_exists_lbl.show()
				self.done.hide()
コード例 #4
0
ファイル: io_misc.py プロジェクト: Pewpews/happypanda
 def on_created(self, event):
     if not app_constants.OVERRIDE_MONITOR:
         if self.file_filter(event):
             gs = 0
             if event.src_path.endswith(utils.ARCHIVE_FILES):
                 gs = len(utils.check_archive(event.src_path))
             elif event.is_directory:
                 g_dirs, g_archs = utils.recursive_gallery_check(event.src_path)
                 gs = len(g_dirs) + len(g_archs)
             if gs:
                 self.CREATE_SIGNAL.emit(event.src_path)
     else:
         app_constants.OVERRIDE_MONITOR = False
コード例 #5
0
 def on_created(self, event):
     if not app_constants.OVERRIDE_MONITOR:
         if self.file_filter(event):
             gs = 0
             if event.src_path.endswith(utils.ARCHIVE_FILES):
                 gs = len(utils.check_archive(event.src_path))
             elif event.is_directory:
                 g_dirs, g_archs = utils.recursive_gallery_check(
                     event.src_path)
                 gs = len(g_dirs) + len(g_archs)
             if gs:
                 self.CREATE_SIGNAL.emit(event.src_path)
     else:
         app_constants.OVERRIDE_MONITOR = False
コード例 #6
0
ファイル: fetch.py プロジェクト: twiddli/happypanda
    def local(self, s_path=None):
        """
        Do a local search in the given series_path.
        """
        self._data.clear()
        if s_path:
            self.series_path = s_path
        try:
            gallery_l = sorted([
                p.name for p in scandir.scandir(self.series_path)
            ])  #list of folders in the "Gallery" folder
            mixed = False
        except TypeError:
            gallery_l = self.series_path
            mixed = True
        if len(gallery_l) != 0:  # if gallery path list is not empty
            log_i('Gallery folder is not empty')
            if len(self.galleries_from_db) != len(app_constants.GALLERY_DATA):
                self._refresh_filter_list()
            self.DATA_COUNT.emit(len(
                gallery_l))  #tell model how many items are going to be added
            log_i('Received {} paths'.format(len(gallery_l)))
            progress = 0

            for folder_name in gallery_l:  # folder_name = gallery folder title
                self._curr_gallery = folder_name
                if mixed:
                    path = folder_name
                    folder_name = os.path.split(path)[1]
                else:
                    path = os.path.join(self.series_path, folder_name)
                if app_constants.SUBFOLDER_AS_GALLERY or app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
                    if app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
                        app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY = False
                    log_i("Treating each subfolder as gallery")
                    if os.path.isdir(path):
                        gallery_folders, gallery_archives = utils.recursive_gallery_check(
                            path)
                        for gs in gallery_folders:
                            self.create_gallery(gs,
                                                os.path.split(gs)[1], False)
                        p_saving = {}
                        for gs in gallery_archives:

                            self.create_gallery(gs[0],
                                                os.path.split(gs[0])[1],
                                                False,
                                                archive=gs[1])
                    elif path.endswith(utils.ARCHIVE_FILES):
                        for g in utils.check_archive(path):
                            self.create_gallery(g,
                                                os.path.split(g)[1],
                                                False,
                                                archive=path)
                else:
                    try:
                        if os.path.isdir(path):
                            if not list(scandir.scandir(path)):
                                raise ValueError
                        elif not path.endswith(utils.ARCHIVE_FILES):
                            raise NotADirectoryError

                        log_i("Treating each subfolder as chapter")
                        self.create_gallery(path,
                                            folder_name,
                                            do_chapters=True)

                    except ValueError:
                        self.skipped_paths.append((path, 'Empty directory'))
                        log_w('Directory is empty: {}'.format(
                            path.encode(errors='ignore')))
                    except NotADirectoryError:
                        self.skipped_paths.append((path, 'Unsupported file'))
                        log_w('Unsupported file: {}'.format(
                            path.encode(errors='ignore')))

                progress += 1  # update the progress bar
                self.PROGRESS.emit(progress)
        else:  # if gallery folder is empty
            log_e('Local search error: Invalid directory')
            log_e('Gallery folder is empty')
            app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = True  # sanity check
            self.FINISHED.emit(False)
            # might want to include an error message
        app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = False
        # everything went well
        log_i('Local search: OK')
        log_i('Created {} items'.format(len(self._data)))
        if self._to_queue_container:
            for x in self._data:
                self._galleries_queue.put(x)
        else:
            self.FINISHED.emit(self._data)
        if self.skipped_paths:
            self.SKIPPED.emit(self.skipped_paths)
コード例 #7
0
ファイル: fetch.py プロジェクト: bigretromike/happypanda
	def local(self, s_path=None):
		"""
		Do a local search in the given series_path.
		"""
		self.data.clear()
		if s_path:
			self.series_path = s_path
		try:
			gallery_l = sorted([p.name for p in scandir.scandir(self.series_path)]) #list of folders in the "Gallery" folder
			mixed = False
		except TypeError:
			gallery_l = self.series_path
			mixed = True
		if len(gallery_l) != 0: # if gallery path list is not empty
			log_i('Gallery folder is not empty')
			try:
				if len(self.galleries_from_db) != len(app_constants.GALLERY_DATA):
					self._refresh_filter_list()
				self.DATA_COUNT.emit(len(gallery_l)) #tell model how many items are going to be added
				log_i('Received {} paths'.format(len(gallery_l)))
				progress = 0
				def create_gallery(path, folder_name, do_chapters=True, archive=None):
					is_archive = True if archive else False
					temp_p = archive if is_archive else path
					folder_name = folder_name or path if folder_name or path else os.path.split(archive)[1]
					if utils.check_ignore_list(temp_p) and not GalleryDB.check_exists(temp_p, self.galleries_from_db, False):
						log_i('Creating gallery: {}'.format(folder_name.encode('utf-8', 'ignore')))
						new_gallery = Gallery()
						images_paths = []
						metafile = utils.GMetafile()
						try:
							con = scandir.scandir(temp_p) #all of content in the gallery folder
							log_i('Gallery source is a directory')
							chapters = sorted([sub.path for sub in con if sub.is_dir() or sub.name.endswith(utils.ARCHIVE_FILES)])\
							    if do_chapters else [] #subfolders
							# if gallery has chapters divided into sub folders
							numb_of_chapters = len(chapters)
							if numb_of_chapters != 0:
								log_i('Gallery has {} chapters'.format(numb_of_chapters))
								for ch in chapters:
									chap = new_gallery.chapters.create_chapter()
									chap.title = utils.title_parser(ch)['title']
									chap.path = os.path.join(path, ch)
									chap.pages = len(list(scandir.scandir(chap.path)))
									metafile.update(utils.GMetafile(chap.path))

							else: #else assume that all images are in gallery folder
								chap = new_gallery.chapters.create_chapter()
								chap.title = utils.title_parser(os.path.split(path)[1])['title']
								chap.path = path
								metafile.update(utils.GMetafile(chap.path))
								chap.pages = len(list(scandir.scandir(path)))
				
							parsed = utils.title_parser(folder_name)
						except NotADirectoryError:
							try:
								if is_archive or temp_p.endswith(utils.ARCHIVE_FILES):
									log_i('Gallery source is an archive')
									contents = utils.check_archive(temp_p)
									if contents:
										new_gallery.is_archive = 1
										new_gallery.path_in_archive = '' if not is_archive else path
										if folder_name.endswith('/'):
											folder_name = folder_name[:-1]
											fn = os.path.split(folder_name)
											folder_name = fn[1] or fn[2]
										folder_name = folder_name.replace('/','')
										if folder_name.endswith(utils.ARCHIVE_FILES):
											n = folder_name
											for ext in utils.ARCHIVE_FILES:
												n = n.replace(ext, '')
											parsed = utils.title_parser(n)
										else:
											parsed = utils.title_parser(folder_name)
												
										if do_chapters:
											archive_g = sorted(contents)
											if not archive_g:
												log_w('No chapters found for {}'.format(temp_p.encode(errors='ignore')))
												raise ValueError
											for g in archive_g:
												chap = new_gallery.chapters.create_chapter()
												chap.in_archive = 1
												chap.title = utils.title_parser(g)['title']
												chap.path = g
												metafile.update(utils.GMetafile(g, temp_p))
												arch = utils.ArchiveFile(temp_p)
												chap.pages = len(arch.dir_contents(g))
												arch.close()
										else:
											chap = new_gallery.chapters.create_chapter()
											chap.title = utils.title_parser(os.path.split(path)[1])['title']
											chap.in_archive = 1
											chap.path = path
											metafile.update(utils.GMetafile(path, temp_p))
											arch = utils.ArchiveFile(temp_p)
											chap.pages = len(arch.dir_contents(''))
											arch.close()
									else:
										raise ValueError
								else:
									raise ValueError
							except ValueError:
								log_w('Skipped {} in local search'.format(path.encode(errors='ignore')))
								self.skipped_paths.append((temp_p, 'Empty archive',))
								return
							except app_constants.CreateArchiveFail:
								log_w('Skipped {} in local search'.format(path.encode(errors='ignore')))
								self.skipped_paths.append((temp_p, 'Error creating archive',))
								return

						new_gallery.title = parsed['title']
						new_gallery.path = temp_p
						new_gallery.artist = parsed['artist']
						new_gallery.language = parsed['language']
						new_gallery.info = ""
						metafile.apply_gallery(new_gallery)
						if app_constants.MOVE_IMPORTED_GALLERIES and not app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH:
							new_gallery.path = utils.move_files(temp_p)

						self.data.append(new_gallery)
						log_i('Gallery successful created: {}'.format(folder_name.encode('utf-8', 'ignore')))
					else:
						log_i('Gallery already exists: {}'.format(folder_name.encode('utf-8', 'ignore')))
						self.skipped_paths.append((temp_p, 'Already exists'))

				for folder_name in gallery_l: # folder_name = gallery folder title
					self._curr_gallery = folder_name
					if mixed:
						path = folder_name
						folder_name = os.path.split(path)[1]
					else:
						path = os.path.join(self.series_path, folder_name)
					if app_constants.SUBFOLDER_AS_GALLERY or app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
						if app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
							app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY = False
						log_i("Treating each subfolder as gallery")
						if os.path.isdir(path):
							gallery_folders, gallery_archives = utils.recursive_gallery_check(path)
							for gs in gallery_folders:
									create_gallery(gs, os.path.split(gs)[1], False)
							p_saving = {}
							for gs in gallery_archives:
									
									create_gallery(gs[0], os.path.split(gs[0])[1], False, archive=gs[1])
						elif path.endswith(utils.ARCHIVE_FILES):
							for g in utils.check_archive(path):
								create_gallery(g, os.path.split(g)[1], False, archive=path)
					else:
						try:
							if os.path.isdir(path):
								if not list(scandir.scandir(path)):
									raise ValueError
							elif not path.endswith(utils.ARCHIVE_FILES):
								raise NotADirectoryError

							log_i("Treating each subfolder as chapter")
							create_gallery(path, folder_name, do_chapters=True)

						except ValueError:
							self.skipped_paths.append((path, 'Empty directory'))
							log_w('Directory is empty: {}'.format(path.encode(errors='ignore')))
						except NotADirectoryError:
							self.skipped_paths.append((path, 'Unsupported file'))
							log_w('Unsupported file: {}'.format(path.encode(errors='ignore')))

					progress += 1 # update the progress bar
					self.PROGRESS.emit(progress)
			except:
				log.exception('Local Search Error:')
				app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = True # sanity check
				self.FINISHED.emit(False)
		else: # if gallery folder is empty
			log_e('Local search error: Invalid directory')
			log_e('Gallery folder is empty')
			app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = True # sanity check
			self.FINISHED.emit(False)
			# might want to include an error message
		app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = False
		# everything went well
		log_i('Local search: OK')
		log_i('Created {} items'.format(len(self.data)))
		self.FINISHED.emit(self.data)
		if self.skipped_paths:
			self.SKIPPED.emit(self.skipped_paths)
コード例 #8
0
ファイル: fetch.py プロジェクト: Pewpews/happypanda
    def local(self, s_path=None):
        """
        Do a local search in the given series_path.
        """
        self._data.clear()
        if s_path:
            self.series_path = s_path
        try:
            gallery_l = sorted([p.name for p in scandir.scandir(self.series_path)]) #list of folders in the "Gallery" folder
            mixed = False
        except TypeError:
            gallery_l = self.series_path
            mixed = True
        if len(gallery_l) != 0: # if gallery path list is not empty
            log_i('Gallery folder is not empty')
            if len(self.galleries_from_db) != len(app_constants.GALLERY_DATA):
                self._refresh_filter_list()
            self.DATA_COUNT.emit(len(gallery_l)) #tell model how many items are going to be added
            log_i('Received {} paths'.format(len(gallery_l)))
            progress = 0

            for folder_name in gallery_l: # folder_name = gallery folder title
                self._curr_gallery = folder_name
                if mixed:
                    path = folder_name
                    folder_name = os.path.split(path)[1]
                else:
                    path = os.path.join(self.series_path, folder_name)
                if app_constants.SUBFOLDER_AS_GALLERY or app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
                    if app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
                        app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY = False
                    log_i("Treating each subfolder as gallery")
                    if os.path.isdir(path):
                        gallery_folders, gallery_archives = utils.recursive_gallery_check(path)
                        for gs in gallery_folders:
                                self.create_gallery(gs, os.path.split(gs)[1], False)
                        p_saving = {}
                        for gs in gallery_archives:
                                    
                                self.create_gallery(gs[0], os.path.split(gs[0])[1], False, archive=gs[1])
                    elif path.endswith(utils.ARCHIVE_FILES):
                        for g in utils.check_archive(path):
                            self.create_gallery(g, os.path.split(g)[1], False, archive=path)
                else:
                    try:
                        if os.path.isdir(path):
                            if not list(scandir.scandir(path)):
                                raise ValueError
                        elif not path.endswith(utils.ARCHIVE_FILES):
                            raise NotADirectoryError

                        log_i("Treating each subfolder as chapter")
                        self.create_gallery(path, folder_name, do_chapters=True)

                    except ValueError:
                        self.skipped_paths.append((path, 'Empty directory'))
                        log_w('Directory is empty: {}'.format(path.encode(errors='ignore')))
                    except NotADirectoryError:
                        self.skipped_paths.append((path, 'Unsupported file'))
                        log_w('Unsupported file: {}'.format(path.encode(errors='ignore')))

                progress += 1 # update the progress bar
                self.PROGRESS.emit(progress)
        else: # if gallery folder is empty
            log_e('Local search error: Invalid directory')
            log_e('Gallery folder is empty')
            app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = True # sanity check
            self.FINISHED.emit(False)
            # might want to include an error message
        app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = False
        # everything went well
        log_i('Local search: OK')
        log_i('Created {} items'.format(len(self._data)))
        if self._to_queue_container:
            for x in self._data:
                self._galleries_queue.put(x)
        else:
            self.FINISHED.emit(self._data)
        if self.skipped_paths:
            self.SKIPPED.emit(self.skipped_paths)
コード例 #9
0
    def local(self, s_path=None):
        """
		Do a local search in the given series_path.
		"""
        self.data.clear()
        if s_path:
            self.series_path = s_path
        try:
            gallery_l = sorted([
                p.name for p in scandir.scandir(self.series_path)
            ])  #list of folders in the "Gallery" folder
            mixed = False
        except TypeError:
            gallery_l = self.series_path
            mixed = True
        if len(gallery_l) != 0:  # if gallery path list is not empty
            log_i('Gallery folder is not empty')
            try:
                if len(self.galleries_from_db) != len(
                        app_constants.GALLERY_DATA):
                    self._refresh_filter_list()
                self.DATA_COUNT.emit(
                    len(gallery_l
                        ))  #tell model how many items are going to be added
                log_i('Received {} paths'.format(len(gallery_l)))
                progress = 0

                def create_gallery(path,
                                   folder_name,
                                   do_chapters=True,
                                   archive=None):
                    is_archive = True if archive else False
                    temp_p = archive if is_archive else path
                    folder_name = folder_name or path if folder_name or path else os.path.split(
                        archive)[1]
                    if utils.check_ignore_list(
                            temp_p) and not GalleryDB.check_exists(
                                temp_p, self.galleries_from_db, False):
                        log_i('Creating gallery: {}'.format(
                            folder_name.encode('utf-8', 'ignore')))
                        new_gallery = Gallery()
                        images_paths = []
                        metafile = utils.GMetafile()
                        try:
                            con = scandir.scandir(
                                temp_p)  #all of content in the gallery folder
                            log_i('Gallery source is a directory')
                            chapters = sorted([sub.path for sub in con if sub.is_dir() or sub.name.endswith(utils.ARCHIVE_FILES)])\
                                if do_chapters else [] #subfolders
                            # if gallery has chapters divided into sub folders
                            numb_of_chapters = len(chapters)
                            if numb_of_chapters != 0:
                                log_i('Gallery has {} chapters'.format(
                                    numb_of_chapters))
                                for ch in chapters:
                                    chap = new_gallery.chapters.create_chapter(
                                    )
                                    chap.title = utils.title_parser(
                                        ch)['title']
                                    chap.path = os.path.join(path, ch)
                                    chap.pages = len(
                                        list(scandir.scandir(chap.path)))
                                    metafile.update(utils.GMetafile(chap.path))

                            else:  #else assume that all images are in gallery folder
                                chap = new_gallery.chapters.create_chapter()
                                chap.title = utils.title_parser(
                                    os.path.split(path)[1])['title']
                                chap.path = path
                                metafile.update(utils.GMetafile(chap.path))
                                chap.pages = len(list(scandir.scandir(path)))

                            parsed = utils.title_parser(folder_name)
                        except NotADirectoryError:
                            try:
                                if is_archive or temp_p.endswith(
                                        utils.ARCHIVE_FILES):
                                    log_i('Gallery source is an archive')
                                    contents = utils.check_archive(temp_p)
                                    if contents:
                                        new_gallery.is_archive = 1
                                        new_gallery.path_in_archive = '' if not is_archive else path
                                        if folder_name.endswith('/'):
                                            folder_name = folder_name[:-1]
                                            fn = os.path.split(folder_name)
                                            folder_name = fn[1] or fn[2]
                                        folder_name = folder_name.replace(
                                            '/', '')
                                        if folder_name.endswith(
                                                utils.ARCHIVE_FILES):
                                            n = folder_name
                                            for ext in utils.ARCHIVE_FILES:
                                                n = n.replace(ext, '')
                                            parsed = utils.title_parser(n)
                                        else:
                                            parsed = utils.title_parser(
                                                folder_name)

                                        if do_chapters:
                                            archive_g = sorted(contents)
                                            if not archive_g:
                                                log_w(
                                                    'No chapters found for {}'.
                                                    format(
                                                        temp_p.encode(
                                                            errors='ignore')))
                                                raise ValueError
                                            for g in archive_g:
                                                chap = new_gallery.chapters.create_chapter(
                                                )
                                                chap.in_archive = 1
                                                chap.title = utils.title_parser(
                                                    g)['title']
                                                chap.path = g
                                                metafile.update(
                                                    utils.GMetafile(g, temp_p))
                                                arch = utils.ArchiveFile(
                                                    temp_p)
                                                chap.pages = len(
                                                    arch.dir_contents(g))
                                                arch.close()
                                        else:
                                            chap = new_gallery.chapters.create_chapter(
                                            )
                                            chap.title = utils.title_parser(
                                                os.path.split(path)
                                                [1])['title']
                                            chap.in_archive = 1
                                            chap.path = path
                                            metafile.update(
                                                utils.GMetafile(path, temp_p))
                                            arch = utils.ArchiveFile(temp_p)
                                            chap.pages = len(
                                                arch.dir_contents(''))
                                            arch.close()
                                    else:
                                        raise ValueError
                                else:
                                    raise ValueError
                            except ValueError:
                                log_w('Skipped {} in local search'.format(
                                    path.encode(errors='ignore')))
                                self.skipped_paths.append((
                                    temp_p,
                                    'Empty archive',
                                ))
                                return
                            except app_constants.CreateArchiveFail:
                                log_w('Skipped {} in local search'.format(
                                    path.encode(errors='ignore')))
                                self.skipped_paths.append((
                                    temp_p,
                                    'Error creating archive',
                                ))
                                return

                        new_gallery.title = parsed['title']
                        new_gallery.path = temp_p
                        new_gallery.artist = parsed['artist']
                        new_gallery.language = parsed['language']
                        new_gallery.info = ""
                        metafile.apply_gallery(new_gallery)
                        if app_constants.MOVE_IMPORTED_GALLERIES and not app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH:
                            new_gallery.path = utils.move_files(temp_p)

                        self.data.append(new_gallery)
                        log_i('Gallery successful created: {}'.format(
                            folder_name.encode('utf-8', 'ignore')))
                    else:
                        log_i('Gallery already exists: {}'.format(
                            folder_name.encode('utf-8', 'ignore')))
                        self.skipped_paths.append((temp_p, 'Already exists'))

                for folder_name in gallery_l:  # folder_name = gallery folder title
                    self._curr_gallery = folder_name
                    if mixed:
                        path = folder_name
                        folder_name = os.path.split(path)[1]
                    else:
                        path = os.path.join(self.series_path, folder_name)
                    if app_constants.SUBFOLDER_AS_GALLERY or app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
                        if app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
                            app_constants.OVERRIDE_SUBFOLDER_AS_GALLERY = False
                        log_i("Treating each subfolder as gallery")
                        if os.path.isdir(path):
                            gallery_folders, gallery_archives = utils.recursive_gallery_check(
                                path)
                            for gs in gallery_folders:
                                create_gallery(gs, os.path.split(gs)[1], False)
                            p_saving = {}
                            for gs in gallery_archives:

                                create_gallery(gs[0],
                                               os.path.split(gs[0])[1],
                                               False,
                                               archive=gs[1])
                        elif path.endswith(utils.ARCHIVE_FILES):
                            for g in utils.check_archive(path):
                                create_gallery(g,
                                               os.path.split(g)[1],
                                               False,
                                               archive=path)
                    else:
                        try:
                            if os.path.isdir(path):
                                if not list(scandir.scandir(path)):
                                    raise ValueError
                            elif not path.endswith(utils.ARCHIVE_FILES):
                                raise NotADirectoryError

                            log_i("Treating each subfolder as chapter")
                            create_gallery(path, folder_name, do_chapters=True)

                        except ValueError:
                            self.skipped_paths.append(
                                (path, 'Empty directory'))
                            log_w('Directory is empty: {}'.format(
                                path.encode(errors='ignore')))
                        except NotADirectoryError:
                            self.skipped_paths.append(
                                (path, 'Unsupported file'))
                            log_w('Unsupported file: {}'.format(
                                path.encode(errors='ignore')))

                    progress += 1  # update the progress bar
                    self.PROGRESS.emit(progress)
            except:
                log.exception('Local Search Error:')
                app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = True  # sanity check
                self.FINISHED.emit(False)
        else:  # if gallery folder is empty
            log_e('Local search error: Invalid directory')
            log_e('Gallery folder is empty')
            app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = True  # sanity check
            self.FINISHED.emit(False)
            # might want to include an error message
        app_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = False
        # everything went well
        log_i('Local search: OK')
        log_i('Created {} items'.format(len(self.data)))
        self.FINISHED.emit(self.data)
        if self.skipped_paths:
            self.SKIPPED.emit(self.skipped_paths)
コード例 #10
0
ファイル: fetch.py プロジェクト: peaceandpizza/happypanda
	def local(self, s_path=None):
		"""
		Do a local search in the given series_path.
		"""
		self.data.clear()
		if s_path:
			self.series_path = s_path
		try:
			gallery_l = sorted([p.name for p in scandir.scandir(self.series_path)]) #list of folders in the "Gallery" folder
			mixed = False
		except TypeError:
			gallery_l = self.series_path
			mixed = True
		if len(gallery_l) != 0: # if gallery path list is not empty
			log_i('Gallery folder is not empty')
			try:
				if len(self.galleries_from_db) != len(gui_constants.GALLERY_DATA):
					self._refresh_filter_list()
				self.DATA_COUNT.emit(len(gallery_l)) #tell model how many items are going to be added
				log_i('Found {} items'.format(len(gallery_l)))
				progress = 0
				def create_gallery(path, folder_name, do_chapters=True, archive=None):
					is_archive = True if archive else False
					temp_p = archive if is_archive else path
					folder_name = folder_name or path if folder_name or path else os.path.split(archive)[1]
					if utils.check_ignore_list(temp_p) and not GalleryDB.check_exists(temp_p, self.galleries_from_db, False):
						log_i('Creating gallery: {}'.format(folder_name.encode('utf-8', 'ignore')))
						new_gallery = Gallery()
						images_paths = []
						try:
							con = scandir.scandir(temp_p) #all of content in the gallery folder
							log_i('Gallery source is a directory')
							chapters = sorted([sub.path for sub in con if sub.is_dir() or sub.name.endswith(utils.ARCHIVE_FILES)])\
							    if do_chapters else [] #subfolders
							# if gallery has chapters divided into sub folders
							if len(chapters) != 0:
								log_i('Gallery has chapters divided in directories')
								for numb, ch in enumerate(chapters):
									chap_path = os.path.join(path, ch)
									new_gallery.chapters[numb] = chap_path

							else: #else assume that all images are in gallery folder
								new_gallery.chapters[0] = path
				
							##find last edited file
							#times = set()
							#for root, dirs, files in os.walk(path, topdown=False):
							#	for img in files:
							#		fp = os.path.join(root, img)
							#		times.add( os.path.getmtime(fp) )
							#last_updated = time.asctime(time.gmtime(max(times)))
							#new_gallery.last_update = last_updated
							parsed = utils.title_parser(folder_name)
						except NotADirectoryError:
							try:
								if is_archive or temp_p.endswith(utils.ARCHIVE_FILES):
									log_i('Gallery source is an archive')
									contents = utils.check_archive(temp_p)
									if contents:
										new_gallery.is_archive = 1
										new_gallery.path_in_archive = '' if not is_archive else path
										if folder_name.endswith('/'):
											folder_name = folder_name[:-1]
											fn = os.path.split(folder_name)
											folder_name = fn[1] or fn[2]
										folder_name = folder_name.replace('/','')
										if folder_name.endswith(utils.ARCHIVE_FILES):
											n = folder_name
											for ext in utils.ARCHIVE_FILES:
												n = n.replace(ext, '')
											parsed = utils.title_parser(n)
										else:
											parsed = utils.title_parser(folder_name)
										if do_chapters:
											archive_g = sorted(contents)
											if not archive_g:
												log_w('No chapters found for {}'.format(temp_p.encode(errors='ignore')))
												raise ValueError
											for n, g in enumerate(archive_g):
												new_gallery.chapters[n] = g
										else:
											new_gallery.chapters[0] = path
									else:
										raise ValueError
								else:
									raise ValueError
							except ValueError:
								log_w('Skipped {} in local search'.format(path.encode(errors='ignore')))
								self.skipped_paths.append(temp_p)
								return

						new_gallery.title = parsed['title']
						new_gallery.path = temp_p
						new_gallery.artist = parsed['artist']
						new_gallery.language = parsed['language']
						new_gallery.info = "No description.."
						if gui_constants.MOVE_IMPORTED_GALLERIES and not gui_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH:
							new_gallery.path = utils.move_files(temp_p)

						self.data.append(new_gallery)
						log_i('Gallery successful created: {}'.format(folder_name.encode('utf-8', 'ignore')))
					else:
						log_i('Gallery already exists: {}'.format(folder_name.encode('utf-8', 'ignore')))
						self.skipped_paths.append(temp_p)

				for folder_name in gallery_l: # folder_name = gallery folder title
					self._curr_gallery = folder_name
					if mixed:
						path = folder_name
						folder_name = os.path.split(path)[1]
					else:
						path = os.path.join(self.series_path, folder_name)
					if gui_constants.SUBFOLDER_AS_GALLERY or gui_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
						if gui_constants.OVERRIDE_SUBFOLDER_AS_GALLERY:
							gui_constants.OVERRIDE_SUBFOLDER_AS_GALLERY = False
						log_i("Treating each subfolder as gallery")
						if os.path.isdir(path):
							gallery_folders, gallery_archives = utils.recursive_gallery_check(path)
							for gs in gallery_folders:
									create_gallery(gs, os.path.split(gs)[1], False)
							p_saving = {}
							for gs in gallery_archives:
									
									create_gallery(gs[0], os.path.split(gs[0])[1], False, archive=gs[1])
						elif path.endswith(utils.ARCHIVE_FILES):
							for g in utils.check_archive(path):
								create_gallery(g, os.path.split(g)[1], False, archive=path)
					else:
						if (os.path.isdir(path) and os.listdir(path)) or path.endswith(utils.ARCHIVE_FILES):
							log_i("Treating each subfolder as chapter")
							create_gallery(path, folder_name, do_chapters=True)
						else:
							self.skipped_paths.append(path)
							log_w('Directory is empty: {}'.format(path.encode(errors='ignore')))

					progress += 1 # update the progress bar
					self.PROGRESS.emit(progress)
			except:
				log.exception('Local Search Error:')
				gui_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = True # sanity check
				self.FINISHED.emit(False)
		else: # if gallery folder is empty
			log_e('Local search error: Invalid directory')
			log_e('Gallery folder is empty')
			gui_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = True # sanity check
			self.FINISHED.emit(False)
			# might want to include an error message
		gui_constants.OVERRIDE_MOVE_IMPORTED_IN_FETCH = False
		# everything went well
		log_i('Local search: OK')
		log_i('Created {} items'.format(len(self.data)))
		self.FINISHED.emit(self.data)
		if self.skipped_paths:
			self.SKIPPED.emit(self.skipped_paths)