Commit f07de06f authored by Miguel Tremblay's avatar Miguel Tremblay
Browse files

Use metro_error class in exception handling instead of string.

parent 3c2e6974
......@@ -40,6 +40,7 @@ import sys
import metro_config
import metro_logger
import metro_error
from toolbox import metro_util
from toolbox import metro_date
......@@ -70,10 +71,10 @@ def validate_handler( sHandler, iFrom, sDescription ):
sFunction_name = lFunctionPart[-1]
try:
metro_util.test_function_existence(sFunction_module, sFunction_name)
except "MetroImportError", sError:
except metro_error.Metro_import_error as inst:
sMessage = config_error_string(sDescription,
iFrom,
sError)
inst)
metro_logger.print_init_message(metro_logger.LOGGER_INIT_ERROR,
sMessage)
sys.exit(3)
......@@ -198,7 +199,7 @@ def validate_execution_sequence( dConf ):
for sModule in lExecutionSequence:
try:
metro_util.test_import(sModule)
except "MetroImportError", sError:
except metro_error.Metro_import_error(sError):
sConfig_path = "%s module:'%s'" % (sKey,sModule)
sMessage = config_error_string(sConfig_path,
iFrom,
......@@ -214,7 +215,7 @@ def validate_roadcast_start_time( dConf ):
if sStart_time != "":
try:
metro_date.parse_date_string(sStart_time)
except "DateError", sError:
except metro_error.Metro_date_error as inst:
sMessage = _("Fatal error, the date string '%s' passed to the\n ")\
% (sStart_time)+\
_("option '--roadcast-start-date' doesn't conform to ") +\
......@@ -223,8 +224,6 @@ def validate_roadcast_start_time( dConf ):
sMessage)
sys.exit(3)
else:
# sMessage = _("METRo need a valid roadcast start date\n" +
# "Please use the option: '--roadcast-start-date'")
sMessage = _("No roadcast start date provided. The date of the\n") + \
_("last observation will be used as the roadcast ") + \
_("start date\n")
......
......@@ -58,6 +58,7 @@ import string
import metro_util
import metro_constant
import metro_error
_ = metro_util.init_translation('metro_date')
......@@ -73,7 +74,8 @@ def parse_date_string( sDate ):
if sDate != None:
try:
# Strip the date in ISO up to the minute only, ie. the first 16 characters
# Strip the date in ISO up to the minute only,
# ie. the first 16 characters
sDate = sDate[0:16]
tDate = time.strptime(sDate, "%Y-%m-%dT%H:%M")
fDate = time.mktime(tDate)
......@@ -81,11 +83,11 @@ def parse_date_string( sDate ):
sMessage = _("The following error occured when parsing the ") +\
_("ISO 8601 date:\n %s") % (sError)
fDate = metro_constant.NaN
raise "METRoDateError", sMessage
raise metro_error.Metro_date_error(sMessage)
else:
sMessage = _("The following error occured when parsing a ") +\
_("date:\nNo date string to convert")
raise "METRoDateError", sMessage
raise metro_error.Metro_date_error(sMessage)
return fDate
......@@ -242,7 +244,7 @@ def get_elapsed_time(ftime1, ftime2, sTimeZone="UTC", sUnit="hours"):
else:
sInvalidParameterError =_("Invalid criteria in get_elapsed_time: sUnit = %s")\
% (sUnit)
raise "METRoDateError", sInvalidParameterError
raise metro_error.Metro_date_error(sInvalidParameterError)
def tranform_decimal_hour_in_minutes(fTimeHour):
......
......@@ -38,6 +38,7 @@ import os
import metro_config
import metro_logger
import metro_error
from toolbox import metro_date
from toolbox import metro_util
from toolbox import metro_xml
......@@ -59,9 +60,9 @@ def read_date(sTag,nodeBranch):
sDate = metro_xml.extract_data_from_node(sTag,nodeBranch)
try:
fDate = metro_date.parse_date_string(sDate)
except "DateError", sError:
except metro_error.Metro_date_error as inst:
metro_logger.print_message(metro_logger.LOGGER_MSG_WARNING,
sError)
str(inst))
fDate = 0.0
return fDate
......
......@@ -82,3 +82,12 @@ class Metro_version_error(Metro_error):
""" Put the message in string only if it is a string"""
self.sError = "\n" + _("METRo's version error: ") + self.sError
return self.sError
class Metro_date_error(Metro_error):
"""
Something went wrong with a date
"""
def __str__(self):
""" Put the message in string only if it is a string"""
self.sError = "\n" + _("Date error: ") + self.sError
return self.sError
......@@ -40,9 +40,14 @@
as a library and metro_util is the module that every other module loads.
Thus, the language setting that are used through metro is always set if
this code is in the metro_util module.
metro_logger cannot be invoked here since this module is imported in
metro_logger. Errors must raised here must be catched elsewhere and then
be logged through this module, if possible.
"""
import os
import metro_error
# set environment variable LANGUAGE and LC_ALL
if 'LANGUAGE' not in os.environ and 'LC_ALL' not in os.environ:
......@@ -125,13 +130,8 @@ def test_import( sModule_name ):
try:
sCode = "import " + sModule_name
exec sCode
except SyntaxError, sError:
raise "MetroImportError", sError
except ImportError, sError:
raise "MetroImportError", sError
except EOFError, sError:
print 'sCode= [%s]' % (sCode)
raise "MetroImportError", sError
except (SyntaxError, ImportError, EOFError) as sError:
raise metro_error.Metro_import_error(sError)
def test_function_existence( sModule_name, sFunction_name ):
"""
......@@ -142,13 +142,9 @@ def test_function_existence( sModule_name, sFunction_name ):
exec sCode_import
sCode_function = "func_object = " + sModule_name + "." + sFunction_name
exec sCode_function
except SyntaxError, sError:
raise "MetroImportError", sError
except ImportError, sError:
raise "MetroImportError", sError
except AttributeError, sError:
raise "MetroImportError", sError
except (SyntaxError, ImportError, AttributeError) as inst:
raise metro_error.Metro_import_error(inst)
def join_dictionaries(dDict1, dDict2):
lKeys = dDict1.keys() + dDict2.keys()
......@@ -216,27 +212,28 @@ def interpolate(xArray, yArray):
# Check if the size of the array is ok.
iLenXArray = len(xArray)
iLenYArray = len(yArray)
if( iLenXArray != iLenYArray):
if ( iLenXArray != iLenYArray):
sMetroUtilWarning = _("In interpolate, the arrays does not") +\
_("have the same length. %d != %d\n") %\
(iLenXArray, iLenYArray )
# Error should be thrown ? Text won<t be in metro_logger...
print sMetroUtilWarning
if iLenYArray < iLenXArray:
if iLenYArray < iLenXArray:
sMetroUtilWarning = _("Padding Y array with average at the end.")
npPadd = numpy.zeros(iLenXArray - iLenYArray)+yArray.mean()
yArray = numpy.concatenate((yArray,npPadd))
else:
raise "METRoUtilError", sMetroUtilWarning
raise metro_error.Metro_util_error(sMetroUtilWarning)
elif(iLenXArray < 2):
elif (iLenXArray < 2):
sMetroUtilError = _("In interpolate, the arrays have only one value (1)")
raise "METRoUtilError", sMetroUtilError
elif(xArray[1]-xArray[0] < iIncrement):
raise metro_error.Metro_util_error(sMetroUtilError)
elif (xArray[1]-xArray[0] < iIncrement):
sMetroUtilError = _("In interpolate, iIncrement is too big. \n")+\
_("Space between two values in xArray:")+\
"xArray[0]: %f xArray[1]:%f \n Increment=%f" \
% (xArray[0], xArray[1], iIncrement)
raise "METRoUtilError", sMetroUtilError
raise metro_error.Metro_util_error(sMetroUtilError)
# Build the new x
xArrayInt = numpy.arange(xArray[0],xArray[iLenXArray-1],iIncrement)
......@@ -271,11 +268,11 @@ def shift_left(npInput, fValueAdded=0):
"""
# Check the dimension
if(npInput.shape <= 0):
if (npInput.shape <= 0):
sMetroUtilError = _("In shift_left, npInput is not of size (1,).\n")+\
"len(npInput.getshape())=%s"\
% (len(npInput.getshape()))
raise "METRoUtilError", sMetroUtilError
"len(npInput.shape)=%s"\
% (len(npInput.shape))
raise metro_error.Metro_util_error(sMetroUtilError)
# Cut the first value
npOutput = numpy.take(npInput,\
......@@ -314,11 +311,11 @@ def shift_right(npInput, fValueAdded=0):
"""
# Check the dimension
if(npInput.shape[0] <= 0):
if (npInput.shape[0] <= 0):
sMetroUtilError = _("In shift_right, npInput is not of size (1,).\n")+\
"len(npInput.getshape())=%s"\
% (len(npInput.getshape()))
raise "METRoUtilError", sMetroUtilError
"len(npInput.shape)=%s"\
% (len(npInput.shape))
raise metro_error.Metro_util_error(sMetroUtilError)
npToBeCat = numpy.array([fValueAdded])
# Cut the trailing value
npOutput = numpy.take(npInput,\
......@@ -359,7 +356,8 @@ def get_indice_of(npInput, fValue=0):
return i
sMetroUtilError = _("No indice with this value: %d") %(fValue)
raise "METRoUtilError", sMetroUtilError
raise metro_error.Metro_util_error(sMetroUtilError)
def get_difference_array(npInput, bPrevious=False):
......@@ -404,8 +402,8 @@ def sign(dResult, dSign):
"""
Name: sign
Parameters: [I] double dResult :
[I] double dSign :
Parameters: [I double dResult :
[I double dSign :
Returns: - abs(dResult) if dSign < 0
abs(dResult) if dSign > 0
......@@ -417,17 +415,17 @@ def sign(dResult, dSign):
Notes: This is the equivalent of SIGN in fortran. And no,
there is no built-in function that does that.
CASE 1: If y >= 0 then
sign(x,y) = abs(x) ,
CASE 2: If y < 0 then
sign(x,y) = - abs(x) .
Revision History:
Author Date Reason
Miguel Tremblay August 24th 2004
"""
if dSign >= 0:
# If dSign is == 0, raise an error
if dSign == 0:
sMetroUtilError = _("Cannot determine the sign of zero")
raise sMetroUtilError
elif dSign > 0:
return abs(dResult)
else:
return -abs(dResult)
......@@ -458,7 +456,7 @@ def subsample(npInput, nSubsamplingIndice):
sMetroUtilError = _("In metro_util.subsample, subsampling rate")+\
_("is higher than array size: %d > %d") %\
(nSubsamplingIndice, len(npInput ))
raise "METRoUtilError", sMetroUtilError
raise metro_error.Metro_util_error(sMetroUtilError)
# Perform the subsampling
npSub = numpy.arange(0, len(npInput), nSubsamplingIndice)
......@@ -508,7 +506,7 @@ def concat_array(npArray1, npArray2):
sMetroUtilError = _("In metro_util.concat_array, array must be") +\
_(" of the same dimension: %d != %d") % \
((nLen1, nLen2))
raise "METRoUtilError", sMetroUtilError
raise metro_error.Metro_util_error(sMetroUtilError)
# First, rotate the axis
npArray1.setshape(nLen1,1)
......@@ -522,27 +520,25 @@ def concat_array(npArray1, npArray2):
def cut_indices(npArray, x0, xn):
"""
Name: cut_indices
Name: cut_indices
Parameters: numpy npArray : array to be cut
float x0 : The minimum from with the left will
be cut.
float xn: The maximum from with the right will
be cut.
Parameters: numpy npArray : array to be cut
float x0 : The minimum from with the left will be cut.
float xn: The maximum from with the right will be cut.
Returns: [indiceLeft, indiceRight]
Returns: [indiceLeft, indiceRight]
Functions Called:
Functions Called:
Description: Reduce an array to the value between x0 and xn
Description: Reduce an array to the value between x0 and xn
Notes: Array should be monotone
Notes: Array should be monotone
Revision History:
Author Date Reason
Miguel Tremblay September 20th 2004
"""
Revision History:
Author Date Reason
Miguel Tremblay September 20th 2004
"""
nFirstValidIndice = get_indice_of(npArray, x0)
nLastValidIndice = get_indice_of(npArray, xn)
......@@ -600,20 +596,20 @@ def validate_version_number(sVersion, sMin_version, sMax_version ):
% (version, min_version) +\
_("to '%s' inclusively are supported") \
% (max_version)
raise "VersionErrorLow", sMessage
elif version > max_version:
raise metro_error.Metro_version_error(sMessage)
elif version > max_version:
sMessage = _("Version number:'%s' is not yet supported. Version ")\
% (version) +\
_("from '%s' to '%s' inclusively are supported") \
% (min_version,max_version)
raise "VersionErrorHigh", sMessage
raise metro_error.Metro_version_error(sMessage)
else:
sMessage = _("Can't find version number. Version from '%s' ") \
% (min_version) +\
_("to '%s' inclusively are supported") \
% (max_version)
raise "VersionErrorUndetermined", sMessage
raise metro_error.Metro_version_error(sMessage)
def init_translation(sFilename):
......
......@@ -47,6 +47,7 @@ import string
import metro_config
import metro_logger
import metro_error
from toolbox import metro_util
_ = metro_util.init_translation('metro_xml')
......@@ -56,7 +57,7 @@ def init( sMetro_xml_lib = ""):
if sMetro_xml_lib != "":
try:
metro_xml_lib = __import__(sMetro_xml_lib)
except "MetroImportError":
except metro_error.Metro_import_error:
sMessage = _("Fatal error! Can't import '%s' xml library") \
% (sMetro_xml_lib)
metro_logger.print_init_message(metro_logger.LOGGER_INIT_ERROR,
......@@ -68,11 +69,11 @@ def init( sMetro_xml_lib = ""):
metro_xml_lib = __import__(sMetro_xml_lib)
else:
# metro_logger.print_init_message(metro_logger.LOGGER_INIT_MESSAGE,
# _("Auto configure METRo XML library"))
metro_logger.print_init_message(metro_logger.LOGGER_INIT_MESSAGE,
_("Auto configure METRo XML library"))
try:
metro_util.test_import("metro_xml_libxml2")
except "MetroImportError":
except metro_error.Metro_import_error as inst:
sMessage = _("Fatal error! No METRo XML library can be use. ") +\
_("\nMETRo need one of the following XML library ") +\
_("installed on the system.\nSupported library:") +\
......@@ -311,7 +312,6 @@ def create_node_tree_from_dict( domDoc, nodeParent, lDefs, dData ):
# create the node
sCode = "nodeData = " + sWriteHandler + \
"(sXml_tag,dData[sTag])"
print sCode
exec sCode
append_child(nodeParent, nodeData)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment