Xlwings + Pygatt: Storing object from Pygatt into Excel VBA - user-defined-functions

I am able to send-receive data between VBA code and Python UDF using xlwings.
I am using Pygatt in the Python UDF to communicate with BLE devices.
I am storing information in one of the sheets in Excel and accessing it in Python UDF as required.
My problem is, i am unable to store the value of adapter and use in another UDF:-
adapter = pygatt.BGAPIBackend(serial_port=str(comPort))
adapter.start()
My idea is store the adapter value in one of the cells in Excel sheet, then access it in another UDF.
Following are my UDFs:-
#Function to open BLE connection based on the comport number
#xw.func
#xw.sub
def ComPortSelect(strP):
wb = xw.Book.caller()
comPort = str(strP)
comPort = 'COM'+comPort
adapter = pygatt.BGAPIBackend(serial_port=str(comPort))
adapter.start()
listOfDevices = adapter.scan()
wb.sheets['Sheet1'].range('A1').value = adapter **<----- Gives error**
#Function to use the *adapter* and execute other Pygatt functions
#xw.func
#xw.sub
def ConnectToSelectedDevice(device):
adapter = wb.sheets['Sheet1'].range('A1').value **<--**
deviceHandle = adapter.connect(device)
.
.
.
How can i store the required object and use it in other UDF?
Is there any other correct way to achieve this?
Thanks a lot in advance!!!

I used global variable and shared it among the Python UDFs.
I moved ahead with this solution.

Related

Azure Forms Recognizer - Saving output results SDK Python

When I used the API from Forms Recognizer, it returned a JSON file. Now, I am using Form Recognizer with SDK and Python, and it returns a data type that seems to be specific from the library azure.ai.formrecognizer.
Does anyone know how to save the data acquired from Form Recognizer SDK Python in a JSON file like the one received from Form Recognzier API?
from azure.ai.formrecognizer import FormRecognizerClient
from azure.identity import ClientSecretCredential
client_secret_credential = ClientSecretCredential(tenant_id, client_id, client_secret)
form_recognizer_client = FormRecognizerClient(endpoint, client_secret_credential)
with open(os.path.join(path, file_name), "rb") as fd:
form = fd.read()
poller = form_recognizer_client.begin_recognize_content(form)
form_pages = poller.result()
Thanks for your question! The Azure Form Recognizer SDK for Python provides helper methods like to_dict and from_dict on the models to facilitate converting the data type in the library to and from a dictionary. You can use the dictionary you get from the to_dict method directly or convert it to JSON.
For your example above, in order to get a JSON output you could do something like:
poller = form_recognizer_client.begin_recognize_content(form)
form_pages = poller.result()
d = [page.to_dict() for page in form_pages]
json_string = json.dumps(d)
I hope that answers your question, please let me know if you need more information related to the library.
Also, there's more information about our models and their methods on our documentation page here. You can use the dropdown to select the version of the library that you're using.

osquery extension in Ruby - create new table

I'm trying to implement an extension for osquery in Ruby.
I found some libs and examples doing the same in Java, Node and Python, but nothing helpful implemented in Ruby language.
According to this documention, it's possible generating the code using Thrift: https://osquery.readthedocs.io/en/stable/development/osquery-sdk/#thrift-api
The steps I did, so far:
Generated the code using thrift -r --gen rb osquery.thrift
Created a class and some code to connect to the server and register the extension
This is the code of the class
# include thrift-generated code
$:.push('./gen-rb')
require 'thrift'
require 'extension_manager'
socket = Thrift::UNIXSocket.new(<path_to_socket>)
transport = Thrift::FramedTransport.new(socket)
protocol = Thrift::BinaryProtocol.new(transport)
client = ExtensionManager::Client.new(protocol)
transport.open()
info = InternalExtensionInfo.new
info.name = "zzz"
info.version = "1.0"
extension = ExtensionManager::RegisterExtension_args.new
extension.info = info
client.registerExtension(extension, {'table' => {'zzz' => [{'name' => 'TEXT'}]}})
To get the <path_to_socket> you can use:
> osqueryi --nodisable_extensions
osquery> select value from osquery_flags where name = 'extensions_socket';
+-----------------------------------+
| value |
+-----------------------------------+
| /Users/USERNAME/.osquery/shell.em |
+-----------------------------------+
When I try to get this table using osqueryi, I don't see the table when I run select * from osquery_registry;.
Have anybody by any chance implemented an osquery extension already? I'm stuck and I don't know how to proceed from here.
I don't think I've seen anyone make a ruby extension, but once you have the thrift side, it should be pretty simple.
As a tool, osquery supports a lot of options. So there's no single way to do this. Generally speaking, extensions run as their own process and the communicate over that thrift socket.
Usually, they're very simple and osquery invokes extensions directly with appropriate command line arguments. This is alluded to in the doc you linked with the example accepting --interval, --socket, and --timeout. If you do this, you'll want to look at osquery's --extensions_autoload and --extensions_require option. (I would recommend this route)
Less common, is to start osquery with a specified socket path using --extensions_socket. And then your extension can use that. This way is more common is the extension cannot be a simple binary, and instead is a large complex system.
I find myself playing around with thrift via ruby. And it seems to work if I used a BufferedTransport:
socket = Thrift::UNIXSocket.new('/tmp/osq.sock')
transport = Thrift::BufferedTransport.new(socket)
protocol = Thrift::BinaryProtocol.new(transport)
client = ExtensionManager::Client.new(protocol)
transport.open()
client.ping()
client.query("select 1")

