2017-06-09 10:52:56 +02:00
|
|
|
import functools
|
|
|
|
import pathlib
|
2019-04-18 12:09:54 +02:00
|
|
|
import typing
|
2017-06-09 10:52:56 +02:00
|
|
|
|
2017-01-13 17:24:37 +01:00
|
|
|
from pillarsdk.resource import List, Find, Create
|
|
|
|
|
|
|
|
|
|
|
|
class Manager(List, Find):
|
|
|
|
"""Manager class wrapping the REST nodes endpoint"""
|
2021-02-16 11:21:06 +01:00
|
|
|
|
|
|
|
path = "flamenco/managers"
|
2017-06-09 10:52:56 +02:00
|
|
|
PurePlatformPath = pathlib.PurePath
|
|
|
|
|
2019-04-18 12:09:54 +02:00
|
|
|
@functools.lru_cache(maxsize=1)
|
|
|
|
def _path_replacements(self) -> list:
|
2019-05-10 12:29:39 +02:00
|
|
|
"""Defer to _path_replacements_vN() to get path replacement vars.
|
|
|
|
|
|
|
|
Returns a list of tuples (variable name, variable value).
|
|
|
|
"""
|
2019-04-18 12:09:54 +02:00
|
|
|
settings_version = self.settings_version or 1
|
|
|
|
try:
|
2021-02-16 11:21:06 +01:00
|
|
|
settings_func = getattr(self, "_path_replacements_v%d" % settings_version)
|
2019-04-18 12:09:54 +02:00
|
|
|
except AttributeError:
|
2021-02-16 11:21:06 +01:00
|
|
|
raise RuntimeError(
|
|
|
|
"This manager has unsupported settings version %d; "
|
|
|
|
"upgrade Blender Cloud add-on"
|
|
|
|
)
|
2019-04-18 12:09:54 +02:00
|
|
|
|
2019-05-10 12:29:39 +02:00
|
|
|
def longest_value_first(item):
|
|
|
|
var_name, var_value = item
|
|
|
|
return -len(var_value), var_value, var_name
|
|
|
|
|
|
|
|
replacements = settings_func()
|
|
|
|
replacements.sort(key=longest_value_first)
|
|
|
|
return replacements
|
2019-04-18 12:09:54 +02:00
|
|
|
|
|
|
|
def _path_replacements_v1(self) -> typing.List[typing.Tuple[str, str]]:
|
2017-07-03 09:14:27 +02:00
|
|
|
import platform
|
2017-06-09 10:52:56 +02:00
|
|
|
|
|
|
|
if self.path_replacement is None:
|
|
|
|
return []
|
|
|
|
|
|
|
|
items = self.path_replacement.to_dict().items()
|
|
|
|
|
2017-07-03 09:14:27 +02:00
|
|
|
this_platform = platform.system().lower()
|
2021-02-16 11:21:06 +01:00
|
|
|
return [
|
|
|
|
(varname, platform_replacements[this_platform])
|
|
|
|
for varname, platform_replacements in items
|
|
|
|
if this_platform in platform_replacements
|
|
|
|
]
|
2017-06-09 10:52:56 +02:00
|
|
|
|
2019-04-18 12:09:54 +02:00
|
|
|
def _path_replacements_v2(self) -> typing.List[typing.Tuple[str, str]]:
|
|
|
|
import platform
|
|
|
|
|
|
|
|
if not self.variables:
|
|
|
|
return []
|
|
|
|
|
|
|
|
this_platform = platform.system().lower()
|
2021-02-16 11:21:06 +01:00
|
|
|
audiences = {"users", "all"}
|
2019-04-18 12:09:54 +02:00
|
|
|
|
|
|
|
replacements = []
|
|
|
|
for var_name, variable in self.variables.to_dict().items():
|
|
|
|
# Path replacement requires bidirectional variables.
|
2021-02-16 11:21:06 +01:00
|
|
|
if variable.get("direction") != "twoway":
|
2019-04-18 12:09:54 +02:00
|
|
|
continue
|
|
|
|
|
2021-02-16 11:21:06 +01:00
|
|
|
for var_value in variable.get("values", []):
|
|
|
|
if var_value.get("audience") not in audiences:
|
2019-04-18 12:09:54 +02:00
|
|
|
continue
|
2021-02-16 11:21:06 +01:00
|
|
|
if var_value.get("platform", "").lower() != this_platform:
|
2019-04-18 12:09:54 +02:00
|
|
|
continue
|
|
|
|
|
2021-02-16 11:21:06 +01:00
|
|
|
replacements.append((var_name, var_value.get("value")))
|
2019-04-18 12:09:54 +02:00
|
|
|
return replacements
|
|
|
|
|
2017-06-09 10:52:56 +02:00
|
|
|
def replace_path(self, some_path: pathlib.PurePath) -> str:
|
|
|
|
"""Performs path variable replacement.
|
|
|
|
|
|
|
|
Tries to find platform-specific path prefixes, and replaces them with
|
|
|
|
variables.
|
|
|
|
"""
|
2021-02-16 11:21:06 +01:00
|
|
|
assert isinstance(some_path, pathlib.PurePath), (
|
|
|
|
"some_path should be a PurePath, not %r" % some_path
|
|
|
|
)
|
2017-06-09 10:52:56 +02:00
|
|
|
|
2019-05-21 10:19:34 +02:00
|
|
|
for varname, path in self._path_replacements():
|
2017-06-09 10:52:56 +02:00
|
|
|
replacement = self.PurePlatformPath(path)
|
|
|
|
try:
|
|
|
|
relpath = some_path.relative_to(replacement)
|
|
|
|
except ValueError:
|
|
|
|
# Not relative to each other, so no replacement possible
|
|
|
|
continue
|
|
|
|
|
2021-02-16 11:21:06 +01:00
|
|
|
replacement_root = self.PurePlatformPath("{%s}" % varname)
|
2017-06-09 10:52:56 +02:00
|
|
|
return (replacement_root / relpath).as_posix()
|
|
|
|
|
|
|
|
return some_path.as_posix()
|
2017-01-13 17:24:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Job(List, Find, Create):
|
2021-02-16 11:21:06 +01:00
|
|
|
"""Job class wrapping the REST nodes endpoint"""
|
|
|
|
|
|
|
|
path = "flamenco/jobs"
|
|
|
|
ensure_query_projections = {"project": 1}
|
2019-02-28 12:52:51 +01:00
|
|
|
|
|
|
|
def patch(self, payload: dict, api=None):
|
|
|
|
import pillarsdk.utils
|
|
|
|
|
|
|
|
api = api or self.api
|
|
|
|
|
2021-02-16 11:21:06 +01:00
|
|
|
url = pillarsdk.utils.join_url(self.path, str(self["_id"]))
|
|
|
|
headers = pillarsdk.utils.merge_dict(
|
|
|
|
self.http_headers(), {"Content-Type": "application/json"}
|
|
|
|
)
|
2019-02-28 12:52:51 +01:00
|
|
|
response = api.patch(url, payload, headers=headers)
|
|
|
|
return response
|