Python 3 compatibility: use print as a function.

In Python 3 print is a function.
Especially for multiple string print, need to import
print_function from __future__.

Story: 2003310
Task: 24265
Change-Id: I1e8f80527f9ba97093a8e753f3460110e160d1b9
Signed-off-by: chenyan <yan.chen@intel.com>
This commit is contained in:
chenyan 2018-08-03 16:48:02 +08:00
parent 7be92d78fc
commit 4ac52a716e
4 changed files with 36 additions and 33 deletions

View File

@ -28,7 +28,7 @@ def print_alarm(alarm):
+ "entity_type_id: " + alarm.entity_type_id + ", timestamp: "+ alarm.timestamp + "\n"
alarm_str += "entity_instance_id: " + alarm.entity_instance_id + ", "
alarm_str += "probable cause:" + alarm.probable_cause + "\n"
print alarm_str
print(alarm_str)
def create():
@ -45,19 +45,19 @@ def create():
service_affecting = False,
suppression = False)
uuid =ser.set_fault(fault)
print uuid
print(uuid)
def delete(alarm_id, instance_id):
ser=FaultAPIs()
ret = ser.clear_fault(alarm_id,instance_id)
print "Delete fault return %s" % str(ret)
print("Delete fault return %s" % str(ret))
def del_all(instance_id):
ser=FaultAPIs()
ret= ser.clear_all(instance_id)
print "Delete faults return: %s" % str(ret)
print("Delete faults return: %s" % str(ret))
def get(alarm_id, instance_id):
@ -66,29 +66,29 @@ def get(alarm_id, instance_id):
if a is not None:
print_alarm(a)
else:
print "Alarm not found"
print("Alarm not found")
def get_all(instance_id):
ser=FaultAPIs()
ll= ser.get_faults(instance_id)
if ll is not None:
print "Total alarm returned: %d\n" % len(ll)
print("Total alarm returned: %d\n" % len(ll))
for i in ll:
print_alarm(i)
else:
print "No alarm returned"
print("No alarm returned")
def get_list(alarm_id):
ser=FaultAPIs()
ll= ser.get_faults_by_id(alarm_id)
if ll is not None:
print "Total alarm returned: %d\n" % len(ll)
print("Total alarm returned: %d\n" % len(ll))
for i in ll:
print_alarm(i)
else:
print "No alarm returned"
print("No alarm returned")
if __name__ == "__main__":
if sys.argv[1] == "create":

View File

@ -68,7 +68,7 @@ def logInfo(msg):
with open(FM_LOG_EVENT_LOG_FILE,"a") as logFile:
logFile.write(output)
except Exception as e:
print e
print(e)
def get_events_yaml_filename():

View File

@ -80,11 +80,11 @@ def get_fm_alarms():
#
if len(sys.argv) == 1:
print "Missing file argument.\n"
print("Missing file argument.\n")
exit(1)
if not os.path.isfile(sys.argv[1]):
print "File \'%s\' does not exist.\n" % (sys.argv[1])
print("File \'%s\' does not exist.\n" % (sys.argv[1]))
exit(1)
exitValue = 0
@ -100,13 +100,13 @@ with open(sys.argv[1], 'r') as stream:
constants_alarms = get_constants_alarms()
for alarm_id in constants_alarms:
if alarm_id not in events_alarm_list:
print "\n ERROR: constants.py alarm \'%s\' must be defined in file events.yaml.\n" % (alarm_id)
print("\n ERROR: constants.py alarm \'%s\' must be defined in file events.yaml.\n" % (alarm_id))
exitValue = 1
fm_alarms = get_fm_alarms()
for alarm_id in fm_alarms:
if alarm_id not in events_alarm_list:
print "\n ERROR: fmAlarm.h alarm \'%s\' must be defined in file events.yaml.\n" % (alarm_id)
print("\n ERROR: fmAlarm.h alarm \'%s\' must be defined in file events.yaml.\n" % (alarm_id))
exitValue = 1
exit (exitValue)

View File

