4

Python newbie here. So, please excuse if this has been asked before in a different format.

I am trying to replicate the following perl snippet in Python using the win32com module. This snippet is provided by Qualcomm for easier automation of their tools.

use Win32::OLE;
use Win32::OLE::Variant;
$prod_id = "QPSTAtmnServer.Application"; # AppId for the Automation server.
eval{ $qpst = Win32::OLE->GetActiveObject($prod_id)}; # Attempt to use a running instance.
die "$prod_id not installed" if $@;
unless (defined $qpst) { $qpst = Win32::OLE->new($prod_id, sub {$_[0]->Quit;}) or die "Cannot start $prod_id";} # Start a new instance. Call Quit when $qpst set to undef or script exits.
if (defined $qpst)
{
    $port = $qpst->GetPort("COM30001");
}

The block of python code I have till now is as follows:

import win32com.client
import time
import os

cmd = 'cls'
os.system(cmd)
cmd = 'start C:\\LAB\\exe\\pskill.exe QPSTConfig'
os.system(cmd)
cmd = 'start C:\\LAB\\exe\\pskill.exe QPSTServer'
os.system(cmd)
cmd = 'start C:\\LAB\\exe\\pskill.exe AtmnServer'
os.system(cmd)
time.sleep(2)
_path = os.getcwd()
qpst = win32com.client.Dispatch('QPSTAtmnServer.Application')
time.sleep(5)
if (qpst is None):
    print('Darn!')
else:
    port = qpst.GetPort('30001')
    print(port)

and it throws the following error:

Traceback (most recent call last):

File "xxxx.py", line 20, in module

port = qpst.GetPort('30001')

TypeError: 'NoneType' object is not callable

After reading a couple of posts it seems like the method (GetPort) is not registering as a method after all.

Is that correct analysis?

If yes, how do I make Python interpret it as a method?

If not, what is going on here with the error?

Thanks in advance for the help!

user3285807
  • 61
  • 1
  • 6

2 Answers2

2

It looks like I had to do couple of things to solve the issue.

  1. Use the makepy command on the "AtmnServer" OLE TypeLibrary file to create a *.py file in:

    ...\Python27\Lib\site-packages\win32com\gen_py\
    
  2. Add an extra line to actually interpret the required Method as a method (instead of as a property/attribute or something) :

    qpst._FlagAsMethod("GetPort")
    

    before the line:

    port = qpst.GetPort("COM30001")
    

Thanks again for offering to help!

Oliver
  • 27,510
  • 9
  • 72
  • 103
user3285807
  • 61
  • 1
  • 6
  • I did not use EnsureDispatch after all. The line was injected into the original snippet of code, I provided. – user3285807 Feb 11 '14 at 21:40
  • +1 Thanks for posting BTW. Since you ran with EnsureDispatch once before, that might have generated the type file. If you could remove the type files generated by EnsureDispatch and try again, it'll be good to know for sure. If you want to try this, it should be in site-packages\win32com\gen_py directory, you can delete the entire directory or just the files in the directory, but be sure to remove all files, not just the .py files. – Oliver Feb 11 '14 at 21:46
  • You read my mind! I was going to try that next. :-) Thanks again for being so resourceful! – user3285807 Feb 11 '14 at 21:49
  • Edited my answer to qualify as a more comprehensive answer. You were right about the intermediate makepy command being crucial. – user3285807 Feb 12 '14 at 00:11
1

Correct, it is saying that GetPort does not exist. Have you checked that the Perl version works? If you don't have Perl, you could try through Excel's VBA (open its VBA console -- you may have to enable it by following the steps here). If you can dispath the QPST from Excel VBA and do the GetPort, then something is very odd.

It could be that QPST COM interface changed since this script was written. You could try

qpst = win32com.client.gencache.EnsureDispatch(
           'QPSTAtmnServer.Application')

which will attempt to create the type library for QPST. Sometimes it finds extra objects, but if not at very least you can then browse the QPST COM from python using combrowse.py (which is part of pywin32) and try to find where that function is. Combrowse is a basic COM browser, just run \Lib\site-packages\win32com\client\combrowse.py, if need more powerful the one from visual studio is probably better.

Oliver
  • 27,510
  • 9
  • 72
  • 103
  • Hey Schollii, thanks for offering to help. Yes, the perl program does work indeed (as a temporary work-around, I am calling the perl program in my python script). Couple of questions: 1. I tried using the EnsureDispatch, FlagAsMethod route, but no such luck. I can provide the exact error it popped in a short while. I might not be using it correctly? 2. How do I use the combrowse.py module? – user3285807 Feb 09 '14 at 15:34
  • Updated my answer for now to run combrowse. Sorry EnsureDispatch did not help, can't think what else could be wrong. If you do find the answer please post for the benefit of all. – Oliver Feb 10 '14 at 16:48