Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
CellMLToNektar.pycml.MathsError Class Reference
Inheritance diagram for CellMLToNektar.pycml.MathsError:
Inheritance graph
[legend]
Collaboration diagram for CellMLToNektar.pycml.MathsError:
Collaboration graph
[legend]

Public Member Functions

def __init__
 
def show_xml_context_only
 
def __str__
 
def ordinal
 
def __unicode__
 

Public Attributes

 message
 
 warn
 
 level
 
 show_xml_context
 
 cname
 
 ename
 
 context
 
 expr_index
 
 math_index
 
 reaction_spec
 

Private Member Functions

def _generate_message
 

Private Attributes

 _show_xml_context_only
 

Detailed Description

Exception class for validation errors raised while checking mathematics.

Definition at line 3343 of file pycml.py.

Constructor & Destructor Documentation

def CellMLToNektar.pycml.MathsError.__init__ (   self,
  context_obj,
  message,
  warn = False,
  level = None 
)
Create a mathematics validation error.

context_class should be the object that is reporting the error.
message gives more details on what went wrong.
If warn is set to true then produce a warning message, not an error.
level gives the level of the message logged.

Definition at line 3347 of file pycml.py.

3348  def __init__(self, context_obj, message, warn=False, level=None):
3349  """Create a mathematics validation error.
3350 
3351  context_class should be the object that is reporting the error.
3352  message gives more details on what went wrong.
3353  If warn is set to true then produce a warning message, not an error.
3354  level gives the level of the message logged.
3355  """
3356  self.message = message
3357  self.warn = warn
3358  self.level = level or (logging.ERROR,logging.WARNING)[warn]
3359  self.show_xml_context = False
3360  self._show_xml_context_only = False
3362  self.cname = context_obj.component.name
3363  self.ename = context_obj.localName
3364  self.context = context_obj
3365 
3366  # Nicer context explanation
3367  if isinstance(context_obj, cellml_variable):
3368  self.expr_index = self.math_index = 0
3369  self.reaction_spec = ''
3370  return
3371  expr_root = context_obj.xml_xpath(u'ancestor-or-self::*[local-name(..)="math"]')[0]
3372  self.expr_index = self.math_index = 1
3373  math = expr_root.xml_parent
3374  for elt in math.xml_element_children():
3375  if elt is expr_root: break
3376  if elt.localName in [u'apply', u'semantics']:
3377  self.expr_index += 1
3378  for elt in math.xml_element_children(math.xml_parent):
3379  if elt is math: break
3380  if elt.localName == u'math':
3381  self.math_index += 1
3382  # Is this in a reaction?
3383  vref = context_obj.xml_xpath(u'ancestor::cml:variable_ref')
3384  if vref:
3385  self.reaction_spec = ' in mathematics for variable "%s" in a reaction' % vref[0].variable
3386  else:
3387  self.reaction_spec = ''
3388  return

Member Function Documentation

def CellMLToNektar.pycml.MathsError.__str__ (   self)

Definition at line 3394 of file pycml.py.

3395  def __str__(self):
3396  msg = unicode(self)
3397  return msg.encode('UTF-8')
def CellMLToNektar.pycml.MathsError.__unicode__ (   self)

Definition at line 3425 of file pycml.py.

References CellMLToNektar.pycml.MathsError._generate_message().

3426  def __unicode__(self):
3427  return self._generate_message('checking mathematics')
def CellMLToNektar.pycml.MathsError._generate_message (   self,
  where 
)
private

Definition at line 3407 of file pycml.py.

References CellMLToNektar.pycml.MathsError._show_xml_context_only, CellMLToNektar.pycml.MathsError.cname, CellMLToNektar.pycml.MathsError.context, CellMLToNektar.utilities.element_xpath(), CellMLToNektar.pycml.MathsError.expr_index, CellMLToNektar.pycml.MathsError.math_index, CellMLToNektar.pycml.MathsError.message, CellMLToNektar.pycml.MathsError.ordinal(), CellMLToNektar.pycml.MathsError.reaction_spec, CellMLToNektar.pycml.MathsError.show_xml_context, and CellMLToNektar.pycml.MathsError.warn.

Referenced by CellMLToNektar.pycml.MathsError.__unicode__(), and CellMLToNektar.pycml.UnitsError.__unicode__().

3408  def _generate_message(self, where):
3409  if self.warn: type = 'Warning'
3410  else: type = 'Error'
3411  msg = [type, ' ', where, ': ', self.message]
3412  if not self._show_xml_context_only:
3413  msg.extend(['\n Context: ', self.ordinal(self.expr_index),
3414  ' expression in the ', self.ordinal(self.math_index),
3415  ' math element', self.reaction_spec,
3416  ' in component ', self.cname, '\n XPath: ',
3417  element_xpath(self.context)])
3418  msg = u''.join(msg)
3419  if self.show_xml_context:
3420  # Return the context XML tree as well.
3421  xml = self.context.xml(indent = u'yes',
3422  omitXmlDeclaration = u'yes')
3423  msg = msg + u'\n' + unicode(xml, encoding='UTF-8')
3424  return msg
def CellMLToNektar.pycml.MathsError.ordinal (   self,
  i 
)

Definition at line 3398 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

3399  def ordinal(self, i):
3400  "Convert integer i to an ordinal string."
3401  if i // 10 == 1: suf = 'th'
3402  elif i % 10 == 1: suf = 'st'
3403  elif i % 10 == 2: suf = 'nd'
3404  elif i % 10 == 3: suf = 'rd'
3405  else: suf = 'th'
3406  return "%d%s" % (i, suf)
def CellMLToNektar.pycml.MathsError.show_xml_context_only (   self)
Only show the XML where the error occurred.

Definition at line 3389 of file pycml.py.

References CellMLToNektar.pycml.MathsError._show_xml_context_only, and CellMLToNektar.pycml.MathsError.show_xml_context.

3390  def show_xml_context_only(self):
3391  """Only show the XML where the error occurred."""
3392  self.show_xml_context = True
3393  self._show_xml_context_only = True

Member Data Documentation

CellMLToNektar.pycml.MathsError._show_xml_context_only
private

Definition at line 3359 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message(), and CellMLToNektar.pycml.MathsError.show_xml_context_only().

CellMLToNektar.pycml.MathsError.cname

Definition at line 3361 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

CellMLToNektar.pycml.MathsError.context

Definition at line 3363 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

CellMLToNektar.pycml.MathsError.ename

Definition at line 3362 of file pycml.py.

CellMLToNektar.pycml.MathsError.expr_index

Definition at line 3367 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

CellMLToNektar.pycml.MathsError.level

Definition at line 3357 of file pycml.py.

CellMLToNektar.pycml.MathsError.math_index

Definition at line 3367 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

CellMLToNektar.pycml.MathsError.message

Definition at line 3355 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

CellMLToNektar.pycml.MathsError.reaction_spec

Definition at line 3368 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

CellMLToNektar.pycml.MathsError.show_xml_context

Definition at line 3358 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message(), and CellMLToNektar.pycml.MathsError.show_xml_context_only().

CellMLToNektar.pycml.MathsError.warn

Definition at line 3356 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().