@ -10,6 +10,9 @@ import os
import yaml
import constants
# Python3 compatibility
from __future__ import print_function
# Record Format (for full description see events.yaml)
#
# 100.001:
@ -99,9 +102,9 @@ serviceAffecting_FieldName : serviceAffecting_FieldValues
def checkField( fieldKey, fieldValues, key, event ):
if not event.has_key(fieldKey):
print "\n ERROR: %s missing \'%s\' field." % (key, fieldKey)
print("\n ERROR: %s missing \'%s\' field." % (key, fieldKey))
return False
# print "START: %s :END" % event[fieldKey]
# print ("START: %s :END" % event[fieldKey])
if type(event[fieldKey]) is str:
if not fieldValues:
@ -109,8 +112,8 @@ def checkField( fieldKey, fieldValues, key, event ):
if event[fieldKey] in fieldValues:
return True
else:
print "\n ERROR: \'%s\' is not a valid \'%s\' field value." % (event[fieldKey], fieldKey)
print " Valid values are:", fieldValues
print("\n ERROR: \'%s\' is not a valid \'%s\' field value." % (event[fieldKey], fieldKey))
print(" Valid values are:", fieldValues)
return False
if type(event[fieldKey]) is list:
@ -118,31 +121,31 @@ def checkField( fieldKey, fieldValues, key, event ):
return True
for listvalue in event[fieldKey]:
if not listvalue in fieldValues:
print "\n ERROR: \'%s\' is not a valid \'%s\' field value." % (listvalue, fieldKey)
print " Valid values are:", fieldValues
print("\n ERROR: \'%s\' is not a valid \'%s\' field value." % (listvalue, fieldKey))
print(" Valid values are:", fieldValues)
return False
if type(event[fieldKey]) is dict:
for dictKey, dictValue in event[fieldKey].iteritems():
if not dictKey in severity_FieldValues:
print "\n ERROR: \'%s\' is not a valid \'%s\' index value." % (dictKey, fieldKey)
print " Valid index values are:", severity_FieldValues
print("\n ERROR: \'%s\' is not a valid \'%s\' index value." % (dictKey, fieldKey))
print(" Valid index values are:", severity_FieldValues)
return False
if fieldValues:
if not dictValue in fieldValues:
print "\n ERROR: \'%s\' is not a valid \'%s\' field value." % (dictValue, fieldKey)
print " Valid values are:", fieldValues
print("\n ERROR: \'%s\' is not a valid \'%s\' field value." % (dictValue, fieldKey))
print(" Valid values are:", fieldValues)
return False
return True
def checkTypeField( key, event ):
if not event.has_key(type_FieldName):
print "\n ERROR: %s missing \'%s\' field." % (key, type_FieldName)
print("\n ERROR: %s missing \'%s\' field." % (key, type_FieldName))
return False
if event[type_FieldName] in type_FieldValues:
return True
print "\n ERROR: \'%s\' is not a valid \'%s\' field value." % (event[type_FieldName], type_FieldName)
print("\n ERROR: \'%s\' is not a valid \'%s\' field value." % (event[type_FieldName], type_FieldName))
return False
@ -160,7 +163,7 @@ def checkFields( key, event ):
for itemKey, itemValue in event.iteritems():
if not eventFields.has_key(itemKey):
print "\n ERROR: \'%s\' is not a valid \'%s\' field." % (itemKey, ("Alarm" if isAlarm else "Log") )
print("\n ERROR: \'%s\' is not a valid \'%s\' field." % (itemKey, ("Alarm" if isAlarm else "Log") ))
isOk = False
return isOk
@ -171,11 +174,11 @@ def checkFields( key, event ):
#
if len(sys.argv) == 1:
print "Missing file argument.\n"
print("Missing file argument.\n")
exit(1)
if not os.path.isfile(sys.argv[1]):
print "File \'%s\' does not exist.\n" % (sys.argv[1])
print("File \'%s\' does not exist.\n" % (sys.argv[1]))
exit(1)
with open(sys.argv[1], 'r') as stream:
@ -184,14 +187,14 @@ with open(sys.argv[1], 'r') as stream:
exitValue = 0
for key in events:
print "%6.3f: checking ... " % key,
print("%6.3f: checking ... " % key)
if not checkFields( key, events[key] ):
print
print()
exitValue = 1
else:
print 'OK.'
print('OK.')
print 'Done.'
print('Done.')
except yaml.YAMLError as exc:
print(exc)