473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can't see the forest for the trees - when reading file, only processingfirst line

Hi Everyone,
The attached code creates client connections to websphere queue managers
and then processes an inquiry against them.

The program functions when it gets options from the command line.

It also works when pulling the options from a file.

My issue is that it only processes the first line of the file.

Does anyone know why this might be the case?

The use_file() function should be returning each file line.

Any help you can provide on this would be greatly appreciated.

Thanks
#!/usr/bin/python
#
# Programmer: Andrew Robert
#
# Function: Generic method to extract information from a queue manager
# This script is intended to be run from the command line to
return values
# based on supplied keys
#
# Depending on whether the -d command switch is used or not,
the output of this script
# will go to the screen or be e-mailed
#
# The e-mailer function included in this program is set use
the generic smtp library
# instead of sendmail or procmail.
#
# This is a design decision since the program is intended
primarily to run
# on a Windows platform.
#
#
#
#
#
#
def usage():
print
"""
usage: test_orig.py [options]

options:
-h, --help show this help message and exit
-mQMANAGER, --qmanager=QMANAGER
Queue Manager to inquire against
-sHOST, --server=HOST
Host the que manager resides on
-cCHECK, --check=CHECK
Test object if it is
less/equal/greater
-pPORT, --port=PORT Port queue manager listens on
-oOBJECT, --object=OBJECT
Queue object being inquired on
-kKEY, --key=KEY object attribute to be inquired
about
-tMTO, --to=MTO e-mail address the report will go to
-q, --quiet optional - just returns the value
-fFILE, --file=FILE Pull command strings from FILE
-d, --display optional - use sends output to
e-mail
"""
#
# Trap signal interrupts if they occur
#
def signal_handler(signal, frame):
print 'Signal trapped %s' % signal
sys.exit(0)
#
# Read a file into a list
#
def use_file():
myfile = open(options.filename)
while True:
line=myfile.readline()
print line
if len(line) == 0:
myfile.close()
break
else:
return line
myfile.close()

def check_params():
if options.report is not None:
if options.mto is None:
print "\nAsked to send via e-mail but no destination address supplied"
usage()
sys.exit(1)
if options.key is None:
print"\n\nPlease enter a valid key to inquire upon.\n\nPlease check
your options and try again"
usage()
sys.exit(1)

if ( options.port <="1414" ) or ( options.port >="1419" ):
print "\n\nAn invalid port number was used. \nValid port numbers
are 1415,1416,1417, and 1418."
print "\nPlease check the port number and try again"
usage()
sys.exit(1)

#
#
# Test return value
#
def testval(inc,value):
vallen = int(value)
if vallen > inc :
return "\nless than test value\n"
elif vallen < inc :
return "\nexceeds test value\n"
else:
return "\nequal to test value\n"
# Validate e-mail addresses based on lexical rules set forth in RFC 822
# Ported from Recipe 3.9 in Secure Programming Cookbook for C and C++ by
# John Viega and Matt Messier (O'Reilly 2003)
#
# If the supplied email address is syntactically valid, isAddressValid()
# will return 1; otherwise, it will return 0.
#
def isAddressValid(addr):
#
# First we validate the name portion (name@domain)
#
c = 0
while c < len(addr):
if addr[c] == '"' and (not c or addr[c - 1] == '.' or addr[c
- 1] == '"'):
c = c + 1
while c < len(addr):
if addr[c] == '"': break
if addr[c] == '\\' and addr[c + 1] == ' ':
c = c + 2
continue
if ord(addr[c]) < 32 or ord(addr[c]) >= 127: return 0
c = c + 1
else: return 0
if addr[c] == '@': break
if addr[c] != '.': return 0
c = c + 1
continue
if addr[c] == '@': break
if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
if addr[c] in rfc822_specials: return 0
c = c + 1
if not c or addr[c - 1] == '.': return 0

# Next we validate the domain portion (name@domain)
domain = c = c + 1
if domain >= len(addr): return 0
count = 0
while c < len(addr):
if addr[c] == '.':
if c == domain or addr[c - 1] == '.': return 0
count = count + 1
if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
if addr[c] in rfc822_specials: return 0
c = c + 1

return count >= 1

#
# Test user supplied keys to ensure they are valid
#
def test_valid_keys(key):
if valid_keys.has_key(key):
return
else:
print '\n\t\t\tInvalid key',key,'\n\n\n'
usage()
sys.exit(1)

#
# Test user supplied keys to ensure they are numeric
#
def test_numeric(key):
if numeric_keys.has_key(key):
return
else:
print '\n\t\t\tString values cannot be numerically tested',key,'\n\n\n'
usage()
sys.exit(1)

#
# SMTP mailer function
#
def mail(serverURL=None, sender='', to='', subject='', text=''):
import smtplib

"""
Usage:
mail('my.mailserver.com', 's************@wherever.com',
't************@wherever.com', 'test', 'This is a test')
"""

headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to,
subject)
message = headers + text
mailServer = smtplib.SMTP(serverURL)
check_valid=isAddressValid(to)
if check_valid==1 :
mailServer.sendmail(sender, to, message)
mailServer.quit()
else:
print "\n\nDestination e-mail address %s is invalid. \n\n\tPlease
check and run again" % to
usage()
sys.exit(1)
def lookup( qmgr=' ', command='', object=''):
import pymqi, pymqe, CMQC
#
# Connect to remote queue manager via client connection
#
m = pymqi.QueueManager(None)
try:
m.connectTCPClient(qmgr,pymqi.cd(),"SYSTEM.ADMIN.S VRCONN",command)
except pymqi.MQMIError, a:
print '\n\nScript error detected:\n', a
sys.exit(1)

#
# Inquire about the passed queue object and return value
#
q = pymqi.Queue(m, object)
try:
arg = 'q.inquire(CMQC.'+valid_keys[vt[count]]+')'
return eval(arg)
except pymqi.MQMIError, a:
print '\n\nScript error detected:\n', a
sys.exit(1)
if __name__ == '__main__':

import sys, string, exceptions, signal
from optparse import OptionParser

#
# Mail gateway and rfc 822 variables
#
mserver = 'harrier.mfs.com'
mfrom = 'd**********@mfs.com'
rfc822_specials = '()<>@,;:\\"[]'
count =0
text =""

#
# Setup signal handlers to detect if program is
# aborted, terminated, or Ctrl-C'ed out of
#
signal.signal(signal.SIGABRT,signal_handler)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM,signal_handler)

#
# Parse command line options and automatically build help/usage display
#
parser = OptionParser()
parser.add_option("-m","--qmanager", dest="qmanager",
help="\t\tQueue Manager to inquire against"),
parser.add_option("-s","--server", dest="host",
help="\t\tHost the que manager resides on"),
parser.add_option("-c","--check", dest="check",
help="\t\tTest object if it is less/equal/greater"),
parser.add_option("-p","--port", type="string", dest="port",
help="\t\tPort queue manager listens on"),
parser.add_option("-o","--object", dest="object",
help="\t\tQueue object being inquired on"),
parser.add_option("-k", "--key",dest="key",
help="\t\tobject attribute to be inquired about"),
parser.add_option("-t","--to", type="string",dest="mto",
help="\t\te-mail address the report will go to"),
parser.add_option("-q","--quiet", action="store_false",dest="quiet",
help="\t\toptional - just returns the value"),
parser.add_option("-f", "--file",
action="store", type="string", dest="filename",
help="Pull command strings from FILE", metavar="FILE")
parser.add_option("-d","--display", action="store_false",dest="report",
help="\t\toptional - use sends output to e-mail")
(options, args) = parser.parse_args()

if options.filename is not None:
line = use_file()
(options,args) = parser.parse_args(line.split())
check_params()
else:
check_params()

#
# Key dictionary
#
valid_keys={'AlterationDate':'MQCA_ALTERATION_DATE ',
'AlterationTime':'MQCA_ALTERATION_TIME',
'BackoutRequeueName':'MQCA_BACKOUT_REQ_Q_NAME',
'BackoutThreshold':'MQIA_BACKOUT_THRESHOLD',
'BaseQName':'MQCA_BASE_Q_NAME',
'ClusterDate':'MQCA_CLUSTER_DATE',
'ClusterName':'MQCA_CLUSTER_NAME',
'ClusterNamelist':'MQCA_CLUSTER_NAMELIST',
'ClusterQType':'MQIA_CLUSTER_Q_TYPE',
'ClusterTime':'MQCA_CLUSTER_TIME',
'CreationDate':'MQCA_CREATION_DATE',
'CreationTime':'MQCA_CREATION_TIME',
'CurrentQDepth':'MQIA_CURRENT_Q_DEPTH',
'DefBind':'MQIA_DEF_BIND',
'DefInputOpenOption':'MQIA_DEF_INPUT_OPEN_OPTION',
'DefPersistence':'MQIA_DEF_PERSISTENCE',
'DefPriority':'MQIA_DEF_PRIORITY',
'DefinitionType':'MQIA_DEFINITION_TYPE',
'DistLists':'MQIA_DIST_LISTS',
'HardenGetBackout':'MQIA_HARDEN_GET_BACKOUT',
'HighQDepth':'MQIA_HIGH_Q_DEPTH',
'InhibitGet':'MQIA_INHIBIT_GET',
'InhibitPut':'MQIA_INHIBIT_PUT',
'InitiationQName':'MQCA_INITIATION_Q_NAME',
'MaxMsgLength':'MQIA_MAX_MSG_LENGTH',
'MaxQDepth':'MQIA_MAX_Q_DEPTH',
'MsgDeliverySequence':'MQIA_MSG_DELIVERY_SEQUENCE' ,
'MsgDeqCount':'MQIA_MSG_DEQ_COUNT',
'MsgEnqCount':'MQIA_MSG_ENQ_COUNT',
'OpenInputCount':'MQIA_OPEN_INPUT_COUNT',
'OpenOutputCount':'MQIA_OPEN_OUTPUT_COUNT',
'ProcessName':'MQCA_PROCESS_NAME',
'QDepthHighEvent':'MQIA_Q_DEPTH_HIGH_EVENT',
'QDepthHighLimit':'MQIA_Q_DEPTH_HIGH_LIMIT',
'QDepthLowEvent':'MQIA_Q_DEPTH_LOW_EVENT',
'QDepthLowLimit':'MQIA_Q_DEPTH_LOW_LIMIT',
'QDepthMaxEvent':'MQIA_Q_DEPTH_MAX_EVENT',
'QDesc':'MQCA_Q_DESC',
'QMgrIdentifier':'MQCA_Q_MGR_IDENTIFIER',
'QMgrName':'MQCA_CLUSTER_Q_MGR_NAME',
'QName':'MQCA_Q_NAME',
'QNames':'MQCACF_Q_NAMES',
'QServiceInterval':'MQIA_Q_SERVICE_INTERVAL',
'QServiceIntervalEvent':'MQIA_Q_SERVICE_INTERVAL_E VENT',
'QType':'MQIA_Q_TYPE',
'RemoteQMgrName':'MQCA_REMOTE_Q_MGR_NAME',
'RemoteQName':'MQCA_REMOTE_Q_NAME',
'RetentionInterval':'MQIA_RETENTION_INTERVAL',
'Scope':'MQIA_SCOPE',
'Shareability':'MQIA_SHAREABILITY',
'TimeSinceReset':'MQIA_TIME_SINCE_RESET',
'TriggerControl':'MQIA_TRIGGER_CONTROL',
'TriggerData':'MQCA_TRIGGER_DATA',
'TriggerDepth':'MQIA_TRIGGER_DEPTH',
'TriggerMsgPriority':'MQIA_TRIGGER_MSG_PRIORITY',
'TriggerType':'MQIA_TRIGGER_TYPE',
'Usage':'MQIA_USAGE',
'XmitQName':'MQCA_XMIT_Q_NAME'}

