From 958d221c449c31be01339f9f24acaf55c405cd93 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 23 Feb 2021 16:24:44 +0100 Subject: [PATCH] added functions to change pype mongo url and get local site id --- pype/api.py | 9 +++++++-- pype/lib/__init__.py | 7 ++++++- pype/lib/user_settings.py | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/pype/api.py b/pype/api.py index 71c31efa93..44caa7632f 100644 --- a/pype/api.py +++ b/pype/api.py @@ -22,7 +22,9 @@ from .lib import ( get_app_environments_for_context, source_hash, get_latest_version, - get_global_environments + get_global_environments, + get_local_site_id, + change_pype_mongo_url ) from .lib.mongo import ( @@ -109,5 +111,8 @@ __all__ = [ "run_subprocess", "get_latest_version", - "get_global_environments" + "get_global_environments", + + "get_local_site_id", + "change_pype_mongo_url" ] diff --git a/pype/lib/__init__.py b/pype/lib/__init__.py index 9d04c1ce31..b5674a9e5d 100644 --- a/pype/lib/__init__.py +++ b/pype/lib/__init__.py @@ -95,7 +95,9 @@ from .plugin_tools import ( from .user_settings import ( IniSettingRegistry, JSONSettingRegistry, - PypeSettingsRegistry + PypeSettingsRegistry, + get_local_site_id, + change_pype_mongo_url ) from .path_tools import ( @@ -198,6 +200,9 @@ __all__ = [ "IniSettingRegistry", "JSONSettingRegistry", "PypeSettingsRegistry", + "get_local_site_id", + "change_pype_mongo_url", + "timeit", "is_overlapping_otio_ranges", diff --git a/pype/lib/user_settings.py b/pype/lib/user_settings.py index 00ce68cb0b..def126d913 100644 --- a/pype/lib/user_settings.py +++ b/pype/lib/user_settings.py @@ -28,6 +28,8 @@ import platform import appdirs import six +from .import validate_mongo_connection + @six.add_metaclass(ABCMeta) class ASettingRegistry(): @@ -464,3 +466,43 @@ class PypeSettingsRegistry(JSONSettingRegistry): self.product = "pype" path = appdirs.user_data_dir(self.product, self.vendor) super(PypeSettingsRegistry, self).__init__("pype_settings", path) + + +def _create_local_site_id(registry=None): + """Create a local site identifier.""" + from uuid import uuid4 + + if registry is None: + registry = PypeSettingsRegistry() + + new_id = str(uuid4()) + + print("Created local site id \"{}\"".format(new_id)) + + registry.set_secure_item("localId", new_id) + + return new_id + + +def get_local_site_id(): + """Get local site identifier. + + Identifier is created if does not exists yet. + """ + registry = PypeSettingsRegistry() + try: + return registry.get_secure_item("localId") + except ValueError: + return _create_local_site_id() + + +def change_pype_mongo_url(new_mongo_url): + """Change mongo url in pype registry. + + Change of Pype mongo URL require restart of running pype processes or + processes using pype. + """ + + validate_mongo_connection(new_mongo_url) + registry = PypeSettingsRegistry() + registry.set_secure_item("pypeMongo", new_mongo_url)