Snippets

ftrack Event listener - Print update event data

Created by Lucas Correia

File print_update_events.py Added

  • Ignore whitespace
  • Hide word diff
+# :coding: utf-8
+# :copyright: Copyright (c) 2017 ftrack
+# 
+# This snippet was last tested with ftrack 3.5.2 and Python API 1.1.1
+# 
+import logging
+import pprint
+
+import ftrack_api
+
+logger = logging.getLogger(__file__)
+
+
+def print_header(title='', char='=', length=80, new_line=False):
+    '''Print formatted header.'''
+    if new_line:
+        print ''
+    if title:
+        num_chars_right = length - len(title) - 2
+        print char, title, num_chars_right*char
+    else:
+        print length*char
+
+
+def print_events(event):
+    '''Event callback printing all new or updated entities.'''
+    print_header('ftrack.update', new_line=True)
+
+    source = event['source']
+    print_header('Source')
+    pprint.pprint(source)
+
+    print_header('Data')
+    for entity in event['data'].get('entities', []):
+        pprint.pprint(entity)
+        print_header(char='-')
+
+        #TODO: Do something with the updated entities
+
+
+def register(session, **kw):
+    '''Register event listener.'''
+
+    # Validate that session is an instance of ftrack_api.Session. If not,
+    # assume that register is being called from an incompatible API
+    # and return without doing anything.
+    if not isinstance(session, ftrack_api.Session):
+        # Exit to avoid registering this plugin again.
+        return
+
+    # Register the event handler
+    session.event_hub.subscribe('topic=ftrack.update', print_events)
+    logger.info('Registered event handler')
+
+
+if __name__ == '__main__':
+    '''Register and wait for events when called as script'''
+    logging.basicConfig(level=logging.INFO)
+    logging.getLogger('ftrack_api').setLevel(level=logging.WARN)
+    logging.getLogger('requests').setLevel(level=logging.WARN)
+    session = ftrack_api.Session()
+    register(session)
+
+    # Wait for events.
+    session.event_hub.wait()
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.