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

N

News

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', '(e-mail address removed)',
'(e-mail address removed)', '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.SVRCONN",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 = '(e-mail address removed)'
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_EVENT',
'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_DATE',
'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)
 
H

Heiko Wundram

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.
 
M

matt

Suggest keeping it simple:

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

m
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', '(e-mail address removed)',
'(e-mail address removed)', '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.SVRCONN",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 = '(e-mail address removed)'
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_EVENT',
'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_DATE',
'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)
 
B

bruno at modulix

News said:
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-generating-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
 
D

Dennis Lee Bieber

#
# 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()

Redundant -- the break takes you out of the loop, and the next
statement outside the loop is a "close"
break
else:
return line

Okay, you read the first line, and exit the function with just the
one line... Since "myfile" is a function local variable, it is freed,
and the part of freeing it will close the file.

No doubt others have shown you such "secrets" as file.readlines()
(which reads all the lines in the file)...

As a learning experience, however, I'll attempt to show how
"use_file" would be coded to work in the style you attempted -- however
inefficient that style ends up being.

def use_file(fid): #pass in the name, don't rely on global variable
aFile = open(fid)
lines = [] #empty initial data
while True:
ln = aFile.readline()
print ln
if not ln: #empty line is "false"
break #exit loop
lines.append(ln)
aFile.close()
return lines
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top