mirror of
https://github.com/ynput/ayon-core.git
synced 2025-12-24 21:04:40 +01:00
Merge pull request #1237 from pypeclub/feature/global_settings
Pype global settings
This commit is contained in:
commit
a6dc83aa13
5 changed files with 96 additions and 74 deletions
115
igniter/tools.py
115
igniter/tools.py
|
|
@ -6,6 +6,7 @@ Functions ``compose_url()`` and ``decompose_url()`` are the same as in
|
|||
version is decided.
|
||||
|
||||
"""
|
||||
import os
|
||||
from typing import Dict, Union
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
from pathlib import Path
|
||||
|
|
@ -130,6 +131,7 @@ def validate_mongo_connection(cnx: str) -> (bool, str):
|
|||
try:
|
||||
client = MongoClient(**mongo_args)
|
||||
client.server_info()
|
||||
client.close()
|
||||
except ServerSelectionTimeoutError as e:
|
||||
return False, f"Cannot connect to server {cnx} - {e}"
|
||||
except ValueError:
|
||||
|
|
@ -185,73 +187,68 @@ def validate_path_string(path: str) -> (bool, str):
|
|||
return True, "valid path"
|
||||
|
||||
|
||||
def load_environments(sections: list = None) -> dict:
|
||||
"""Load environments from Pype.
|
||||
|
||||
This will load environments from database, process them with
|
||||
:mod:`acre` and return them as flattened dictionary.
|
||||
|
||||
Args:
|
||||
sections (list, optional): load specific types
|
||||
|
||||
Returns;
|
||||
dict of str: loaded and processed environments.
|
||||
|
||||
"""
|
||||
import acre
|
||||
|
||||
from pype import settings
|
||||
|
||||
all_env = settings.get_environments()
|
||||
merged_env = {}
|
||||
|
||||
sections = sections or all_env.keys()
|
||||
|
||||
for section in sections:
|
||||
try:
|
||||
parsed_env = acre.parse(all_env[section])
|
||||
except AttributeError:
|
||||
continue
|
||||
merged_env = acre.append(merged_env, parsed_env)
|
||||
|
||||
return acre.compute(merged_env, cleanup=True)
|
||||
|
||||
|
||||
def get_pype_path_from_db(url: str) -> Union[str, None]:
|
||||
"""Get Pype path from database.
|
||||
def get_pype_global_settings(url: str) -> dict:
|
||||
"""Load global settings from Mongo database.
|
||||
|
||||
We are loading data from database `pype` and collection `settings`.
|
||||
There we expect document type `global_settings`.
|
||||
|
||||
Args:
|
||||
url (str): MongoDB url.
|
||||
|
||||
Returns:
|
||||
dict: With settings data. Empty dictionary is returned if not found.
|
||||
"""
|
||||
try:
|
||||
components = decompose_url(url)
|
||||
except RuntimeError:
|
||||
return {}
|
||||
mongo_kwargs = {
|
||||
"host": compose_url(**components),
|
||||
"serverSelectionTimeoutMS": 2000
|
||||
}
|
||||
port = components.get("port")
|
||||
if port is not None:
|
||||
mongo_kwargs["port"] = int(port)
|
||||
|
||||
try:
|
||||
# Create mongo connection
|
||||
client = MongoClient(**mongo_kwargs)
|
||||
# Access settings collection
|
||||
col = client["pype"]["settings"]
|
||||
# Query global settings
|
||||
global_settings = col.find_one({"type": "global_settings"}) or {}
|
||||
# Close Mongo connection
|
||||
client.close()
|
||||
|
||||
except Exception:
|
||||
# TODO log traceback or message
|
||||
return {}
|
||||
|
||||
return global_settings.get("data") or {}
|
||||
|
||||
|
||||
def get_pype_path_from_db(url: str) -> Union[str, None]:
|
||||
"""Get Pype path from global settings.
|
||||
|
||||
Args:
|
||||
url (str): mongodb url.
|
||||
|
||||
Returns:
|
||||
path to Pype or None if not found
|
||||
|
||||
"""
|
||||
try:
|
||||
components = decompose_url(url)
|
||||
except RuntimeError:
|
||||
return None
|
||||
mongo_args = {
|
||||
"host": compose_url(**components),
|
||||
"serverSelectionTimeoutMS": 2000
|
||||
}
|
||||
port = components.get("port")
|
||||
if port is not None:
|
||||
mongo_args["port"] = int(port)
|
||||
global_settings = get_pype_global_settings(url)
|
||||
paths = (
|
||||
global_settings
|
||||
.get("pype_path", {})
|
||||
.get(platform.system().lower())
|
||||
) or []
|
||||
# For cases when `pype_path` is a single path
|
||||
if paths and isinstance(paths, str):
|
||||
paths = [paths]
|
||||
|
||||
try:
|
||||
client = MongoClient(**mongo_args)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
db = client.pype
|
||||
col = db.settings
|
||||
|
||||
global_settings = col.find_one({"type": "global_settings"}, {"data": 1})
|
||||
if not global_settings:
|
||||
return None
|
||||
global_settings.get("data", {})
|
||||
return global_settings.get("pype_path", {}).get(platform.system().lower())
|
||||
# Loop over paths and return only existing
|
||||
for path in paths:
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ METADATA_KEYS = (
|
|||
)
|
||||
|
||||
# File where studio's system overrides are stored
|
||||
GLOBAL_SETTINGS_KEY = "global_settings"
|
||||
SYSTEM_SETTINGS_KEY = "system_settings"
|
||||
PROJECT_SETTINGS_KEY = "project_settings"
|
||||
PROJECT_ANATOMY_KEY = "project_anatomy"
|
||||
|
|
|
|||
|
|
@ -1,11 +1,6 @@
|
|||
{
|
||||
"studio_name": "Studio name",
|
||||
"studio_code": "stu",
|
||||
"studio_soft": {
|
||||
"windows": "convert from \"STUDIO_SOFT\"",
|
||||
"darwin": "",
|
||||
"linux": ""
|
||||
},
|
||||
"environment": {
|
||||
"FFMPEG_PATH": {
|
||||
"windows": "{PYPE_ROOT}/vendor/bin/ffmpeg_exec/windows/bin",
|
||||
|
|
@ -19,5 +14,10 @@
|
|||
"PYPE_OCIO_CONFIG"
|
||||
]
|
||||
}
|
||||
},
|
||||
"pype_path": {
|
||||
"windows": [],
|
||||
"darwin": [],
|
||||
"linux": []
|
||||
}
|
||||
}
|
||||
|
|
@ -19,20 +19,20 @@
|
|||
"type": "splitter"
|
||||
},
|
||||
{
|
||||
"key": "studio_soft",
|
||||
"type": "path",
|
||||
"label": "Studio Software Location",
|
||||
"multiplatform": true,
|
||||
"multipath": false
|
||||
"key": "environment",
|
||||
"label": "Environment",
|
||||
"type": "raw-json",
|
||||
"env_group_key": "global"
|
||||
},
|
||||
{
|
||||
"type": "splitter"
|
||||
},
|
||||
{
|
||||
"key": "environment",
|
||||
"label": "Environment",
|
||||
"type": "raw-json",
|
||||
"env_group_key": "global"
|
||||
"type": "path",
|
||||
"key": "pype_path",
|
||||
"label": "Pype Path",
|
||||
"multiplatform": true,
|
||||
"multipath": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from abc import ABCMeta, abstractmethod
|
|||
import six
|
||||
import pype
|
||||
from .constants import (
|
||||
GLOBAL_SETTINGS_KEY,
|
||||
SYSTEM_SETTINGS_KEY,
|
||||
PROJECT_SETTINGS_KEY,
|
||||
PROJECT_ANATOMY_KEY,
|
||||
|
|
@ -224,6 +225,13 @@ class MongoSettingsHandler(SettingsHandler):
|
|||
self._prepare_project_settings_keys()
|
||||
return self._attribute_keys
|
||||
|
||||
def _prepare_global_settings(self, data):
|
||||
output = {}
|
||||
# Add "pype_path" key to global settings if is set
|
||||
if "general" in data and "pype_path" in data["general"]:
|
||||
output["pype_path"] = data["general"]["pype_path"]
|
||||
return output
|
||||
|
||||
def save_studio_settings(self, data):
|
||||
"""Save studio overrides of system settings.
|
||||
|
||||
|
|
@ -235,8 +243,8 @@ class MongoSettingsHandler(SettingsHandler):
|
|||
Args:
|
||||
data(dict): Data of studio overrides with override metadata.
|
||||
"""
|
||||
# Store system settings
|
||||
self.system_settings_cache.update_data(data)
|
||||
|
||||
self.collection.replace_one(
|
||||
{
|
||||
"type": SYSTEM_SETTINGS_KEY
|
||||
|
|
@ -248,6 +256,22 @@ class MongoSettingsHandler(SettingsHandler):
|
|||
upsert=True
|
||||
)
|
||||
|
||||
# Get global settings from system settings
|
||||
global_settings = self._prepare_global_settings(
|
||||
self.system_settings_cache.data
|
||||
)
|
||||
# Store global settings
|
||||
self.collection.replace_one(
|
||||
{
|
||||
"type": GLOBAL_SETTINGS_KEY
|
||||
},
|
||||
{
|
||||
"type": GLOBAL_SETTINGS_KEY,
|
||||
"data": global_settings
|
||||
},
|
||||
upsert=True
|
||||
)
|
||||
|
||||
def save_project_settings(self, project_name, overrides):
|
||||
"""Save studio overrides of project settings.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue