I'm probably doing something very silly here, but
I'm having trouble creating a COM from a Python
program, and then using that COM object. I've
followed the steps in Chapter 5 of:
Python Programming on Win32
and in the article:
http://pyzine.com/Issue007/index.html
http://www.pyzine.com/Issue007/Section_Articles/article_PythonCOM.html?printit=1
It gives the appearance of creating the COM object,
because when I run the script (from within
PythonWin), I get this:
Registered: Nassco.TribonComDxComponent
I can see it with REGEDIT:
Nassco.TribonComDxComponent
CLSID -> (GUID from my _reg_clsid_ statement)
But when I try to run makepy (from the Tools
menu inside the PythonWin IDE), I can't find the
Text Library for it. And my test program can't
instantiate the class with a Dispatch call.
(But that's probably because the makepy can't
be run on it.)
I'm probably doing something very silly here,
but I've been looking at this for too long to
see what that is.
Would someone be able to give me a pointer on
what I'm doing wrong?
Thanks in advance.
Johan
"""Component Handling routines
NOTE: These routines are intended to be used
by NON-interactive applications
(batch or background jobs,
with no Tribon interactive application running,
scheduled jobs that have no terminal/GUI attached, ...)
Requires (import of):
win32com.client
Creator: Veronique Simon, Johan Vinke
Creation date: 2005-01-12
Modification date: 2005-07-08
Version: 1.0"""
import win32com.client as w32c
import string
import os
tbDEXobj = w32c.Dispatch(r"TBDex.TBDex.1")
# .. the following statements are required for versions of Python prior
# to 2.3 (?__) Python2.0 does not have the Boolean constants True/False
#__#True = 1
#__#False = 0
#.. ____________________________________________________________________
#.. begin "external" function definitions here
#.. NOTE: "external" functions can only be called by methods within
#.. this module; they cannot be called from outside the module
#.. ____________________________________________________________________
#.. ____________________________________________________________________
#.. begin class definition here
#.. ____________________________________________________________________
#__#class TribonComponent:
#__#class NasTbComponent:
class TribonComponent:
_public_methods_ = [ 'setCompoName',
'checkComponentExist',
'getCompoType',
'getCompoMatCodeInt',
'getCompoNasscoPartNumber',
'getCompoDBNasscoPartNumber' ]
#__# 'getCompo',
#__# 'getCompo',
#__# ]
_public_attrs_ = [ 'componentName',
'componentExists',
'componentType' ]
_readonly_attrs_ = [ 'componentName',
'componentExists',
'componentType' ]
_reg_progid_ = "Nassco.TribonComDxComponent"
_reg_verprogid_ = "Nassco.TribonComDxComponent.01"
_reg_clsid_ = "{010478BC-A1CF-400C-915D-AD62392C10D8}"
_reg_desc_ = "Nassco.Tribon.Component Data Extraction COM Server"
#.. define names of attributes/fields
#__# componentName = None
#__# ... should the _name_ field be initialized to None
#__# OR the empty string '' ?_____?
componentName = ''
componentExists = False
componentType = 0
#
#__# componentMatCodeInt = 0
#__# componentNasscoPartNumber = ''
#__# validComponent = False
#.. ____________________________________________________________________
def __init__( self, inputName=None ) :
self.componentName = ''
self.componentExists = False
self.componentType = 0
#__# self.componentMatCodeInt = 0
#__# self.componentNasscoPartNumber = ''
return()
#.. ____________________________________________________________________
# should this method be public or private? If the setCompoName
def checkComponentExist( self, inputName) :
#default return value
componentExists = False
if not inputName :
self.componentName = ''
self.componentExists = False
return(componentExists)
if inputName == '' :
self.componentName = ''
self.componentExists = False
return(componentExists)
extractStatement = "COMPO('" + inputName + "').NAME"
try :
tbDEXobj.DoDataExtraction(extractStatement)
extractedComponentName = tbDEXobj.GetValue()
if extractedComponentName == inputName:
componentExists = True
self.componentExists = True
self.componentName = inputName
except:
# -----
errorMessageDo_005 = \
' .. MessageDo_005: error in function checkComponentExist '
print errorMessageDo_005
pass
# -----
return(componentExists)
#.. ____________________________________________________________________
#__# def setTribonComponentName( self, inputName=None ) :
def setCompoName( self, inputName=None ) :
#default return value
componentExists = False
if not inputName :
self.componentName = ''
self.componentExists = False
return(componentExists)
if inputName == '' :
self.componentName = ''
self.componentExists = False
return(componentExists)
self.componentName = ''
self.componentExists = False
if inputName :
if self.checkComponentExist(inputName) :
self.componentName = inputName
self.componentExists = True
return(self.componentExists)
#.. ____________________________________________________________________
def getCompoType(self) :
#default return value
extractedTypeString = ""
extractStatement = "COMPO('" + self.componentName + "').TYPE"
try :
tbDEXobj.DoDataExtraction(extractStatement)
extractedTypeString = tbDEXobj.GetValue()
except:
pass
return(extractedTypeString)
#.. ____________________________________________________________________
def getCompoMatCodeInt(self) :
#Creator : Johan Vinke
#Modification date: 01/14/05
#default return value
tbMatCodeInt = 0
extractStatement = "COMPO('" + self.componentName + "').GEN_P.MTRL_C"
try :
tbDEXobj.DoDataExtraction(extractStatement)
tbMatCodeInt = tbDEXobj.GetValue()
except:
pass
return(tbMatCodeInt)
#.. ____________________________________________________________________
def getCompoDBNasscoPartNumber( self ) :
#__#"""Function to return the _RAW_ Nassco Part Number
#__#for a Component, as specified by a inputName.
#__#No manipulation of the field found in the database is done.
#__#"""
#default return value
rawNasscoPartNumber = ""
extractStatement = "COMPO('" + self.componentName + "').MAT"
try :
tbDEXobj.DoDataExtraction(extractStatement)
rawNasscoPartNumber = tbDEXobj.GetValue()
except:
pass
return(rawNasscoPartNumber)
#.. ____________________________________________________________________
def getCompoNasscoPartNumber( self ) :
#__#"""Function to return the Nassco Part Number for a Component,
#__#as specified by a inputName. The name of the field in
#__#Tribon terms is MATERIAL (abbreviation MAT).
#__#For invalid or missing Part Numbers in the Component Database,
#__#N/A is returned. """
#default return value
nasscoPartNumber = ""
try :
nasscoPartNumber = self.getCompoDBNasscoPartNumber()
if (nasscoPartNumber == "") or ( (nasscoPartNumber[0]).upper() ==
"X" ):
nasscoPartNumber = "N/A"
elif ( (nasscoPartNumber[:4]).upper() == "NONE" ):
nasscoPartNumber = "N/A"
except:
pass
return(nasscoPartNumber)
#.. ____________________________________________________________________
#__#
#__#def getCompo...(self) :
#__#
#__# #default return value
#__# returnVal = False
#__#
#__# return(returnVal)
#__#
#.. ____________________________________________________________________
#.. ___.code.Fragment.to.Register.the.COM.______________________________
#.. ____________________________________________________________________
if __name__ == '__main__' :
print ".... Registering COM Server ... "
import win32com.server.register
win32com.server.register.UseCommandLine(TribonComponent)
#.. ____________________________________________________________________
#.. ___.end.NasTbComponent.py.__________________________________________
#.. ____________________________________________________________________
#
# NAME:
# Test_COM5_Driver_03.py
#
# PURPOSE:
#
# ... import of standard Python modules (libraries?)
import string
import sys
# .... # do NOT import posix directly; use import os instead
import os
import shutil
import time
# ... import of KCS-supplied Python modules (libraries?)
#__#import kcs_ui
#__#import kcs_util
import win32com.client as w32c
# ... import of Nassco-written modules
#__#nasTbComponentCOMObj = w32c.Dispatch(r"NasTbComponent3.01")
#__#tbCompObj = nasTbComponentCOMObj.NasTbComponent3()
tbCompObj = w32c.Dispatch(r"Nassco.TribonComDxComponent.01")
#__#tbCompObj = w32c.Dispatch(r"{010478BC-A1CF-400C-915D-AD62392C10D8}")
#.. ____________________________________________________________________
#.. begin function definition(s) here
#.. ____________________________________________________________________
def perfTestCOM1Component(testComponentName) :
errorReturnStatus = False
pipeComponentFound = False
#__# okToProceed = False
okToProceed = True
if okToProceed:
componentExistsFlag = tbCompObj.setCompoName(testComponentName)
#__# debugMessage_01c = ' .. debugMessage_01c: componentExistsFlag= '
+ repr(componentExistsFlag)
#__# print debugMessage_01c
## .. temporarily reset flag to indicate component does.NOT exist
#__# componentExistsFlag = False
if componentExistsFlag :
componentType = tbCompObj.getCompoType()
#__# debugMessage_01d = ' .. debugMessage_01d: component ' +
\testComponentName
#__# debugMessage_01d = debugMessage_01d + ' is Type:' +
repr(componentType)
#__# print debugMessage_01d
componentMatCodeInt = tbCompObj.getCompoMatCodeInt()
#__# debugMessage_01h = ' .. debugMessage_01h:
componentMatCodeInt= ' + repr(componentMatCodeInt)
#__# print debugMessage_01h
componentDBNasscoPartNumber =
tbCompObj.getCompoDBNasscoPartNumber()
#__# debugMessage_01j = ' .. debugMessage_01j:
componentDBNasscoPartNumber= ' + repr(componentDBNasscoPartNumber)
#__# print debugMessage_01j
#__##__# kcs_ui.message_confirm(debugMessage_01j)
#__# componentNasscoPartNumber =
tbCompObj.getCompoNasscoPartNumber()
#__# debugMessage_01k = ' .. debugMessage_01k:
componentNasscoPartNumber= ' + repr(componentNasscoPartNumber)
#__# print debugMessage_01k
#__##__# kcs_ui.message_confirm(debugMessage_01k)
if componentType == 7101 :
pipeComponentFound = True
else: #..# else clause for if componentExistsFlag :
#__# errorMessage_77p = ' .. errorMessage_77p: ERROR! ' +
testComponentName + \
#__# ' is NOT a valid ComponentName!!'
errorMessage_77p = ' .. msg_77p: ERROR! ' + testComponentName + \
' is NOT a valid ComponentName!!'
print errorMessage_77p
errorReturnStatus = True
else:
debugMessage_01x = ' .. debugMessage_01x: okToProceed is False, not
checking ComponentName'
print debugMessage_01x
#__# kcs_ui.message_confirm(debugMessage_01x)
return(errorReturnStatus, pipeComponentFound)
#.. ____________________________________________________________________
#.. ___.begin.MAINline._________________________________________________
#__#try:
testMode = True
okToProceed = True
if testMode:
print '\n\n___________.Test_COM5_Driver_03:start.new.run. ______'
message = 'Please enter filename (file containing list of Component Names)'
TIS_DevRootDir = '//Eng/Prod/Group/TIS/Dev'
supportingToolsNDataDir = TIS_DevRootDir + '/Dev_Support_Tools_n_Data'
data_FilesDir = supportingToolsNDataDir + '/List.Data.Files'
listComponentNamesFile = 'AllComponents.NamesOnly.txt'
listComponentNamesFullFile = data_FilesDir + '/' +
listComponentNamesFile
print ' ... listComponentNamesFullFile =',listComponentNamesFullFile
#__# now, open the file, read a line at a time (each line should be a
#__# a component.Name), and call the performance.Test function for
#__# each component
pipeCounter = 0
validComponentCounter = 0
if okToProceed:
startTime = time.time()
startTimeTuple = time.localtime(startTime)
ISO_DateTimeFormatString = '%Y-%m-%dT%H:%M:%S'
formattedStartTimeString = time.strftime(ISO_DateTimeFormatString,
startTimeTuple)
#__# infoMessage_44a = ' .. infoMessage_44a:
formattedStartTimeString: ' + repr(formattedStartTimeString)
#__# infoMessage_44a = ' .. msg_44a: start.Time: ' +
repr(formattedStartTimeString)
infoMessage_44a = '\n __________________________________________
start.Time: ' + \
repr(formattedStartTimeString) + '\n'
print infoMessage_44a
#__# kcs_ui.message_confirm(infoMessage_44a)
inputFile = open(listComponentNamesFullFile, 'r')
inFileD = inputFile.fileno()
for lineFromFile in inputFile:
#__# testComponentName = lineFromFile
testComponentName = lineFromFile.rstrip('\n')
#__# debugMessage_01b = ' .. debugMessage_01b: would test
Component: ' + testComponentName
#__# debugMessage_01b = ' .. debugMessage_01b: would test
Component: ' + repr(testComponentName)
#__# print debugMessage_01b
#__# kcs_ui.message_confirm(debugMessage_01b)
(errorStatus, pipeFound) =
perfTestCOM1Component(testComponentName)
if not errorStatus :
validComponentCounter = validComponentCounter + 1
if pipeFound :
pipeCounter = pipeCounter + 1
inFileD = inputFile.close()
endTime = time.time()
elapsedSeconds = round( (endTime - startTime), 2)
infoMessage_55a = ' .. msg_55a: elapsedSeconds: ' +
str(elapsedSeconds)
print infoMessage_55a
infoMessage_55b = ' .. msg_55b: numberComponentsFound: ' +
repr(validComponentCounter)
print infoMessage_55b
infoMessage_55c = ' .. msg_55c: numberPipesFound: ' +
repr(pipeCounter)
print infoMessage_55c
endTimeTuple = time.localtime(endTime)
ISO_DateTimeFormatString = '%Y-%m-%dT%H:%M:%S'
formattedEndTimeString = time.strftime(ISO_DateTimeFormatString,
endTimeTuple)
infoMessage_55d = ' .. msg_55d: endTimeTuple: ' + repr(endTimeTuple)
print infoMessage_55d
infoMessage_55e = ' .. msg_55e: formattedEndTimeString: ' +
repr(formattedEndTimeString)
print infoMessage_55e
#__# kcs_ui.message_confirm(infoMessage_55a)
#__# kcs_ui.message_confirm(infoMessage_55b)
#__# kcs_ui.message_confirm(infoMessage_55c)
#__# kcs_ui.message_confirm(infoMessage_55d)
#__# kcs_ui.message_confirm(infoMessage_55e)
else:
debugMessage_01x = ' .. debugMessage_01x: User selected CANCEL, not
checking ComponentName'
print debugMessage_01x
#__# kcs_ui.message_confirm(debugMessage_01x)
#__#except:
#__# debugMessage_01y = ' .. debugMessage_01y: some kind of error
occurred in ..ExtractDriver '
#__# print debugMessage_01y
debugMessage_01z = ' .. debugMessage_01z: end.of.main
Test_COM5_Driver_03.py\n'
print debugMessage_01z
#__#kcs_ui.message_confirm(debugMessage_01z)
#.. ____________________________________________________________________
#.. ___.end.Test_COM5_Driver_03.py._____________________________________
#.. ____________________________________________________________________
Here's some info on the version of PythonWin I have installed
on my PC (as of 2005-02-10, machine 3RCYP11).
When PythonWin starts, I get this in the interactive window:
PythonWin 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on
win32.
Portions Copyright 1994-2004 Mark Hammond ([EMAIL PROTECTED]) -
see 'Help/About PythonWin' for further copyright information.
>>>
The Help -> About PythonWin reports this:
Python 2.3 pywin32 (build 203)
_______________________________________________
Python-win32 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-win32