#
# Keys that can be numerically compared against
#
numeric_keys={'AlterationDate':'MQCA_ALTERATION_DA TE',
'AlterationTime':'MQCA_ALTERATION_TIME',
'BackoutThreshold':'MQIA_BACKOUT_THRESHOLD',
'DefPriority':'MQIA_DEF_PRIORITY',
'ClusterDate':'MQCA_CLUSTER_DATE',
'ClusterTime':'MQCA_CLUSTER_TIME',
'CreationDate':'MQCA_CREATION_DATE',
'CreationTime':'MQCA_CREATION_TIME',
'CurrentQDepth':'MQIA_CURRENT_Q_DEPTH',
'HighQDepth':'MQIA_HIGH_Q_DEPTH',
'MaxMsgLength':'MQIA_MAX_MSG_LENGTH',
'MaxQDepth':'MQIA_MAX_Q_DEPTH',
'MsgDeliverySequence':'MQIA_MSG_DELIVERY_SEQUENCE' ,
'MsgDeqCount':'MQIA_MSG_DEQ_COUNT',
'MsgEnqCount':'MQIA_MSG_ENQ_COUNT',
'OpenInputCount':'MQIA_OPEN_INPUT_COUNT',
'OpenOutputCount':'MQIA_OPEN_OUTPUT_COUNT',
'QDepthHighEvent':'MQIA_Q_DEPTH_HIGH_EVENT',
'QDepthHighLimit':'MQIA_Q_DEPTH_HIGH_LIMIT',
'QMgrIdentifier':'MQCA_Q_MGR_IDENTIFIER',
'QServiceInterval':'MQIA_Q_SERVICE_INTERVAL',
'TriggerDepth':'MQIA_TRIGGER_DEPTH',
'TriggerMsgPriority':'MQIA_TRIGGER_MSG_PRIORITY'}
host_port = "%s(%s)" % (options.host.lower(), options.port)
vt = options.key.split(',')

for i in vt:
test_valid_keys(vt[count])
inq = lookup( options.qmanager.upper(), host_port, options.object)
if options.quiet is False:
text=text+str(inq)
else:
text = text+"\n%s for %s object %s is %s" % ( vt[count],
options.qmanager.upper(), options.object,inq)

if options.check is not None:
ct = options.check.split(",")
test_numeric(vt[count])
value_test=testval(inq,ct[count])
text=text+" "+value_test
count=count+1
if options.report is None:
print text
sys.exit(0)
else:
subject = "Report for queue manager %s on server %s " % (
options.qmanager.upper() , options.host.lower() )
mail( mserver, mfrom, options.mto, subject, text )
sys.exit(0)
Apr 13 '06 #1
4 2354
Am Donnerstag 13 April 2006 19:12 schrieb News:
The use_file() function should be returning each file line.


How do you think it should do that? There's a return line statement in the
function which breaks function execution right after having read the first
line.

Rethink your logic...

--- Heiko.
Apr 13 '06 #2
Suggest keeping it simple:

def use_file():
return open(options.filename).readlines()

m

News wrote:
Hi Everyone,
The attached code creates client connections to websphere queue managers
and then processes an inquiry against them.

The program functions when it gets options from the command line.

It also works when pulling the options from a file.

My issue is that it only processes the first line of the file.

Does anyone know why this might be the case?

The use_file() function should be returning each file line.

Any help you can provide on this would be greatly appreciated.

Thanks
#!/usr/bin/python
#
# Programmer: Andrew Robert
#
# Function: Generic method to extract information from a queue manager
# This script is intended to be run from the command line to
return values
# based on supplied keys
#
# Depending on whether the -d command switch is used or not,
the output of this script
# will go to the screen or be e-mailed
#
# The e-mailer function included in this program is set use
the generic smtp library
# instead of sendmail or procmail.
#
# This is a design decision since the program is intended
primarily to run
# on a Windows platform.
#
#
#
#
#
#
def usage():
print
"""
usage: test_orig.py [options]

options:
-h, --help show this help message and exit
-mQMANAGER, --qmanager=QMANAGER
Queue Manager to inquire against
-sHOST, --server=HOST
Host the que manager resides on
-cCHECK, --check=CHECK
Test object if it is
less/equal/greater
-pPORT, --port=PORT Port queue manager listens on
-oOBJECT, --object=OBJECT
Queue object being inquired on
-kKEY, --key=KEY object attribute to be inquired
about
-tMTO, --to=MTO e-mail address the report will go to
-q, --quiet optional - just returns the value
-fFILE, --file=FILE Pull command strings from FILE
-d, --display optional - use sends output to
e-mail
"""
#
# Trap signal interrupts if they occur
#
def signal_handler(signal, frame):
print 'Signal trapped %s' % signal
sys.exit(0)
#
# Read a file into a list
#
def use_file():
myfile = open(options.filename)
while True:
line=myfile.readline()
print line
if len(line) == 0:
myfile.close()
break
else:
return line
myfile.close()

