Skip to content

ayon_ftrack

FtrackAddon

Bases: AYONAddon, ITrayAddon, IPluginPaths

Source code in client/ayon_ftrack/ftrack_addon.py
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
class FtrackAddon(
    AYONAddon,
    ITrayAddon,
    IPluginPaths,
):
    name = "ftrack"
    version = __version__

    def initialize(self, settings):
        ftrack_settings = settings[self.name]

        self._settings_ftrack_url = ftrack_settings["ftrack_server"]
        self._ftrack_url = _URL_NOT_SET

        current_dir = os.path.dirname(os.path.abspath(__file__))

        # User event handler paths
        user_event_handlers_paths = [
            os.path.join(current_dir, "event_handlers_user")
        ]

        # Prepare attribute
        self.user_event_handlers_paths = user_event_handlers_paths
        self._tray_wrapper = None

        # TimersManager connection
        self.timers_manager_connector = None
        self._timers_manager_addon = None

    def webserver_initialization(self, web_manager):
        self._tray_wrapper.webserver_initialization(web_manager)

    def get_ftrack_url(self):
        """Resolved ftrack url.

        Resolving is trying to fill missing information in url and tried to
        connect to the server.

        Returns:
            Union[str, None]: Final variant of url or None if url could not be
                reached.
        """

        if self._ftrack_url is _URL_NOT_SET:
            self._ftrack_url = resolve_ftrack_url(
                self._settings_ftrack_url,
                logger=self.log
            )
        return self._ftrack_url

    ftrack_url = property(get_ftrack_url)

    @property
    def settings_ftrack_url(self):
        """ftrack url from settings in a format as it is.

        Returns:
            str: ftrack url from settings.
        """

        return self._settings_ftrack_url

    def get_global_environments(self):
        """ftrack's global environments."""

        return {
            "FTRACK_SERVER": self.ftrack_url
        }

    def get_plugin_paths(self):
        """ftrack plugin paths."""
        plugins_dir = os.path.join(FTRACK_ADDON_DIR, "plugins")
        return {
            "actions": [os.path.join(plugins_dir, "launcher_actions")],
            "publish": [os.path.join(plugins_dir, "publish")]
        }

    def get_launch_hook_paths(self):
        """Implementation for applications launch hooks."""

        return os.path.join(FTRACK_ADDON_DIR, "launch_hooks")

    def connect_with_addons(self, enabled_addons):
        for addon in enabled_addons:
            if not hasattr(addon, "get_ftrack_event_handler_paths"):
                continue

            try:
                paths_by_type = addon.get_ftrack_event_handler_paths()
            except Exception:
                continue

            if not isinstance(paths_by_type, dict):
                continue

            for key, value in paths_by_type.items():
                if not value:
                    continue

                if key not in ("server", "user"):
                    self.log.warning(
                        "Unknown event handlers key \"{}\" skipping.".format(
                            key
                        )
                    )
                    continue

                if not isinstance(value, (list, tuple, set)):
                    value = [value]

                if key == "user":
                    self.user_event_handlers_paths.extend(value)

    def create_ftrack_session(self, **session_kwargs):
        import ftrack_api

        if "server_url" not in session_kwargs:
            session_kwargs["server_url"] = self.ftrack_url

        api_key = session_kwargs.get("api_key")
        api_user = session_kwargs.get("api_user")
        # First look into environments
        # - both AYON tray and ftrack event server should have set them
        # - ftrack event server may crash when credentials are tried to load
        #   from keyring
        if not api_key or not api_user:
            api_key = os.environ.get("FTRACK_API_KEY")
            api_user = os.environ.get("FTRACK_API_USER")

        if not api_key or not api_user:
            cred = get_credentials()
            api_user = cred.get("username")
            api_key = cred.get("api_key")

        session_kwargs["api_user"] = api_user
        session_kwargs["api_key"] = api_key
        return ftrack_api.Session(**session_kwargs)

    def tray_init(self):
        from .tray import FtrackTrayWrapper

        self._tray_wrapper = FtrackTrayWrapper(self)
        # Addon is it's own connector to TimersManager
        self.timers_manager_connector = self

    def tray_menu(self, parent_menu):
        return self._tray_wrapper.tray_menu(parent_menu)

    def tray_start(self):
        return self._tray_wrapper.validate()

    def tray_exit(self):
        self._tray_wrapper.tray_exit()

    def set_credentials_to_env(self, username, api_key):
        os.environ["FTRACK_API_USER"] = username or ""
        os.environ["FTRACK_API_KEY"] = api_key or ""

    # --- TimersManager connection methods ---
    def start_timer(self, data):
        if self._tray_wrapper:
            self._tray_wrapper.start_timer_manager(data)

    def stop_timer(self):
        if self._tray_wrapper:
            self._tray_wrapper.stop_timer_manager()

    def ensure_is_process_ready(self, context):
        """Ensure addon is ready for process.

        Args:
            context (ProcessContext): Process context.

        """
        # Safe to support older ayon-core without 'ProcessPreparationError'
        from ayon_core.addon import ProcessPreparationError
        from ayon_ftrack.common import is_ftrack_enabled_in_settings

        # Do not continue if ftrack is not enabled in settings
        if context.project_name:
            settings = get_project_settings(context.project_name)
        else:
            settings = get_studio_settings()

        if not is_ftrack_enabled_in_settings(settings):
            return

        # Not sure if this should crash or silently continue?
        server_url = self.get_ftrack_url()
        if not server_url:
            return

        username = os.getenv("FTRACK_API_USER")
        api_key = os.getenv("FTRACK_API_KEY")

        if (
            username and api_key
            and check_credentials(username, api_key, server_url)
        ):
            self.set_credentials_to_env(username, api_key)
            return

        username, api_key = self.get_credentials()
        if (
            username and api_key
            and check_credentials(username, api_key, server_url)
        ):
            self.set_credentials_to_env(username, api_key)
            return

        if context.headless:
            raise ProcessPreparationError(
                "ftrack login details are missing. Unable to proceed"
                " without a user interface."
            )

        username, api_key = self._ask_for_credentials(server_url)
        if username and api_key:
            self.set_credentials_to_env(username, api_key)
            # Send the credentials to the running tray
            save_credentials(username, api_key, self.get_ftrack_url())
            tray_url = get_tray_server_url()
            if tray_url:
                requests.post(
                    f"{tray_url}/addons/ftrack/credentials",
                    json={"username": username, "api_key": api_key},
                )
            return

        raise ProcessPreparationError(
            "Unable to connect to ftrack. The process cannot proceed"
            " without this connection."
        )

    def register_timers_manager(self, timers_manager_addon):
        self._timers_manager_addon = timers_manager_addon

    def timer_started(self, data):
        if self._timers_manager_addon is not None:
            self._timers_manager_addon.timer_started(self.id, data)

    def timer_stopped(self):
        if self._timers_manager_addon is not None:
            self._timers_manager_addon.timer_stopped(self.id)

    def get_task_time(self, project_name, folder_path, task_name):
        folder_entity = ayon_api.get_folder_by_path(project_name, folder_path)
        if not folder_entity:
            return 0
        ftrack_id = folder_entity["attrib"].get("ftrackId")
        if not ftrack_id:
            return 0

        session = self.create_ftrack_session()
        query = (
            'select time_logged from Task where name is "{}"'
            ' and parent_id is "{}"'
            ' and project.full_name is "{}"'
        ).format(task_name, ftrack_id, project_name)
        task_entity = session.query(query).first()
        if not task_entity:
            return 0
        hours_logged = (task_entity["time_logged"] / 60) / 60
        return hours_logged

    def get_credentials(self):
        # type: () -> tuple
        """Get local ftrack credentials."""

        cred = get_credentials(self.ftrack_url)
        return cred.get("username"), cred.get("api_key")

    @staticmethod
    def _ask_for_credentials(ftrack_url):
        login_script = os.path.join(
            FTRACK_ADDON_DIR, "tray", "login_dialog.py"
        )
        with tempfile.NamedTemporaryFile(
            mode="w", prefix="ay_ftrack", suffix=".json", delete=False
        ) as tmp:
            json_path = tmp.name
            json.dump({"server_url": ftrack_url}, tmp.file)

        run_ayon_launcher_process(
            "--skip-bootstrap",
            login_script, json_path,
            add_sys_paths=True,
            creationflags=0,

        )

        with open(json_path, "r") as stream:
            data = json.load(stream)
        return data.get("username"), data.get("api_key")

