+class MemoryAction(object):
+ identifier = 'MemoryAction'
+ def __init__(self, session):
+ super(MemoryAction, self).__init__()
+ self.session.event_hub.subscribe(
+ 'topic=ftrack.action.discover',
+ self.session.event_hub.subscribe(
+ 'topic=ftrack.action.launch and data.actionIdentifier={0}'.format(
+ def discover(self, event):
+ 'actionIdentifier':self.identifier
+ def _translate_event(self, session, event):
+ '''Return *event* translated structure to be used with the API.'''
+ _selection = event['data'].get('selection', [])
+ for entity in _selection:
+ self._get_entity_type(entity), entity.get('entityId')
+ def _get_entity_type(self, entity):
+ '''Return translated entity type tht can be used with API.'''
+ # Get entity type and make sure it is lower cased. Most places except
+ # the component tab in the Sidebar will use lower case notation.
+ entity_type = entity.get('entityType').replace('_', '').lower()
+ for schema in self.session.schemas:
+ alias_for = schema.get('alias_for')
+ for schema in self.session.schemas:
+ alias_for = schema.get('alias_for')
+ alias_for and isinstance(alias_for, basestring) and
+ alias_for.lower() == entity_type
+ for schema in self.session.schemas:
+ if schema['id'].lower() == entity_type:
+ 'Unable to translate entity type: {0}.'.format(entity_type)
+ def launch(self, event):
+ '''Callback method for custom action.'''
+ entities, _event = self._translate_event(
+ for entity_type, entity_id in entities:
+ 'got entities : {0}'.format(
+ 'entities in cache : {0}'.format(
+ len(self.session.cache.caches[0].keys())
+ 'memory information launch: {0}'.format(
+ psutil.Process().memory_full_info()
+ format='%(funcName)20s() ] %(message)s'
+ session = ftrack_api.Session()
+ action = MemoryAction(session)
+ session.event_hub.wait()
+if __name__ == '__main__':