def check_params():
if options.report is not None:
if options.mto is None:
print "\nAsked to send via e-mail but no destination address supplied"
usage()
sys.exit(1)
if options.key is None:
print"\n\nPlease enter a valid key to inquire upon.\n\nPlease check
your options and try again"
usage()
sys.exit(1)

if ( options.port <="1414" ) or ( options.port >="1419" ):
print "\n\nAn invalid port number was used. \nValid port numbers
are 1415,1416,1417, and 1418."
print "\nPlease check the port number and try again"
usage()
sys.exit(1)

#
#
# Test return value
#
def testval(inc,value):
vallen = int(value)
if vallen > inc :
return "\nless than test value\n"
elif vallen < inc :
return "\nexceeds test value\n"
else:
return "\nequal to test value\n"
# Validate e-mail addresses based on lexical rules set forth in RFC 822
# Ported from Recipe 3.9 in Secure Programming Cookbook for C and C++ by
# John Viega and Matt Messier (O'Reilly 2003)
#
# If the supplied email address is syntactically valid, isAddressValid()
# will return 1; otherwise, it will return 0.
#
def isAddressValid(addr):
#
# First we validate the name portion (name@domain)
#
c = 0
while c < len(addr):
if addr[c] == '"' and (not c or addr[c - 1] == '.' or addr[c
- 1] == '"'):
c = c + 1
while c < len(addr):
if addr[c] == '"': break
if addr[c] == '\\' and addr[c + 1] == ' ':
c = c + 2
continue
if ord(addr[c]) < 32 or ord(addr[c]) >= 127: return 0
c = c + 1
else: return 0
if addr[c] == '@': break
if addr[c] != '.': return 0
c = c + 1
continue
if addr[c] == '@': break
if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
if addr[c] in rfc822_specials: return 0
c = c + 1
if not c or addr[c - 1] == '.': return 0

# Next we validate the domain portion (name@domain)
domain = c = c + 1
if domain >= len(addr): return 0
count = 0
while c < len(addr):
if addr[c] == '.':
if c == domain or addr[c - 1] == '.': return 0
count = count + 1
if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
if addr[c] in rfc822_specials: return 0
c = c + 1

return count >= 1

#
# Test user supplied keys to ensure they are valid
#
def test_valid_keys(key):
if valid_keys.has_key(key):
return
else:
print '\n\t\t\tInvalid key',key,'\n\n\n'
usage()
sys.exit(1)

#
# Test user supplied keys to ensure they are numeric
#
def test_numeric(key):
if numeric_keys.has_key(key):
return
else:
print '\n\t\t\tString values cannot be numerically tested',key,'\n\n\n'
usage()
sys.exit(1)

#
# SMTP mailer function
#
def mail(serverURL=None, sender='', to='', subject='', text=''):
import smtplib

"""
Usage:
mail('my.mailserver.com', 's************@wherever.com',
't************@wherever.com', 'test', 'This is a test')
"""

headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to,
subject)
message = headers + text
mailServer = smtplib.SMTP(serverURL)
check_valid=isAddressValid(to)
if check_valid==1 :
mailServer.sendmail(sender, to, message)
mailServer.quit()
else:
print "\n\nDestination e-mail address %s is invalid. \n\n\tPlease
check and run again" % to
usage()
sys.exit(1)
def lookup( qmgr=' ', command='', object=''):
import pymqi, pymqe, CMQC
#
# Connect to remote queue manager via client connection
#
m = pymqi.QueueManager(None)
try:
m.connectTCPClient(qmgr,pymqi.cd(),"SYSTEM.ADMIN.S VRCONN",command)
except pymqi.MQMIError, a:
print '\n\nScript error detected:\n', a
sys.exit(1)

#
# Inquire about the passed queue object and return value
#
q = pymqi.Queue(m, object)
try:
arg = 'q.inquire(CMQC.'+valid_keys[vt[count]]+')'
return eval(arg)
except pymqi.MQMIError, a:
print '\n\nScript error detected:\n', a
sys.exit(1)
if __name__ == '__main__':

