Skip to content

Commit

Permalink
Update active mode to respect environment values
Browse files Browse the repository at this point in the history
    I introduced recently environment values for subprocesses launched via
    Bookmarks. ``BOOKMARKS_ACTIVE_SERVER``, ``BOOKMARKS_ACTIVE_JOB``,
    ``BOOKMARKS_ACTIVE_ROOT``, ``BOOKMARKS_ACTIVE_ASSET`` and ``BOOKMARKS_ACTIVE_TASK``
    will be used to set the default active paths in any subsequent Bookmarks session
    and Bookmarks will automatically be put in private active path mode when any of
    these environments are set.
  • Loading branch information
wgergely committed Oct 25, 2023
1 parent a310020 commit 337daf2
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<img src="https://img.shields.io/badge/Python-3.8%2B-lightgrey">
<img src="https://img.shields.io/badge/Python-PySide2-lightgrey">
<img src="https://img.shields.io/badge/Platform-Windows-lightgrey">
<img src="https://img.shields.io/badge/Version-v0.8.6-green">
<img src="https://img.shields.io/badge/Version-v0.8.9-green">
</p>

<p align="center">
Expand Down
4 changes: 2 additions & 2 deletions bookmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
.. |label3| image:: https://img.shields.io/badge/Platform-Windows-lightgrey
:height: 18
.. |label4| image:: https://img.shields.io/badge/Version-v0.8.6-green
.. |label4| image:: https://img.shields.io/badge/Version-v0.8.9-green
:height: 18
.. |image1| image:: ./images/active_bookmark.png
Expand All @@ -100,7 +100,7 @@
__email__ = 'hello@gergely-wootsch.com'

#: Project version
__version__ = '0.8.6'
__version__ = '0.8.9'

#: Project version
__version_info__ = __version__.split('.')
Expand Down
2 changes: 1 addition & 1 deletion bookmarks/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
from .font import *
from .monitor import *
from .sequence import *
from .session_lock import *
from .active_mode import *
from .settings import *
from .setup import *
from .signals import *
Expand Down
42 changes: 20 additions & 22 deletions bookmarks/common/active_mode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Module defines the classes and methods needed to set and edit session lock
files.
"""Defines active path reading mode.
The app has two session modes. When `common.active_mode` is
`common.SynchronisedActivePaths`, the app will save active paths in the user settings
Expand All @@ -11,6 +10,11 @@
active path values are read from the user settings, but active paths changes won't be
saved to the settings file.
The session mode will also be set to `common.PrivateActivePaths` if any of the
`BOOKMARKS_ACTIVE_SERVER`, `BOOKMARKS_ACTIVE_JOB`, `BOOKMARKS_ACTIVE_ROOT`,
`BOOKMARKS_ACTIVE_ASSET` and `BOOKMARKS_ACTIVE_TASK` environment variables are set
as these will take precedence over the user settings.
To toggle between the two modes use :func:`bookmarks.actions.toggle_active_mode`. Also see
:class:`bookmarks.statusbar.ToggleSessionModeButton`.
Expand All @@ -34,11 +38,7 @@ def get_lock_path():
return LOCK_PATH.format(
root=QtCore.QStandardPaths.writableLocation(
QtCore.QStandardPaths.GenericDataLocation
),
product=common.product,
prefix=PREFIX,
pid=os.getpid(),
ext=FORMAT
), product=common.product, prefix=PREFIX, pid=os.getpid(), ext=FORMAT
)


Expand All @@ -49,9 +49,7 @@ def prune_lock():
path = LOCK_DIR.format(
root=QtCore.QStandardPaths.writableLocation(
QtCore.QStandardPaths.GenericDataLocation
),
product=common.product,
)
), product=common.product, )

r = fr'{PREFIX}_([0-9]+)\.{FORMAT}'
pids = psutil.pids()
Expand Down Expand Up @@ -79,21 +77,23 @@ def init_active_mode():
``PrivateActivePaths`` when the Bookmarks sessions set the active paths values internally without changing the user
settings.
The session mode can be toggled via the ui (there's a button in the lower right hand corner) and will be initialised
to a default value based on the following conditions:
The session mode will be initialised to a default value based on the following conditions:
If any of the `BOOKMARKS_ACTIVE_SERVER`, `BOOKMARKS_ACTIVE_JOB`, `BOOKMARKS_ACTIVE_ROOT`, `BOOKMARKS_ACTIVE_ASSET`
and `BOOKMARKS_ACTIVE_TASK` environment values have valid values, the session will automatically be marked
``PrivateActivePaths``.
If any of the `BOOKMARKS_ACTIVE_SERVER`, `BOOKMARKS_ACTIVE_JOB`, `BOOKMARKS_ACTIVE_ROOT`,
`BOOKMARKS_ACTIVE_ASSET` and `BOOKMARKS_ACTIVE_TASK` environment values have valid values, the session will
automatically be marked ``PrivateActivePaths``.
If the environment has not been set but there's already an active ``SynchronisedActivePaths`` session running the
current session will be set to ``PrivateActivePaths``.
If the environment has not been set but there's already an active ``SynchronisedActivePaths`` session
running, the current session will be set to ``PrivateActivePaths``.
Any sessions that doesn't have environment values set and does not find synchronized session lock files will be
marked ``SynchronisedActivePaths``.
Any sessions that doesn't have environment values set and does not find synchronized session lock files will
be marked ``SynchronisedActivePaths``.
"""
# Remove stale lock files
prune_lock()

