Skip to content

lib

convert_to_fps(source_value)

Convert value into fps value.

Non string values are kept untouched. String is tried to convert. Valid values: "1000" "1000.05" "1000,05" ",05" ".05" "1000," "1000." "1000/1000" "1000.05/1000" "1000/1000.05" "1000.05/1000.05" "1000,05/1000" "1000/1000,05" "1000,05/1000,05"

Invalid values: "/" "/1000" "1000/" "," "." ...any other string

Returns:

Name Type Description
float

Converted value.

Raises:

Type Description
InvalidFpsValue

When value can't be converted to float.

Source code in client/ayon_ftrack/common/lib.py
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
def convert_to_fps(source_value):
    """Convert value into fps value.

    Non string values are kept untouched. String is tried to convert.
    Valid values:
    "1000"
    "1000.05"
    "1000,05"
    ",05"
    ".05"
    "1000,"
    "1000."
    "1000/1000"
    "1000.05/1000"
    "1000/1000.05"
    "1000.05/1000.05"
    "1000,05/1000"
    "1000/1000,05"
    "1000,05/1000,05"

    Invalid values:
    "/"
    "/1000"
    "1000/"
    ","
    "."
    ...any other string

    Returns:
        float: Converted value.

    Raises:
        InvalidFpsValue: When value can't be converted to float.
    """

    if not isinstance(source_value, str):
        if isinstance(source_value, numbers.Number):
            return float(source_value)
        return source_value

    value = source_value.strip().replace(",", ".")
    if not value:
        raise InvalidFpsValue("Got empty value")

    subs = value.split("/")
    if len(subs) == 1:
        str_value = subs[0]
        if not is_string_number(str_value):
            raise InvalidFpsValue(
                "Value \"{}\" can't be converted to number.".format(value)
            )
        return float(str_value)

    elif len(subs) == 2:
        divident, divisor = subs
        if not divident or not is_string_number(divident):
            raise InvalidFpsValue(
                "Divident value \"{}\" can't be converted to number".format(
                    divident
                )
            )

        if not divisor or not is_string_number(divisor):
            raise InvalidFpsValue(
                "Divisor value \"{}\" can't be converted to number".format(
                    divident
                )
            )
        divisor_float = float(divisor)
        if divisor_float == 0.0:
            raise InvalidFpsValue("Can't divide by zero")
        return float(divident) / divisor_float

    raise InvalidFpsValue(
        "Value can't be converted to number \"{}\"".format(source_value)
    )

create_chunks(iterable, chunk_size=None)

Separate iterable into multiple chunks by size.

Parameters:

Name Type Description Default
iterable Iterable[Any]

Object that will be separated into chunks.

required
chunk_size int

Size of one chunk. Default value is 200.

None

Returns:

Type Description

List[List[Any]]: Chunked items.

Source code in client/ayon_ftrack/common/lib.py
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
def create_chunks(iterable, chunk_size=None):
    """Separate iterable into multiple chunks by size.

    Args:
        iterable (Iterable[Any]): Object that will be separated into chunks.
        chunk_size (int): Size of one chunk. Default value is 200.

    Returns:
        List[List[Any]]: Chunked items.
    """

    chunks = []
    tupled_iterable = tuple(iterable)
    if not tupled_iterable:
        return chunks
    iterable_size = len(tupled_iterable)
    if chunk_size is None:
        chunk_size = 200

    if chunk_size < 1:
        chunk_size = 1

    for idx in range(0, iterable_size, chunk_size):
        chunks.append(tupled_iterable[idx:idx + chunk_size])
    return chunks

get_ftrack_icon_url(icon_name, addon_version, addon_name=None)

Helper to get icon url to server.

The existence of file is not validated.

Parameters:

Name Type Description Default
icon_name str

Name of icon filename.

required
addon_version str

Version of addon.

required
addon_name Optional[str]

Name of addon. For development purposes. Default value 'ftrack'.

None

Returns:

Name Type Description
str

Url to icon on server.

Source code in client/ayon_ftrack/common/lib.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
def get_ftrack_icon_url(icon_name, addon_version, addon_name=None):
    """Helper to get icon url to server.

    The existence of file is not validated.

    Args:
        icon_name (str): Name of icon filename.
        addon_version (str): Version of addon.
        addon_name (Optional[str]): Name of addon. For development purposes.
            Default value 'ftrack'.

    Returns:
        str: Url to icon on server.
    """

    return get_ftrack_public_url(
        "icons", icon_name,
        addon_version=addon_version,
        addon_name=addon_name
    )

get_ftrack_public_url(*args, addon_version, addon_name=None)

Url to public path in ftrack addon.

Parameters:

Name Type Description Default
args tuple[str]

Subpaths in 'public' dir.

()
addon_version str

Version of addon.

required
addon_name Optional[str]

