From ca6759b69e16775fcc976ed8e733898fb4f02569 Mon Sep 17 00:00:00 2001 From: AndyTitu Date: Tue, 12 Mar 2024 17:18:28 +0100 Subject: [PATCH] Improve dependency import --- .../{src => sdk-core-linux-amd64}/__init__.py | 0 .../libop_uniffi_core.so | Bin .../op_uniffi_core.py | 0 op_uniffi_core_linux_amd64/setup.py | 2 +- .../__init__.py | 0 .../libop_uniffi_core.dylib | Bin .../op_uniffi_core.py | 0 op_uniffi_core_mac_arm64/setup.py | 2 +- sdk/onepassword/core.py | 36 ++++++++++-------- sdk/setup.py | 4 +- 10 files changed, 25 insertions(+), 19 deletions(-) rename op_uniffi_core_linux_amd64/{src => sdk-core-linux-amd64}/__init__.py (100%) rename op_uniffi_core_linux_amd64/{src => sdk-core-linux-amd64}/libop_uniffi_core.so (100%) rename op_uniffi_core_linux_amd64/{src => sdk-core-linux-amd64}/op_uniffi_core.py (100%) rename op_uniffi_core_mac_arm64/{op_uniffi_lib_mac_arm64 => sdk-core-mac-arm64}/__init__.py (100%) rename op_uniffi_core_mac_arm64/{op_uniffi_lib_mac_arm64 => sdk-core-mac-arm64}/libop_uniffi_core.dylib (100%) rename op_uniffi_core_mac_arm64/{op_uniffi_lib_mac_arm64 => sdk-core-mac-arm64}/op_uniffi_core.py (100%) diff --git a/op_uniffi_core_linux_amd64/src/__init__.py b/op_uniffi_core_linux_amd64/sdk-core-linux-amd64/__init__.py similarity index 100% rename from op_uniffi_core_linux_amd64/src/__init__.py rename to op_uniffi_core_linux_amd64/sdk-core-linux-amd64/__init__.py diff --git a/op_uniffi_core_linux_amd64/src/libop_uniffi_core.so b/op_uniffi_core_linux_amd64/sdk-core-linux-amd64/libop_uniffi_core.so similarity index 100% rename from op_uniffi_core_linux_amd64/src/libop_uniffi_core.so rename to op_uniffi_core_linux_amd64/sdk-core-linux-amd64/libop_uniffi_core.so diff --git a/op_uniffi_core_linux_amd64/src/op_uniffi_core.py b/op_uniffi_core_linux_amd64/sdk-core-linux-amd64/op_uniffi_core.py similarity index 100% rename from op_uniffi_core_linux_amd64/src/op_uniffi_core.py rename to op_uniffi_core_linux_amd64/sdk-core-linux-amd64/op_uniffi_core.py diff --git a/op_uniffi_core_linux_amd64/setup.py b/op_uniffi_core_linux_amd64/setup.py index 25a4559..a58dca1 100644 --- a/op_uniffi_core_linux_amd64/setup.py +++ b/op_uniffi_core_linux_amd64/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages setup( - name='op_uniffi_lib_mac_arm64', + name='sdk-core-linux-amd64', version='0.1.0', packages=find_packages(), author='1Password', diff --git a/op_uniffi_core_mac_arm64/op_uniffi_lib_mac_arm64/__init__.py b/op_uniffi_core_mac_arm64/sdk-core-mac-arm64/__init__.py similarity index 100% rename from op_uniffi_core_mac_arm64/op_uniffi_lib_mac_arm64/__init__.py rename to op_uniffi_core_mac_arm64/sdk-core-mac-arm64/__init__.py diff --git a/op_uniffi_core_mac_arm64/op_uniffi_lib_mac_arm64/libop_uniffi_core.dylib b/op_uniffi_core_mac_arm64/sdk-core-mac-arm64/libop_uniffi_core.dylib similarity index 100% rename from op_uniffi_core_mac_arm64/op_uniffi_lib_mac_arm64/libop_uniffi_core.dylib rename to op_uniffi_core_mac_arm64/sdk-core-mac-arm64/libop_uniffi_core.dylib diff --git a/op_uniffi_core_mac_arm64/op_uniffi_lib_mac_arm64/op_uniffi_core.py b/op_uniffi_core_mac_arm64/sdk-core-mac-arm64/op_uniffi_core.py similarity index 100% rename from op_uniffi_core_mac_arm64/op_uniffi_lib_mac_arm64/op_uniffi_core.py rename to op_uniffi_core_mac_arm64/sdk-core-mac-arm64/op_uniffi_core.py diff --git a/op_uniffi_core_mac_arm64/setup.py b/op_uniffi_core_mac_arm64/setup.py index 917ae47..30ddc3c 100644 --- a/op_uniffi_core_mac_arm64/setup.py +++ b/op_uniffi_core_mac_arm64/setup.py @@ -2,7 +2,7 @@ setup( - name='op_uniffi_lib_mac_arm64', + name='sdk_core_mac_arm64', version='0.1.0', packages=find_packages(), author='1Password', diff --git a/sdk/onepassword/core.py b/sdk/onepassword/core.py index 879ce5b..a3ee071 100644 --- a/sdk/onepassword/core.py +++ b/sdk/onepassword/core.py @@ -1,20 +1,26 @@ import json -import sys import importlib.util -#import op_uniffi_lib_mac_arm64.op_uniffi_core as core - -if (spec := importlib.util.find_spec("op_uniffi_lib_mac_arm64.op_uniffi_core")) is not None: - module = importlib.util.module_from_spec(spec) - print(module) - sys.modules["core"] = module - spec.loader.exec_module(module) - print(f"op_uniffi_lib_mac_arm64 has been imported") -elif (spec := importlib.util.find_spec("op_uniffi_lib_linux_amd64")) is not None: - module = importlib.util.module_from_spec(spec) - sys.modules["core"] = module - spec.loader.exec_module(module) - -core = sys.modules["core"] + + +def try_import_core(platform_specific_package): + if (spec := importlib.util.find_spec(platform_specific_package)) is not None: + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + return None + +potential_dependencies = [ + "sdk_core_linux_amd64.op_uniffi_core", + "sdk_core_mac_arm64.op_uniffi_core" +] + +core = None +for dep in potential_dependencies: + core = try_import_core(dep) + # Stop at the first succesful import + if core is not None: + break + # InitClient creates a client instance in the current core module and returns its unique ID. async def _init_client(client_config): diff --git a/sdk/setup.py b/sdk/setup.py index a030ae2..0c7b99d 100644 --- a/sdk/setup.py +++ b/sdk/setup.py @@ -8,7 +8,7 @@ description="The 1Password Python SDK offers programmatic read access to your secrets in 1Password in an interface native to Python.", url='https://github.com/1Password/onepassword-sdk-python', install_requires=[ - "op_uniffi_lib_mac_arm64; platform_system=='Darwin' and platform_machine=='arm64'", - "op_uniffi_lib_mac_x86_64; platform_system=='Linux' and platform_machine=='x86_64'", + "sdk_core_mac_arm64; platform_system=='Darwin' and platform_machine=='arm64'", + "sdk_core_linux_x86_64; platform_system=='Linux' and platform_machine=='x86_64'", ], ) \ No newline at end of file