import sys, string, exceptions, signal
from optparse import OptionParser

#
# Mail gateway and rfc 822 variables
#
mserver = 'harrier.mfs.com'
mfrom = 'd**********@mfs.com'
rfc822_specials = '()<>@,;:\\"[]'
count =0
text =""

#
# Setup signal handlers to detect if program is
# aborted, terminated, or Ctrl-C'ed out of
#
signal.signal(signal.SIGABRT,signal_handler)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM,signal_handler)

#
# Parse command line options and automatically build help/usage display
#
parser = OptionParser()
parser.add_option("-m","--qmanager", dest="qmanager",
help="\t\tQueue Manager to inquire against"),
parser.add_option("-s","--server", dest="host",
help="\t\tHost the que manager resides on"),
parser.add_option("-c","--check", dest="check",
help="\t\tTest object if it is less/equal/greater"),
parser.add_option("-p","--port", type="string", dest="port",
help="\t\tPort queue manager listens on"),
parser.add_option("-o","--object", dest="object",
help="\t\tQueue object being inquired on"),
parser.add_option("-k", "--key",dest="key",
help="\t\tobject attribute to be inquired about"),
parser.add_option("-t","--to", type="string",dest="mto",
help="\t\te-mail address the report will go to"),
parser.add_option("-q","--quiet", action="store_false",dest="quiet",
help="\t\toptional - just returns the value"),
parser.add_option("-f", "--file",
action="store", type="string", dest="filename",
help="Pull command strings from FILE", metavar="FILE")
parser.add_option("-d","--display", action="store_false",dest="report",
help="\t\toptional - use sends output to e-mail")
(options, args) = parser.parse_args()

if options.filename is not None:
line = use_file()
(options,args) = parser.parse_args(line.split())
check_params()
else:
check_params()

#
# Key dictionary
#
valid_keys={'AlterationDate':'MQCA_ALTERATION_DATE ',
'AlterationTime':'MQCA_ALTERATION_TIME',
'BackoutRequeueName':'MQCA_BACKOUT_REQ_Q_NAME',
'BackoutThreshold':'MQIA_BACKOUT_THRESHOLD',
'BaseQName':'MQCA_BASE_Q_NAME',
'ClusterDate':'MQCA_CLUSTER_DATE',
'ClusterName':'MQCA_CLUSTER_NAME',
'ClusterNamelist':'MQCA_CLUSTER_NAMELIST',
'ClusterQType':'MQIA_CLUSTER_Q_TYPE',
'ClusterTime':'MQCA_CLUSTER_TIME',
'CreationDate':'MQCA_CREATION_DATE',
'CreationTime':'MQCA_CREATION_TIME',
'CurrentQDepth':'MQIA_CURRENT_Q_DEPTH',
'DefBind':'MQIA_DEF_BIND',
'DefInputOpenOption':'MQIA_DEF_INPUT_OPEN_OPTION',
'DefPersistence':'MQIA_DEF_PERSISTENCE',
'DefPriority':'MQIA_DEF_PRIORITY',
'DefinitionType':'MQIA_DEFINITION_TYPE',
'DistLists':'MQIA_DIST_LISTS',
'HardenGetBackout':'MQIA_HARDEN_GET_BACKOUT',
'HighQDepth':'MQIA_HIGH_Q_DEPTH',
'InhibitGet':'MQIA_INHIBIT_GET',
'InhibitPut':'MQIA_INHIBIT_PUT',
'InitiationQName':'MQCA_INITIATION_Q_NAME',
'MaxMsgLength':'MQIA_MAX_MSG_LENGTH',
'MaxQDepth':'MQIA_MAX_Q_DEPTH',
'MsgDeliverySequence':'MQIA_MSG_DELIVERY_SEQUENCE' ,
'MsgDeqCount':'MQIA_MSG_DEQ_COUNT',
'MsgEnqCount':'MQIA_MSG_ENQ_COUNT',
'OpenInputCount':'MQIA_OPEN_INPUT_COUNT',
'OpenOutputCount':'MQIA_OPEN_OUTPUT_COUNT',
'ProcessName':'MQCA_PROCESS_NAME',
'QDepthHighEvent':'MQIA_Q_DEPTH_HIGH_EVENT',
'QDepthHighLimit':'MQIA_Q_DEPTH_HIGH_LIMIT',
'QDepthLowEvent':'MQIA_Q_DEPTH_LOW_EVENT',
'QDepthLowLimit':'MQIA_Q_DEPTH_LOW_LIMIT',
'QDepthMaxEvent':'MQIA_Q_DEPTH_MAX_EVENT',
'QDesc':'MQCA_Q_DESC',
'QMgrIdentifier':'MQCA_Q_MGR_IDENTIFIER',
'QMgrName':'MQCA_CLUSTER_Q_MGR_NAME',
'QName':'MQCA_Q_NAME',
'QNames':'MQCACF_Q_NAMES',
'QServiceInterval':'MQIA_Q_SERVICE_INTERVAL',
'QServiceIntervalEvent':'MQIA_Q_SERVICE_INTERVAL_E VENT',
'QType':'MQIA_Q_TYPE',
'RemoteQMgrName':'MQCA_REMOTE_Q_MGR_NAME',
'RemoteQName':'MQCA_REMOTE_Q_NAME',
'RetentionInterval':'MQIA_RETENTION_INTERVAL',
'Scope':'MQIA_SCOPE',
'Shareability':'MQIA_SHAREABILITY',
'TimeSinceReset':'MQIA_TIME_SINCE_RESET',
'TriggerControl':'MQIA_TRIGGER_CONTROL',
'TriggerData':'MQCA_TRIGGER_DATA',
'TriggerDepth':'MQIA_TRIGGER_DEPTH',
'TriggerMsgPriority':'MQIA_TRIGGER_MSG_PRIORITY',
'TriggerType':'MQIA_TRIGGER_TYPE',
'Usage':'MQIA_USAGE',
'XmitQName':'MQCA_XMIT_Q_NAME'}