# Check if any of the environment variables are set
_env_active_server = os.environ.get('BOOKMARKS_ACTIVE_SERVER', None)
_env_active_job = os.environ.get('BOOKMARKS_ACTIVE_JOB', None)
Expand All @@ -108,9 +108,7 @@ def init_active_mode():
path = LOCK_DIR.format(
root=QtCore.QStandardPaths.writableLocation(
QtCore.QStandardPaths.GenericDataLocation
),
product=common.product,
)
), product=common.product, )

# Iterate over all lock files and check their contents
for entry in os.scandir(path):
Expand Down
48 changes: 32 additions & 16 deletions bookmarks/common/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ def init_settings():
for key in SECTIONS['active']:
common.active_paths[mode][key] = None

# Create the setting object, this will load the previously saved active
# paths from the ini file.
# Initialize the user settings instance
common.settings = UserSettings()

# Load values from the user settings file
common.settings.load_active_values()
common.update_private_values()

Expand All @@ -155,6 +156,7 @@ def init_settings():
if not v or not isinstance(v, dict):
v = {}
common.favourites = v

common.signals.favouritesChanged.emit()

_init_bookmarks()
Expand Down Expand Up @@ -306,12 +308,26 @@ def bookmark_key(server, job, root):


def update_private_values():
"""Copy the ``SynchronisedActivePaths`` values to ``PrivateActivePaths``.
"""Copy the controlling values to ``PrivateActivePaths``.
The source of the value is determined by the active mode and the current environment.
"""
_env_active_server = os.environ.get('BOOKMARKS_ACTIVE_SERVER', None)
_env_active_job = os.environ.get('BOOKMARKS_ACTIVE_JOB', None)
_env_active_root = os.environ.get('BOOKMARKS_ACTIVE_ROOT', None)
_env_active_asset = os.environ.get('BOOKMARKS_ACTIVE_ASSET', None)
_env_active_task = os.environ.get('BOOKMARKS_ACTIVE_TASK', None)

if any((_env_active_server, _env_active_job, _env_active_root, _env_active_asset, _env_active_task)):
for k in SECTIONS['active']:
common.active_paths[PrivateActivePaths][k] = os.environ.get(f'BOOKMARKS_ACTIVE_{k.upper()}', None)

for k in SECTIONS['active']:
common.active_paths[PrivateActivePaths][k] = \
common.active_paths[SynchronisedActivePaths][k]
common.active_paths[PrivateActivePaths][k] = common.active_paths[SynchronisedActivePaths][k]

# Verify the values
common.settings.verify_active(PrivateActivePaths)


_true = {'True', 'true', '1', True}
Expand All @@ -338,10 +354,11 @@ def __init__(self, parent=None):
self.verify_timer.timeout.connect(self.load_active_values)

def load_active_values(self):
"""Load previously saved active path elements from the settings file.
"""Load active path elements from the settings file.
If the resulting path is invalid, we'll progressively unset the invalid
path segments until we find a valid path.
Whilst the function will load the current values from the settings file,
it doesn't guarantee the values will actually be used. App will only use
these values if the active mode is set to `SynchronisedActivePaths`.
"""
self.sync()
Expand All @@ -351,22 +368,21 @@ def load_active_values(self):
v = None
common.active_paths[SynchronisedActivePaths][k] = v
self.verify_active(SynchronisedActivePaths)
self.verify_active(PrivateActivePaths)

def verify_active(self, m):
def verify_active(self, active_mode):
"""Verify the active path values and unset any item, that refers to an invalid path.
Args:
m (int): The active mode.
active_mode (int): One of ``SynchronisedActivePaths`` or ``Pr.
"""
p = str()
for k in SECTIONS['active']:
if common.active_paths[m][k]:
p += common.active_paths[m][k]
if common.active_paths[active_mode][k]:
p += common.active_paths[active_mode][k]
if not os.path.exists(p):
common.active_paths[m][k] = None
if m == SynchronisedActivePaths:
common.active_paths[active_mode][k] = None
if active_mode == SynchronisedActivePaths:
self.setValue(f'active/{k}', None)
p += '/'