How do we import the listener events of LibreOffice Writer in Visual Basic 6

How do we import the listener events of LibreOffice writer in Visual Basic 6?
I am trying to create a UNO service to get container listener event like following code,
Dim oListener As Object
oListener = CreateUnoListener("ContListener_",
"com.sun.star.container.XContainerListener")
I am getting an error
Compile error : Sub or Function not defined
Can anyone please help?
As explained here, CreateUnoListener does not work in VB6. So instead, it is necessary to implement the listener interface a different way.
Here is a VBScript example from https://wiki.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Document_Events.
set xContext = objServiceManager.getPropertyValue( "DefaultContext" )
set xCoreReflection = xContext.getValueByName( "/singletons/com.sun.star.reflection.theCoreReflection" )
set xClass = xCoreReflection.forName( "com.sun.star.document.XEventBroadcaster" )
set xMethod = xClass.getMethod( "addEventListener" )
dim invokeargs(0)
invokeargs(0) = myListener
set value = objServiceManager.Bridge_GetValueObject()
call value.InitInOutParam("[]any", invokeargs)
call xMethod.invoke( objDocument, value )
Define a subroutine called myListener.
It may also help to check out the information at https://www.openoffice.org/udk/common/man/tutorial/office_automation.html.
There is a discussion of someone attempting similar code at https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=14217, although the final solution uses Javascript.
Disclaimer: I do not have any way to test VB6 code, so this information may not be entirely accurate. If you switch to Python or another language commonly used with LibreOffice then I can be of more help.

I am trying to upgrade my script from Cloudera hbase 4(CDH4) version to (CDH5)

def getRegions(config, servername)
connection = HConnectionManager::getConnection(config)
parts = servername.split(',')
puts parts
rs = connection.getHRegionConnection(parts[0], parts[1].to_i)
return rs.getOnlineRegions()
end
I am trying to make this code compatible with CDH5. I have looked into CDH5 library but unable to find exact solution.
I am using
connection = ConnectionFactory::createConnection(config) which returns Connection object.
I want list of onlineRegions on given server.
Have a look the following api's
Admin.html#getClusterStatus()
ClusterStatus.html#getServers()
Admin.html#getOnlineRegions(org.apache.hadoop.hbase.ServerName)
Note : In older versions, Some of the Admin functions live in HBaseAdmin class. (Rest of the usage should be same/similar)
Hopefully, that should help you.

Burning CD/DVD using IMAPI2.dll

I am trying to add the facility to burn CD/DVD into my app by using IMAPI2.dll. I am using Microsoft Visual FoxPro 9 SP 2 to devolopment. When I invork the method Write() which is a member of the IMAPI2.MsftDiscFormat2Data class (Last line of the sample code) Visual FoxPro gives the following error message. Error Msg : "OLE error code 0x80004002: No such interface supported."
OS : Windows 7
Please Help.
**--Creating MsftDiscMaster2 object to connect to optical drives.
loDiscMaster = CREATEOBJECT("IMAPI2.MsftDiscMaster2")
**--Creating MsftDiscRecorder2 object for the specified burning device.
loRecorder = CREATEOBJECT("IMAPI2.MsftDiscRecorder2")
lcUniqueId = loDiscMaster.ITEM(0)
loRecorder.InitializeDiscRecorder(lcUniqueId)
**--Create an image stream for the specified directory.
loFileSystem = CREATEOBJECT("IMAPI2FS.MsftFileSystemImage")
loRootDir = loFileSystem.Root
**--Create the new disc format and set the recorder.
loDataWriter = CREATEOBJECT("IMAPI2.MsftDiscFormat2Data")
loDataWriter.Recorder = loRecorder
loDataWriter.ClientName = "IMAPIv2 TEST"
loFileSystem.ChooseImageDefaults(loRecorder)
**--Add the directory and its contents to the file system.
loRootDir.AddTree("F:\VSS",.F.)
**--Create an image from the file system
loResultImage = loFileSystem.CreateResultImage()
loStream = loResultImage.ImageStream
**--Write stream to disc using the specified recorder.
loDataWriter.Write(loStream)
I'm afraid you are out of luck there. FoxPro interacts with COM objects at a fairly high level. In fact, it works in much the same way that VBScript interacts with COM. Normally, if your code works in VBScript, it will also work in FoxPro.
This is actually a common problem with some ActiveX/COM libraries. While the objects implemented in imapi2.dll and imapi2fs.dll all use IDispatch - the highest level and most interoperable form of COM interface - some of the method parameters, method returns, and properties of those objects are not IDispatch.
Specifically, the ImageStream property returns something called an IStream which inherits from IUnknown instead of IDispatch. Because of this, the ImageStream property returns something that FoxPro doesn't know how to deal with. FoxPro knows that it is a COM interface, but it doesn't know how to find or call the methods on that object.

Resources