From a9cdfe874109fe4e6132a9ea136a43e41a1d5ef1 Mon Sep 17 00:00:00 2001 From: Gergely Wootsch Date: Sun, 8 Jan 2023 19:19:03 +0100 Subject: [PATCH] Add links to jobs This allosd removing the previously used, and clumsy 'job_has_clients' setting. This solution instead utilises our previously defined .links definition to indicate where nested job folders reside. --- bookmarks/bookmarker/bookmark_editor.py | 21 ++++-- bookmarks/bookmarker/job_editor.py | 87 ++++++++++--------------- bookmarks/common/core.py | 4 +- bookmarks/common/settings.py | 1 - bookmarks/editor/preferences.py | 14 ---- 5 files changed, 53 insertions(+), 74 deletions(-) diff --git a/bookmarks/bookmarker/bookmark_editor.py b/bookmarks/bookmarker/bookmark_editor.py index b0508a8c..dfbfc45d 100644 --- a/bookmarks/bookmarker/bookmark_editor.py +++ b/bookmarks/bookmarker/bookmark_editor.py @@ -197,7 +197,9 @@ def toggle_item_state(self, item): @common.error @QtCore.Slot() def add(self, *args, **kwargs): - """Pick and add a folder as a new bookmark item.""" + """Pick and add a folder as a new bookmark item. + + """ if not self.window().server() or not self.window().job(): return @@ -214,8 +216,8 @@ def add(self, *args, **kwargs): if self.window().job_path() not in path: raise RuntimeError('Bookmark item must be inside the current job folder.') - - if not QtCore.QDir(path).mkdir(common.bookmark_cache_dir): + dir = QtCore.QDir(path) + if not dir.exists() and not dir.mkdir(common.bookmark_cache_dir): raise RuntimeError('Could not create bookmark') name = path[len(self.window().job_path()) + 1:] @@ -229,6 +231,11 @@ def add(self, *args, **kwargs): ).open() return + # Add link + if not common.add_link(self.window().job_path(), name, section='links/root'): + log.error('Could not add link') + + # Add the QListWidgetItem item = QtWidgets.QListWidgetItem() item.setFlags( QtCore.Qt.ItemIsEnabled | @@ -317,18 +324,20 @@ def item_generator(self, path, recursion=0, max_recursion=3): if self._interrupt_requested: return - # Return items stored in the link file + # If links exist, return items stored in the link file and nothing else if recursion == 0: - for v in common.get_links(path, section='links/root'): + links = common.get_links(path, section='links/root') + for v in links: if self._interrupt_requested: return yield v, f'{path}/{v}' + # Otherwise parse the folder recursion += 1 if recursion > max_recursion: return - # We'll let unreadable paths fail silently + # Let unreadable paths fail silently try: it = os.scandir(path) except: diff --git a/bookmarks/bookmarker/job_editor.py b/bookmarks/bookmarker/job_editor.py index 3d77819e..593f8efb 100644 --- a/bookmarks/bookmarker/job_editor.py +++ b/bookmarks/bookmarker/job_editor.py @@ -17,6 +17,7 @@ from .. import common from .. import contextmenu from .. import images +from .. import log from .. import shortcuts from .. import templates from .. import ui @@ -119,8 +120,8 @@ def init_data(self): self.jobs = [] items = [] - for name, path in self.parent().job_editor.item_generator(self.server, - emit_progress=False): + it = self.parent().job_editor.item_generator(self.server, emit_progress=False) + for name, path in it: items.append(name) completer = QtWidgets.QCompleter(items, parent=self) @@ -135,31 +136,36 @@ def save_changes(self): """Saves changes. """ - if not self.name_editor.text(): + text = self.name_editor.text() + if not text: raise ValueError('Must enter a name to create a job.') + text = text.replace('\\', '/') root = self.server - name = self.name_editor.text() - if not name: - raise ValueError('Must enter a name to create job') - name = name.replace('\\', '/') - - if '/' in name: - _name = name.split('/')[-1] - _root = name[:-len(_name) - 1] + if '/' in self.name_editor.text(): + _name = text.split('/')[-1] + _root = text[:-len(_name) - 1] name = _name - root = f'{self.server}/{_root}' + root = f'{self.server}/{_root}'.rstrip('/') if not QtCore.QFileInfo(root).exists(): if not QtCore.QDir(root).mkpath('.'): raise RuntimeError('Error creating folders.') + else: + name = text # Create template and signal self.template_editor.template_list_widget.create(name, root) path = f'{root}/{name}' - if not QtCore.QFileInfo(path).exists(): + # Save link + if '/' in text: + if not common.add_link(root, name, section='links/job'): + log.error('Could not add link') + + file_info = QtCore.QFileInfo(path) + if not file_info.exists(): raise RuntimeError('Could not find the added job.') try: @@ -168,7 +174,7 @@ def save_changes(self): except: pass - common.signals.jobAdded.emit(path) + common.signals.jobAdded.emit(file_info.filePath()) ui.MessageBox(f'{name} was successfully created.').open() return True @@ -286,23 +292,17 @@ def item_generator(self, source, emit_progress=True): if emit_progress: self.progressUpdate.emit('') - has_subdir = common.settings.value('settings/jobs_have_clients') - has_subdir = QtCore.Qt.Unchecked if has_subdir is None else \ - QtCore.Qt.CheckState( - has_subdir - ) - has_subdir = has_subdir == QtCore.Qt.Checked - - for client_entry in os.scandir(source): + # Parse source otherwise + for entry in os.scandir(source): if self._interrupt_requested: if emit_progress: self.progressUpdate.emit('') return - if not client_entry.is_dir(): + if not entry.is_dir(): continue - file_info = QtCore.QFileInfo(client_entry.path) + file_info = QtCore.QFileInfo(entry.path) if emit_progress: self.progressUpdate.emit(f'Scanning: {file_info.filePath()}') @@ -310,38 +310,21 @@ def item_generator(self, source, emit_progress=True): continue if not file_info.isReadable(): continue + # Test access try: next(os.scandir(file_info.filePath())) except: continue - if not has_subdir: - yield client_entry.name, file_info.filePath() - continue - - for job_entry in os.scandir(client_entry.path): - if self._interrupt_requested: - if emit_progress: - self.progressUpdate.emit('') - return - - if not job_entry.is_dir(): - continue - - file_info = QtCore.QFileInfo(job_entry.path) - if emit_progress: - self.progressUpdate.emit(f'Scanning: {file_info.filePath()}') - - if file_info.isHidden(): - continue - if not file_info.isReadable(): - continue - try: - next(os.scandir(file_info.filePath())) - except: - continue - yield f'{client_entry.name}/{job_entry.name}', file_info.filePath() - continue + # Use paths in the link file, if available + links = common.get_links(file_info.filePath(), section='links/job') + if links: + for link in links: + _file_info = QtCore.QFileInfo(f'{file_info.filePath()}/{link}') + _name = _file_info.filePath()[len(source) + 1:] + yield _name, _file_info.filePath() + else: + yield entry.name, file_info.filePath() if emit_progress: self.progressUpdate.emit('') @@ -382,7 +365,7 @@ def init_data(self, *args, **kwargs): replace(' ', ' '). strip(). replace('/', ' | '). - strip().upper() + strip() ) item.setData(_name, role=QtCore.Qt.DisplayRole) diff --git a/bookmarks/common/core.py b/bookmarks/common/core.py index ac079e2a..db7c81a3 100644 --- a/bookmarks/common/core.py +++ b/bookmarks/common/core.py @@ -499,7 +499,7 @@ def add_link(path, link, section='links/asset'): path (str): Path to a folder where the link file resides. E.g. an asset root folder. link (str): Relative path to the link file. section (str): - The settings section to look for links in. Defaults to `links/assets`. + The settings section to look for links in. Defaults to `links/asset`. """ l = f'{path}/{common.link_file}' @@ -508,6 +508,8 @@ def add_link(path, link, section='links/asset'): links = [] if QtCore.QFileInfo(l).exists(): links = s.value(section) + if links and not isinstance(links, (list, tuple)): + links = [links,] links = links if links else [] # Make sure the link points to a valid file diff --git a/bookmarks/common/settings.py b/bookmarks/common/settings.py index c08c2a06..40cf85d6 100644 --- a/bookmarks/common/settings.py +++ b/bookmarks/common/settings.py @@ -30,7 +30,6 @@ 'user/favourites', ), 'settings': ( - 'settings/jobs_have_clients', 'settings/job_scan_depth', 'settings/ui_scale', 'settings/show_menu_icons', diff --git a/bookmarks/editor/preferences.py b/bookmarks/editor/preferences.py index 02b6fa5b..5525f1a3 100644 --- a/bookmarks/editor/preferences.py +++ b/bookmarks/editor/preferences.py @@ -123,20 +123,6 @@ def init_data(self): }, 1: { 0: { - 'name': 'Jobs have clients', - 'key': 'settings/jobs_have_clients', - 'validator': None, - 'widget': functools.partial(QtWidgets.QCheckBox, 'Enable'), - 'placeholder': '', - 'description': 'In case your job folder uses a client/project like ' - 'structure, tick this box. Leave it un-ticked if the ' - 'project folders are nested directly in the server' - 'folder.', - 'help': 'Enable if jobs have separate client/project folders. By ' - 'default, Bookmarks assumes jobs are kept directly in the ' - 'root of the server folder but you can override this here.' - }, - 1: { 'name': 'Bookmark item search depth', 'key': 'settings/job_scan_depth', 'validator': base.int_validator,