FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
12-13-2023 03:31 PM - edited 12-13-2023 03:32 PM
Hi all.
In this example is shown how to create a Data Layer scope for a specific node written in C++: How-to-provide-API-in-the-Data-Layer-for-your-app .
Is there a way to do the exact thing but in Python? I could not find the CreateScopeDirect method in python.
Thank you,
Criss.
Solved! Go to Solution.
12-13-2023 04:58 PM
Hi Criss,
Yes. You can do the same thing also in Python. Therefor import the "Scope.py" from ctrlx-automation-sdk/src/python/comm/datalayer/Scope.py.
Working with Flatbuffers in Python is a little bit different to C++. If you are struggling with that, I can recommend you to take a look at the documentaion from google.
Best regards,
Nick
12-14-2023 04:18 PM - edited 12-14-2023 04:19 PM
Thank you for the hint. I was able to write down some code but it still does not work.
builder = flatbuffers.Builder(0)
permissionsRW = builder.CreateString("test-node/**")
description = builder.CreateString("Allow to read and write")
name = builder.CreateString("Test-node RW permission")
identifier = builder.CreateString("datalayer.test-node.rw")
Scope.ScopeStart(builder)
Scope.AddPermissionsRw(builder, permissionsRW)
Scope.AddDescription(builder, description)
Scope.AddName(builder, name)
Scope.AddIdentifier(builder, identifier)
scope = Scope.ScopeEnd(builder)
builder.Finish(scope)
variant = Variant()
variant_result = variant.set_flatbuffers(builder.Output())
if variant_result is not Result.OK:
print("ERROR: creating variant flatbuffer: ", variant_result, flush=True)
result, var= client.create_sync("datalayer/security/scopes", variant)
if result is not Result.OK:
print("ERROR: create scope failed: ", result, flush=True)
I receive the error : ERROR: create scope failed: Result.TYPE_MISMATCH. It seems like the variant type is wrong maybe? I don't know why.
12-18-2023 11:42 AM
Hi Chriss,
If you take a closer look into the Scope.py you can see, that the permissions in this Flatbuffer are not Strings but Vector of Strings. Here is how I got it working.
builder = flatbuffers.Builder(1024)
permissionsRW = builder.CreateString('test-node/**')
Scope.StartPermissionsRwVector(builder, 1)
builder.PrependSOffsetTRelative(permissionsRW)
permissionsRWVector = builder.EndVector()
description = builder.CreateString("Allow to read and write")
name = builder.CreateString("Test-node RW permission")
identifier = builder.CreateString("datalayer.test-node.rw")
Scope.ScopeStart(builder)
Scope.ScopeAddIdentifier(builder, identifier)
Scope.ScopeAddName(builder, name)
Scope.ScopeAddDescription(builder, description)
Scope.ScopeAddPermissionsRw(builder, permissionsRWVector)
scope = Scope.ScopeEnd(builder)
builder.Finish(scope)
variant = Variant()
variant_result = variant.set_flatbuffers(builder.Output())
if variant_result is not Result.OK:
print("ERROR: creating variant flatbuffer: ", variant_result, flush=True)
result, var= datalayer_client.create_sync("datalayer/security/scopes", variant)
if result is not Result.OK:
print("ERROR: create scope failed: ", result, flush=True)
Best regards,
Nick
05-24-2024 04:20 PM