Browse Source

Improve Rx exceptions

pull/190/head
Oliverpool 9 years ago
parent
commit
10aece6ada
  1. 5
      patacrep/build.py
  2. 10
      patacrep/errors.py
  3. 9
      patacrep/templates.py
  4. 5
      patacrep/utils.py

5
patacrep/build.py

@ -44,7 +44,12 @@ class Songbook:
def __init__(self, raw_songbook, basename): def __init__(self, raw_songbook, basename):
# Validate config # Validate config
schema = config_model('schema') schema = config_model('schema')
try:
utils.validate_yaml_schema(raw_songbook, schema) utils.validate_yaml_schema(raw_songbook, schema)
except errors.SchemaError as exception:
exception.message = "The songbook file '{}' is not valid".format(basename)
raise exception
self._raw_config = raw_songbook self._raw_config = raw_songbook
self.basename = basename self.basename = basename

10
patacrep/errors.py

@ -7,14 +7,18 @@ class SongbookError(Exception):
""" """
pass pass
class SBFileError(SongbookError): class SchemaError(SongbookError):
"""Error during songbook file decoding""" """Error on the songbook schema"""
def __init__(self, message=None): def __init__(self, message='', rx_exception=None):
super().__init__() super().__init__()
self.message = message self.message = message
self.rx_exception = rx_exception
def __str__(self): def __str__(self):
if self.rx_exception:
return self.message + "\n" + str(self.rx_exception)
else:
return self.message return self.message
class TemplateError(SongbookError): class TemplateError(SongbookError):

9
patacrep/templates.py

@ -163,19 +163,26 @@ class TexBookRenderer(Renderer):
def get_all_variables(self, user_config): def get_all_variables(self, user_config):
'''Validate template variables (and set defaults when needed) '''Validate template variables (and set defaults when needed)
Will raise `SchemaError` if any data does not respect the schema
''' '''
data = self.get_template_variables(self.template) data = self.get_template_variables(self.template)
variables = dict() variables = dict()
for templatename, param in data.items(): for templatename, param in data.items():
template_config = user_config.get(templatename, {}) template_config = user_config.get(templatename, {})
try:
variables[templatename] = self._get_variables(param, template_config) variables[templatename] = self._get_variables(param, template_config)
except errors.SchemaError as exception:
exception.message = "The songbook file is not valid\n"
exception.message += "'template' > '{}' >".format(templatename)
raise exception
return variables return variables
@staticmethod @staticmethod
def _get_variables(parameter, user_config): def _get_variables(parameter, user_config):
'''Get the default value for the parameter, according to the language. '''Get the default value for the parameter, according to the language.
May raise an errors.SBFileError if the data does not respect the schema Will raise `SchemaError` if the data does not respect the schema
''' '''
data = utils.DictOfDict(parameter.get('default', {})) data = utils.DictOfDict(parameter.get('default', {}))
data.update(user_config) data.update(user_config)

5
patacrep/utils.py

@ -81,7 +81,7 @@ def yesno(string):
def validate_yaml_schema(data, schema): def validate_yaml_schema(data, schema):
"""Check that the data respects the schema """Check that the data respects the schema
Will raise `SBFileError` if the schema is not respected. Will raise `SchemaError` if the schema is not respected.
""" """
schema = Rx.make_schema(schema) schema = Rx.make_schema(schema)
@ -91,5 +91,4 @@ def validate_yaml_schema(data, schema):
try: try:
schema.validate(data) schema.validate(data)
except Rx.SchemaMismatch as exception: except Rx.SchemaMismatch as exception:
msg = 'Could not parse songbook file:\n' + str(exception) raise errors.SchemaError(rx_exception=exception)
raise errors.SBFileError(msg)
Loading…
Cancel
Save