commands get executed on mutiple subscribers via actions
0

5 posts in this topic

Hi guys,

I'm writing an action that opens the file path for a selected entity. 

When I launch the action, it acutually opens the file exploerer on every computer which is running ftrack connect.

How can I restrict the command only excutes on the local caller?

Thank you for your help!

 

 

My code -----------------------------------------------------------------------------------------------------------------------------------

def register(self):
    self.session.event_hub.subscribe('topic=ftrack.action.discover',
                                     self.discover)

    self.session.event_hub.subscribe('topic=ftrack.action.launch and data.actionIdentifier={0}'.format(self.identifier),
                                     self.launch)

def discover(self, event):
    # Check ftrack user == system user
    connect_user = event['source']['user']['username'].split('@')[0]
    sys_user = getuser().split('@')[0]

    if connect_user != sys_user:
        return

    selection = event['data']['selection']
    if len(selection) != 1:
        return

    return {'items': [{'label': self.label,
                       'description': self.description,
                       'actionIdentifier': self.identifier}]}

def launch(self, event):
    path = utils.get_entity_root(self.entity)
    if os.path.exists(path):
        webbrowser.open(path)
        os.system('xdg-open "%s"' % path)
        return {'success': True,
                'message': 'Success'}
    else:
        return {'success': False,
                'message': 'No such directory.'}

Share this post


Link to post
Share on other sites

When you register the launch event add the source.user.username so that the action only launches for that user.

self.session.event_hub.subscribe(
      'topic=ftrack.action.launch and source.user.username={0} '
      'and data.actionIdentifier={1}'.format(
      getpass.getuser(), self.identifier
      ),
      self.launch
)

 

Share this post


Link to post
Share on other sites

Hi there,

This would however not be complete if the same user launch program from a different machine...

I can't find anything in the event['source'] which would allow me to filter event to be run on a specific machine.

Is there a way to do this ?

cheers,

 

 

Share this post


Link to post
Share on other sites

And of course you might even have multiple copies of the same listener running on a single machine ;-)

Generally you need to include extra information in the returned action description and filter against it in your subscribe expression.

One option is to make use of the 'variant' key as it presents nicely to the user. Something like (untested):

self.variant = '{} ({})'.format(socket.getfqdn(), os.getpid())

return {
    'items': [
        {
            'label': self.label,
            'variant': self.variant,
            'description': self.description,
            'actionIdentifier': self.identifier
        }
    ]
}

And filter on variant in your subscribe expression:

self.session.event_hub.subscribe(
    'topic=ftrack.action.launch and '
    'data.actionIdentifier={0} and '
    'data.variant="{1}"'
    .format(self.identifier, self.variant), 
    self.launch)
)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
0