Name of addon. This is for development purposes. Default value 'ftrack'.

None

Returns:

Name Type Description
str

Url to public file on server in ftrack addon.

Source code in client/ayon_ftrack/common/lib.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
def get_ftrack_public_url(*args, addon_version, addon_name=None):
    """Url to public path in ftrack addon.

    Args:
        args (tuple[str]): Subpaths in 'public' dir.
        addon_version (str): Version of addon.
        addon_name (Optional[str]): Name of addon. This is for development
            purposes. Default value 'ftrack'.

    Returns:
        str: Url to public file on server in ftrack addon.
    """

    server_url = get_base_url()
    parts = [
        server_url,
        "addons",
        addon_name or "ftrack",
        addon_version,
        "public"
    ]
    parts.extend(args)
    return "/".join(parts)

get_host_ip()

Get IP of machine.

Returns:

Type Description

Union[str, None]: IP address of machine or None if could not be detected.

Source code in client/ayon_ftrack/common/lib.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def get_host_ip():
    """Get IP of machine.

    Returns:
        Union[str, None]: IP address of machine or None if could not be
            detected.
    """

    host_name = socket.gethostname()
    try:
        return socket.gethostbyname(host_name)
    except Exception:
        pass

    return None

get_service_ftrack_icon_url(icon_name, addon_version=None, addon_name=None)

Icon url to server for service process.

Information about addon version are taken from registered service in 'ayon_api'.

Parameters:

Name Type Description Default
icon_name str

Name of icon filename.

required
addon_version Optional[str]

Version of addon. Version from registered service is used if not passed. For development purposes.

None
addon_name Optional[str]

Name of addon. For development purposes.

None

Returns:

Name Type Description
str

Url to icon on server.

Source code in client/ayon_ftrack/common/lib.py
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
def get_service_ftrack_icon_url(
    icon_name, addon_version=None, addon_name=None
):
    """Icon url to server for service process.

    Information about addon version are taken from registered service
    in 'ayon_api'.

    Args:
        icon_name (str): Name of icon filename.
        addon_version (Optional[str]): Version of addon. Version from
            registered service is used if not passed. For development purposes.
        addon_name (Optional[str]): Name of addon. For development purposes.

    Returns:
        str: Url to icon on server.
    """

    return get_ftrack_icon_url(
        icon_name,
        addon_version=addon_version or get_service_addon_version(),
        addon_name=addon_name or get_service_addon_name()
    )

is_ftrack_enabled_in_settings(project_settings)

Check if ftrack is enabled in ftrack project settings.

This function expect settings for a specific project. It is not checking if ftrack is enabled in general.

Project settings gives option to disable ftrack integration per project. That should disable most of ftrack integration functionality, especially pipeline integration > publish plugins, and some automations like event server handlers.

Parameters:

Name Type Description Default
project_settings dict[str, Any]

ftrack project settings.

required

Returns:

Name Type Description
bool

True if ftrack is enabled in project settings.

Source code in client/ayon_ftrack/common/lib.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def is_ftrack_enabled_in_settings(project_settings):
    """Check if ftrack is enabled in ftrack project settings.

    This function expect settings for a specific project. It is not checking
    if ftrack is enabled in general.

    Project settings gives option to disable ftrack integration per project.
    That should disable most of ftrack integration functionality, especially
    pipeline integration > publish plugins, and some automations like event
    server handlers.

    Args:
        project_settings (dict[str, Any]): ftrack project settings.

    Returns:
        bool: True if ftrack is enabled in project settings.
    """

    ftrack_enabled = project_settings.get("enabled")
    # If 'ftrack_enabled' is not set, we assume it is enabled.
    # - this is for backwards compatibility - remove in future
    if ftrack_enabled is None:
        return True
    return ftrack_enabled

is_string_number(value)

Can string value be converted to number (float).

Source code in client/ayon_ftrack/common/lib.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def is_string_number(value: str) -> bool:
    """Can string value be converted to number (float)."""

    if not isinstance(value, str):
        raise TypeError(f"Expected str got {str(type(value))}")
    if value == ".":
        return False

    if value.startswith("."):
        value = "0" + value
    elif value.endswith("."):
        value = value + "0"

    if re.match(r"^\d+(\.\d+)?$", value) is None:
        return False
    return True

join_filter_values(values)

Prepare values to be used for filtering in ftrack query.

Parameters:

Name Type Description Default
Iterable[str]

Values to join for filter query.

required

Returns:

Name Type Description
str

Prepared values for ftrack query.

Source code in client/ayon_ftrack/common/lib.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def join_filter_values(values):
    """Prepare values to be used for filtering in ftrack query.

    Args:
        Iterable[str]: Values to join for filter query.

    Returns:
        str: Prepared values for ftrack query.
    """

    return ",".join({
        '"{}"'.format(value)
        for value in values
    })