-
Notifications
You must be signed in to change notification settings - Fork 49
Merge Context Manager + Work Files (Fix #412) #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
6ae6742
ecf15b1
94b317e
5d0b923
0c9a5c2
8d84544
eb5f152
b76a1ae
7b8499d
cef2880
7ad657a
af1917f
bb05974
051cb99
238809c
c3d0b7d
e60e306
d713043
9bc0164
83617b6
2f4aa80
3d7c65e
0aa7781
0617307
b7b95fd
050f442
ff30979
acf5886
ee1ae65
be07276
bc8ef09
78be136
63e7d4c
48c362d
43b60c1
297824d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -926,61 +926,103 @@ def get_representation_context(representation): | |
return context | ||
|
||
|
||
def update_current_task(task=None, asset=None, app=None): | ||
"""Update active Session to a new task work area. | ||
def compute_session_changes(session, task=None, asset=None, app=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've made this a new function to compute the changes to the current session required to become a new valid session. Before we only had We might want to come up with a better name and maybe expose it in the API? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or not expose If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And maybe need to add one more internal arg called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I'm not too big a fan of adding the |
||
"""Compute the changes for a Session object on asset, task or app switch | ||
|
||
This updates the live Session to a different `asset`, `task` or `app`. | ||
This does *NOT* update the Session object, but returns the changes | ||
required for a valid update of the Session. | ||
|
||
Args: | ||
task (str): The task to set. | ||
asset (str): The asset to set. | ||
app (str): The app to set. | ||
session (dict): The initial session to compute changes to. | ||
This is required for computing the full Work Directory, as that | ||
also depends on the values that haven't changed. | ||
task (str, Optional): Name of task to switch to. | ||
asset (str or dict, Optional): Name of asset to switch to. | ||
You can also directly provide the Asset dictionary as returned | ||
from the database to avoid an additional query. (optimization) | ||
app (str, Optional): Name of app to switch to. | ||
|
||
Returns: | ||
dict: The changed key, values in the current Session. | ||
dict: The required changes in the Session dictionary. | ||
|
||
""" | ||
|
||
mapping = { | ||
"AVALON_ASSET": asset, | ||
"AVALON_TASK": task, | ||
"AVALON_APP": app, | ||
} | ||
changed = {key: value for key, value in mapping.items() if value} | ||
if not changed: | ||
return | ||
changes = dict() | ||
|
||
# Update silo when asset changed | ||
if "AVALON_ASSET" in changed: | ||
asset_document = io.find_one({"name": changed["AVALON_ASSET"], | ||
"type": "asset"}) | ||
assert asset_document, "Asset must exist" | ||
changed["AVALON_SILO"] = asset_document["silo"] | ||
# If no changes, return directly | ||
if not any([task, asset, app]): | ||
return changes | ||
|
||
if task: | ||
changes["AVALON_TASK"] = task | ||
|
||
if app: | ||
changes["AVALON_APP"] = app | ||
|
||
# Update silo and hierarchy when asset changed | ||
if asset: | ||
if isinstance(asset, dict): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has now additionally been allowed to be passed a database document for an asset as opposed to only working by its name. This allows to avoid the additional query to the database. |
||
# Assume database document | ||
asset_document = asset | ||
asset = asset["name"] | ||
else: | ||
asset_document = io.find_one({"name": asset, | ||
"type": "asset"}) | ||
assert asset_document, "Asset must exist" | ||
|
||
changes["AVALON_ASSET"] = asset | ||
|
||
# Update silo | ||
changes["AVALON_SILO"] = asset_document["silo"] | ||
|
||
# Update hierarchy | ||
parents = asset_document['data'].get('parents', []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code has moved and fixes a BUG where previously if asset had not changed with the call then |
||
hierarchy = "" | ||
if len(parents) > 0: | ||
hierarchy = os.path.sep.join(parents) | ||
changes['AVALON_HIERARCHY'] = hierarchy | ||
|
||
# Compute work directory (with the temporary changed session so far) | ||
project = io.find_one({"type": "project"}, | ||
projection={"config.template.work": True}) | ||
template = project["config"]["template"]["work"] | ||
Comment on lines
994
to
996
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be worth checking whether we might want to cache this call to project because usually as you're working in a project the Work files template should be a static concept (not change during the project runs) and since we're unable to change project in-app this means this template would never change. Maybe we can cache it on the first ever call. (Optimization only) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, maybe cache it on App launch. |
||
_session = Session.copy() | ||
_session.update(changed) | ||
changed["AVALON_WORKDIR"] = _format_work_template(template, _session) | ||
_session = session.copy() | ||
_session.update(changes) | ||
changes["AVALON_WORKDIR"] = _format_work_template(template, _session) | ||
|
||
return changes | ||
|
||
|
||
def update_current_task(task=None, asset=None, app=None): | ||
"""Update active Session to a new task work area. | ||
|
||
This updates the live Session to a different `asset`, `task` or `app`. | ||
|
||
Args: | ||
task (str): The task to set. | ||
asset (str): The asset to set. | ||
app (str): The app to set. | ||
|
||
Returns: | ||
dict: The changed key, values in the current Session. | ||
|
||
""" | ||
|
||
parents = asset_document['data'].get('parents', []) | ||
hierarchy = "" | ||
if len(parents) > 0: | ||
hierarchy = os.path.sep.join(parents) | ||
changed['AVALON_HIERARCHY'] = hierarchy | ||
changes = compute_session_changes(Session, | ||
task=task, | ||
asset=asset, | ||
app=app) | ||
|
||
# Update the full session in one go to avoid half updates | ||
Session.update(changed) | ||
Session.update(changes) | ||
|
||
# Update the environment | ||
os.environ.update(changed) | ||
os.environ.update(changes) | ||
|
||
# Emit session change | ||
emit("taskChanged", changed.copy()) | ||
emit("taskChanged", changes.copy()) | ||
|
||
return changed | ||
return changes | ||
|
||
|
||
def _format_work_template(template, session=None): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,11 @@ def get_active_asset(self): | |
current = self.view.currentIndex() | ||
return current.data(self.model.ObjectIdRole) | ||
|
||
def get_active_asset_document(self): | ||
"""Return the asset id the current asset.""" | ||
current = self.view.currentIndex() | ||
return current.data(self.model.DocumentRole) | ||
Comment on lines
+123
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Solely added this to make it very trivial to retrieve the currently active asset's document as opposed to its name. |
||
|
||
def get_active_index(self): | ||
return self.view.currentIndex() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each Work files API now requires
session
for thework_root
to be able to compute the Work Root for a non-active Session object (e.g. notapi.Session
)