settings_ftrack_url property

ftrack url from settings in a format as it is.

Returns:

Name Type Description
str

ftrack url from settings.

ensure_is_process_ready(context)

Ensure addon is ready for process.

Parameters:

Name Type Description Default
context ProcessContext

Process context.

required
Source code in client/ayon_ftrack/ftrack_addon.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def ensure_is_process_ready(self, context):
    """Ensure addon is ready for process.

    Args:
        context (ProcessContext): Process context.

    """
    # Safe to support older ayon-core without 'ProcessPreparationError'
    from ayon_core.addon import ProcessPreparationError
    from ayon_ftrack.common import is_ftrack_enabled_in_settings

    # Do not continue if ftrack is not enabled in settings
    if context.project_name:
        settings = get_project_settings(context.project_name)
    else:
        settings = get_studio_settings()

    if not is_ftrack_enabled_in_settings(settings):
        return

    # Not sure if this should crash or silently continue?
    server_url = self.get_ftrack_url()
    if not server_url:
        return

    username = os.getenv("FTRACK_API_USER")
    api_key = os.getenv("FTRACK_API_KEY")

    if (
        username and api_key
        and check_credentials(username, api_key, server_url)
    ):
        self.set_credentials_to_env(username, api_key)
        return

    username, api_key = self.get_credentials()
    if (
        username and api_key
        and check_credentials(username, api_key, server_url)
    ):
        self.set_credentials_to_env(username, api_key)
        return

    if context.headless:
        raise ProcessPreparationError(
            "ftrack login details are missing. Unable to proceed"
            " without a user interface."
        )

    username, api_key = self._ask_for_credentials(server_url)
    if username and api_key:
        self.set_credentials_to_env(username, api_key)
        # Send the credentials to the running tray
        save_credentials(username, api_key, self.get_ftrack_url())
        tray_url = get_tray_server_url()
        if tray_url:
            requests.post(
                f"{tray_url}/addons/ftrack/credentials",
                json={"username": username, "api_key": api_key},
            )
        return

    raise ProcessPreparationError(
        "Unable to connect to ftrack. The process cannot proceed"
        " without this connection."
    )