#
# Keys that can be numerically compared against
#
numeric_keys={'AlterationDate':'MQCA_ALTERATION_DA TE',
'AlterationTime':'MQCA_ALTERATION_TIME',
'BackoutThreshold':'MQIA_BACKOUT_THRESHOLD',
'DefPriority':'MQIA_DEF_PRIORITY',
'ClusterDate':'MQCA_CLUSTER_DATE',
'ClusterTime':'MQCA_CLUSTER_TIME',
'CreationDate':'MQCA_CREATION_DATE',
'CreationTime':'MQCA_CREATION_TIME',
'CurrentQDepth':'MQIA_CURRENT_Q_DEPTH',
'HighQDepth':'MQIA_HIGH_Q_DEPTH',
'MaxMsgLength':'MQIA_MAX_MSG_LENGTH',
'MaxQDepth':'MQIA_MAX_Q_DEPTH',
'MsgDeliverySequence':'MQIA_MSG_DELIVERY_SEQUENCE' ,
'MsgDeqCount':'MQIA_MSG_DEQ_COUNT',
'MsgEnqCount':'MQIA_MSG_ENQ_COUNT',
'OpenInputCount':'MQIA_OPEN_INPUT_COUNT',
'OpenOutputCount':'MQIA_OPEN_OUTPUT_COUNT',
'QDepthHighEvent':'MQIA_Q_DEPTH_HIGH_EVENT',
'QDepthHighLimit':'MQIA_Q_DEPTH_HIGH_LIMIT',
'QMgrIdentifier':'MQCA_Q_MGR_IDENTIFIER',
'QServiceInterval':'MQIA_Q_SERVICE_INTERVAL',
'TriggerDepth':'MQIA_TRIGGER_DEPTH',
'TriggerMsgPriority':'MQIA_TRIGGER_MSG_PRIORITY'}
host_port = "%s(%s)" % (options.host.lower(), options.port)
vt = options.key.split(',')

for i in vt:
test_valid_keys(vt[count])
inq = lookup( options.qmanager.upper(), host_port, options.object)
if options.quiet is False:
text=text+str(inq)
else:
text = text+"\n%s for %s object %s is %s" % ( vt[count],
options.qmanager.upper(), options.object,inq)

