Skip to content

Commit

Permalink
Add Update button for APIs on NukeAPICache node
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Einfalt committed Jun 28, 2018
1 parent af80890 commit 85ac6d9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
45 changes: 42 additions & 3 deletions nukedatastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ def import_nuke():

DS_PREFIX = 'ds_'
FROZEN_ATTR = 'nds_frozen'
UPDATE_CMD = r"""import nukedatastore
api_cache = nukedatastore.NukeAPICache(nuke.thisNode().name())
for api in api_cache.list():
diff = api_cache.diff(api)
if diff[api]:
result = nuke.ask('API {api} has the following changes on the server:\n\n{diff}\n\nUpdate {api}?'.format(api=api, diff=diff))
if result:
api_cache.update(api)
else:
continue"""


class NukeDataStoreError(ValueError):
Expand Down Expand Up @@ -68,6 +80,15 @@ class NukeDataStore(object):
>>> {'id': 1234, 'name': 'project name'}
"""
def __init__(self, name):
self._create_store(name)

def _create_store(self, name):
"""
Given a ``name``, create a data store
:param name: Data store name
:type name: str
"""
self.store = self._init(name)

@property
Expand Down Expand Up @@ -98,7 +119,7 @@ def store(self, node):
raise NukeDataStoreError('Data store must be a Nuke node')
self._store = node

def _init(self, name):
def _init(self, name, api_cache=False):
"""
Given a ``name``, initialise a :class:`~nukedatastore.NukeDataStore`
with the same ``name``. Find existing
Expand All @@ -107,6 +128,8 @@ def _init(self, name):
:param node: Node name
:type node: str
:param api_cache: Whether the data store is an API cache
:type api_cache: bool
:return: Data store node
:rtype: :class:`~nuke.Node`
"""
Expand All @@ -128,8 +151,15 @@ def _init(self, name):
store = nuke.nodes.NoOp(name=name)
set_uuid(store, **kw)
store.addKnob(self._create_knob(FROZEN_ATTR))
for attr, value in attrs.iteritems():
store[attr].setValue(value)
for attr, value in attrs.iteritems():
store[attr].setValue(value)
if api_cache:
actions_tab = nuke.Tab_Knob('actions', 'Actions')
update_btn = nuke.PyScript_Knob('update', 'Update APIs')
update_btn.setCommand(UPDATE_CMD)
update_btn.setFlag(nuke.STARTLINE)
store.addKnob(actions_tab)
store.addKnob(update_btn)
return store

def _create_knob(self, attr):
Expand Down Expand Up @@ -305,6 +335,15 @@ class NukeAPICache(NukeDataStore):
def __init__(self, name):
super(NukeAPICache, self).__init__(name)

def _create_store(self, name):
"""
Given a ``name``, create a data store
:param name: Data store name
:type name: str
"""
self.store = self._init(name, api_cache=True)

def __setitem__(self, key, value):
raise NotImplementedError('Please update API cache instead of trying '
'to set the data manually, use '
Expand Down
6 changes: 2 additions & 4 deletions tests/test_nukeapicache.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ def test_api_cache_crud_frozen(api_cache):
api_cache.unfreeze()


def test_api_chache_diff(api_cache):
def test_api_cache_diff(api_cache):
diff = api_cache.diff()
assert diff
print diff
assert 'project_data' in diff
assert 'values_changed' in diff['project_data']
assert 'root[\'headers\'][\'X-Request-Id\']' in diff['project_data']['values_changed']

assert 'values_changed' not in diff['project_data']

def test_api_cache_diff_invalid(api_cache):
temp_cache = NukeAPICache('temp_cache')
Expand Down

0 comments on commit 85ac6d9

Please sign in to comment.