get_credentials()

Get local ftrack credentials.

Source code in client/ayon_ftrack/ftrack_addon.py
294
295
296
297
298
299
def get_credentials(self):
    # type: () -> tuple
    """Get local ftrack credentials."""

    cred = get_credentials(self.ftrack_url)
    return cred.get("username"), cred.get("api_key")

get_ftrack_url()

Resolved ftrack url.

Resolving is trying to fill missing information in url and tried to connect to the server.

Returns:

Type Description

Union[str, None]: Final variant of url or None if url could not be reached.

Source code in client/ayon_ftrack/ftrack_addon.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def get_ftrack_url(self):
    """Resolved ftrack url.

    Resolving is trying to fill missing information in url and tried to
    connect to the server.

    Returns:
        Union[str, None]: Final variant of url or None if url could not be
            reached.
    """

    if self._ftrack_url is _URL_NOT_SET:
        self._ftrack_url = resolve_ftrack_url(
            self._settings_ftrack_url,
            logger=self.log
        )
    return self._ftrack_url

get_global_environments()

ftrack's global environments.

Source code in client/ayon_ftrack/ftrack_addon.py
91
92
93
94
95
96
def get_global_environments(self):
    """ftrack's global environments."""

    return {
        "FTRACK_SERVER": self.ftrack_url
    }

get_launch_hook_paths()

Implementation for applications launch hooks.

Source code in client/ayon_ftrack/ftrack_addon.py
106
107
108
109
def get_launch_hook_paths(self):
    """Implementation for applications launch hooks."""

    return os.path.join(FTRACK_ADDON_DIR, "launch_hooks")

get_plugin_paths()

ftrack plugin paths.

Source code in client/ayon_ftrack/ftrack_addon.py
 98
 99
100
101
102
103
104
def get_plugin_paths(self):
    """ftrack plugin paths."""
    plugins_dir = os.path.join(FTRACK_ADDON_DIR, "plugins")
    return {
        "actions": [os.path.join(plugins_dir, "launcher_actions")],
        "publish": [os.path.join(plugins_dir, "publish")]
    }

resolve_ftrack_url(url, logger=None)

Checks if ftrack server is responding.

Source code in client/ayon_ftrack/ftrack_addon.py
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
def resolve_ftrack_url(url, logger=None):
    """Checks if ftrack server is responding."""

    if logger is None:
        logger = Logger.get_logger(__name__)

    url = url.strip("/ ")
    if not url:
        logger.error("ftrack URL is not set!")
        return None

    if not url.startswith("http"):
        url = "https://" + url

    ftrack_url = None
    if url and _check_ftrack_url(url):
        ftrack_url = url

    if not ftrack_url and not url.endswith("ftrackapp.com"):
        ftrackapp_url = url + ".ftrackapp.com"
        if _check_ftrack_url(ftrackapp_url):
            ftrack_url = ftrackapp_url

    if not ftrack_url and _check_ftrack_url(url):
        ftrack_url = url

    if ftrack_url:
        logger.debug("ftrack server \"{}\" is accessible.".format(ftrack_url))

    else:
        logger.error("ftrack server \"{}\" is not accessible!".format(url))

    return ftrack_url