if options.check is not None:
ct = options.check.split(",")
test_numeric(vt[count])
value_test=testval(inq,ct[count])
text=text+" "+value_test
count=count+1
if options.report is None:
print text
sys.exit(0)
else:
subject = "Report for queue manager %s on server %s " % (
options.qmanager.upper() , options.host.lower() )
mail( mserver, mfrom, options.mto, subject, text )
sys.exit(0)


Apr 13 '06 #3
ma**@vazor.com wrote:
Suggest keeping it simple:

def use_file():
return open(options.filename).readlines()

m


Very cool..

Didn't know you could do that.

Apr 13 '06 #4
News wrote:
Hi Everyone,
The attached code creates client connections to websphere queue managers
and then processes an inquiry against them.

The program functions when it gets options from the command line.

It also works when pulling the options from a file.

My issue is that it only processes the first line of the file.

Does anyone know why this might be the case?

The use_file() function should be returning each file line.

Any help you can provide on this would be greatly appreciated.

Thanks

(snip)
#
# Read a file into a list
Hint : use docstrings for this kind of comments.
def use_file():
myfile = open(options.filename)
1/ options.filename should be passed as an argument. Globals are
definitively evil and should be avoided by all means.

2/ open() may raise an error. You may (or not) want to handle this error
here.
while True:
line=myfile.readline()
print line
Why this print statement ? debug ?
if len(line) == 0:
myfile.close()
break
else:
return line
This of course will read only the first line. The return statements
terminates execution of the function. And BTW:
myfile.close()
.... This will never execute.

(snip)
if options.filename is not None:
line = use_file()
(options,args) = parser.parse_args(line.split())
Using it that way wouldn't work anyway... and may have unexpected
results, since you're overwriting the existing options object.
check_params()
else:
check_params()

Since you call check_params() anyway, it would simpler to write it:

if options.filename is not None:
do_someting_with_options_here()
check_params()
Now for your question:

if options.filename is not None:
try:
f = open(options.filename)
except IOError, e:
# stdout is for normal program outputs only,
# error messages should go to stderr
print >> sys.stderr, \
"failed to open options file %s : %s" \
% (options.filename, e)
sys.exit(1)
for line in f:
# pass the existing options object in,
# so it's updated with the new args instead of
# being overwritten
options, args = parser.parse_args(line.split(), options=options)
f.close()
del f
check_params()

BTW, check_params() should also takes the options object as argument.
Globals are evil. Definitively. Also, and if I may suggest,
check_options() would be a better name IMVHO.

And while we're at it, you're once again in a bad case of reinventing
the SquareWheel(tm) with the usage() function - optparse takes care of this:
http://docs.python.org/lib/optparse-...ting-help.html
(snip)
if options.quiet is False:
You'd better not identity test against True or False. There are lot of
expressions that evaluate to True or False in a boolen context, without
actually *being* neither the True object nor the False object.

-> if not options.quiet:
(snip)

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
1
by: | last post by:
Can anyone help me on how to enumerate all the domains in a forest with VB.NET. Thanks, Dan Rhoads
4
by: Gregory Piñero | last post by:
Hi, Would anyone be able to tell me why my function below is getting stuck in infinite recusion? Maybe I'm just tired and missing something obvious? def...
1
by: bob | last post by:
I cannot seem to display any info with my Data Grid ... the online examples I have seen 'appear' to say the same thing. create a DataTable then simply call something like this myDataGrid.Source =...
8
by: sudharsan | last post by:
please gimme the logic to merge two binary search trees?I mean which node has to be the root node of the new binary tree?? Thanks in advance
13
by: sonald | last post by:
Hi, Can anybody tell me how to change the text delimiter in FastCSV Parser ? By default the text delimiter is double quotes(") I want to change it to anything else... say a pipe (|).. can anyone...
1
by: manojdagar | last post by:
here now stands that small knot of villages known as the Endians, a mighty forest once stood. Indeed, legand has it that you could have stoodon the edge of the wood and seen it stretch out for miles,...
4
by: charonzen | last post by:
I have a list of strings. These strings are previously selected bigrams with underscores between them ('and_the', 'nothing_given', and so on). I need to write a regex that will read another text...
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.