Skip to content

Commit

Permalink
Add links to jobs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wgergely committed Jan 8, 2023
1 parent 8e38b83 commit a9cdfe8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 74 deletions.
21 changes: 15 additions & 6 deletions bookmarks/bookmarker/bookmark_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:]
Expand All @@ -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 |
Expand Down Expand Up @@ -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:
Expand Down
87 changes: 35 additions & 52 deletions bookmarks/bookmarker/job_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -286,62 +292,39 @@ 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()}')

if file_info.isHidden():
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('')
Expand Down Expand Up @@ -382,7 +365,7 @@ def init_data(self, *args, **kwargs):
replace(' ', ' ').
strip().
replace('/', ' | ').
strip().upper()
strip()
)

item.setData(_name, role=QtCore.Qt.DisplayRole)
Expand Down
4 changes: 3 additions & 1 deletion bookmarks/common/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion bookmarks/common/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
'user/favourites',
),
'settings': (
'settings/jobs_have_clients',
'settings/job_scan_depth',
'settings/ui_scale',
'settings/show_menu_icons',
Expand Down
14 changes: 0 additions & 14 deletions bookmarks/editor/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <span style="color:white">client/project</span> 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,
Expand Down

0 comments on commit a9cdfe8

Please sign in to comment.