Expand Down Expand Up @@ -452,7 +468,7 @@ def setValue(self, key, v):
v (object): The value to save.
"""
# Skip saving active values when PrivateActivePaths is on
# We don't want to save private active values to the user settings
if common.active_mode == PrivateActivePaths and key in SECTIONS['active']:
return

Expand Down
22 changes: 20 additions & 2 deletions bookmarks/common/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def initialize(mode):
os.makedirs(os.path.normpath(common.temp_path()))

common.init_signals()
common.prune_lock()
common.init_lock() # Sets the current active mode
common.init_active_mode()
common.init_settings()

_init_ui_scale()
Expand Down Expand Up @@ -120,6 +119,25 @@ def initialize(mode):
break


def initialize_core():
""""""
if common.init_mode is not None:
raise RuntimeError(f'Already initialized as "{common.init_mode}"!')

common.init_mode = common.EmbeddedMode

_init_config()

common.item_data = common.DataDict()

if not os.path.isdir(common.temp_path()):
os.makedirs(os.path.normpath(common.temp_path()))

common.init_signals(connect_signals=False)
common.init_active_mode()
common.init_settings()


def uninitialize():
"""Un-initializes all app components.
Expand Down
9 changes: 6 additions & 3 deletions bookmarks/common/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from .. import common


def init_signals():
def init_signals(connect_signals=True):
"""Initialize signals."""
common.signals = CoreSignals()
common.signals = CoreSignals(connect_signals=connect_signals)


class CoreSignals(QtCore.QObject):
Expand Down Expand Up @@ -132,9 +132,12 @@ class CoreSignals(QtCore.QObject):
#: Signals an item is ready to be processed by a thread
threadItemsQueued = QtCore.Signal()

def __init__(self, parent=None):
def __init__(self, connect_signals=True, parent=None):
super().__init__(parent=parent)

if not connect_signals:
return

from .. import actions

self.toggleFilterButton.connect(actions.toggle_filter_editor)
Expand Down
2 changes: 1 addition & 1 deletion bookmarks/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def get_row(self, source, table):
Args:
source (str): A source file path.
table (str): A database 01table name.
table (str): A database table name.
Returns:
dict: A dictionary of column/value pairs.
Expand Down
4 changes: 0 additions & 4 deletions bookmarks/items/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,10 +1024,6 @@ def filterAcceptsRow(self, idx, parent=None):
if not ref():
return False

# Task item specific filter
if '#empty#' in ref()[idx][common.DescriptionRole]:
return False

filter_text = self.filter_text()
if filter_text:
filter_text = filter_text.strip().lower()
Expand Down
2 changes: 1 addition & 1 deletion bookmarks/maya/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def show():

def init_maya_widget():
"""Initializes the maya widget.
Usually the Maya plugin will call this method.
Usually the Maya plugin will call this function.
"""
if isinstance(common.maya_widget, MayaWidget):
Expand Down
2 changes: 1 addition & 1 deletion bookmarks/maya/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
product = 'bookmarks'

__author__ = 'Gergely Wootsch'
__version__ = '0.8.6'
__version__ = '0.8.9'

maya_useNewAPI = True

Expand Down
9 changes: 2 additions & 7 deletions bookmarks/threads/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,6 @@ def queue_model(self, data_type_ref1, data_type_ref2):
if ref().loaded:
continue # Skip if the model is loaded already

# Skip if the model has already been queued
if ref in threads.THREADS[q]['queue']:
continue

idxs = ref().keys()
for idx in idxs:
if not ref() or self.interrupt:
Expand Down Expand Up @@ -698,12 +694,11 @@ def _file_it(path):
break

_suffix = f'{_idx + 1} items' if _idx < _max else f'{_max}+ items'
_suffix = _suffix if _idx > 0 else '#empty#'
_suffix = f' ({_suffix})' if description else _suffix
_suffix = _suffix if _idx > 0 else ''
_suffix = f' ({_suffix})' if description and _idx > 0 else _suffix

ref()[common.DescriptionRole] += _suffix


self._process_bookmark_item(ref, db.source(), bookmark_row_data, pp)
self._process_file_item(ref, item_type)
self._process_sequence_item(ref, item_type)
Expand Down
1 change: 1 addition & 0 deletions bookmarks/topbar/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def __init__(self, Model, tab_index, parent=None):
self.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContents)
self.setFixedHeight(common.size(common.size_margin))
self.setMinimumWidth(common.size(common.size_margin) * 3)
self.setMaxVisibleItems(48)

min_width = self.minimumSizeHint().width()
self.view().setMinimumWidth(min_width * 3)
Expand Down
2 changes: 1 addition & 1 deletion package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20)

project(
Bookmarks
VERSION 0.8.6
VERSION 0.8.9
DESCRIPTION "Bookmarks is a lightweight asset manager for digital artists working in the animation, motion graphics and VFX industries."
HOMEPAGE_URL "https://bookmarks-vfx.com"
)
Expand Down

0 comments on commit 337daf2

Please sign in to comment.