|
@ -68,7 +68,7 @@ import sys |
|
|
import jinja2 |
|
|
import jinja2 |
|
|
import yaml |
|
|
import yaml |
|
|
|
|
|
|
|
|
from patacrep import files, utils |
|
|
from patacrep import files, Rx, utils |
|
|
from patacrep.errors import SBFileError, SharedError |
|
|
from patacrep.errors import SBFileError, SharedError |
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__) |
|
|
LOGGER = logging.getLogger(__name__) |
|
@ -223,37 +223,31 @@ def render(context, content): |
|
|
|
|
|
|
|
|
return rendered |
|
|
return rendered |
|
|
|
|
|
|
|
|
def build_plugin_schema(plugins): |
|
|
def validate_parser_argument(raw_schema): |
|
|
"""Builds the Rx schema for the ContentItem""" |
|
|
"""Check that the parser argument respects the schema |
|
|
plugin_schemas = {} |
|
|
|
|
|
for keyword, parser in plugins.items(): |
|
|
Will raise `SBFileError` if the schema is not respected. |
|
|
subschema = getattr(parser, 'rxschema', '//any') |
|
|
""" |
|
|
plugin_schemas[keyword] = yaml.load(subschema) |
|
|
rx_checker = Rx.Factory({"register_core_types": True}) |
|
|
plugin_schema = [{ |
|
|
schema = rx_checker.make_schema(yaml.load(raw_schema)) |
|
|
'type': '//rec', |
|
|
|
|
|
'optional': plugin_schemas, |
|
|
def wrap(parse): |
|
|
}] |
|
|
"""Wrap the parse function""" |
|
|
song_schema = { |
|
|
def wrapped(keyword, argument, config): |
|
|
'type': '//str', |
|
|
"""Check the argument schema before calling the plugin parser""" |
|
|
} |
|
|
try: |
|
|
plugin_schema.append(song_schema) |
|
|
schema.validate(argument) |
|
|
return { |
|
|
except Rx.SchemaMismatch as exception: |
|
|
'type': '//any', |
|
|
msg = 'Invalid `{}` syntax:\n---\n{}---\n{}'.format( |
|
|
'of': plugin_schema, |
|
|
keyword, |
|
|
} |
|
|
yaml.dump({keyword: argument}, default_flow_style=False), |
|
|
|
|
|
str(exception) |
|
|
|
|
|
) |
|
|
|
|
|
raise SBFileError(msg) |
|
|
|
|
|
return parse(keyword, argument=argument, config=config) |
|
|
|
|
|
return wrapped |
|
|
|
|
|
return wrap |
|
|
|
|
|
|
|
|
def validate_content(content, plugins): |
|
|
|
|
|
"""Validate the content against the Rx content schema""" |
|
|
|
|
|
plugin_schema = build_plugin_schema(plugins) |
|
|
|
|
|
content_schema = { |
|
|
|
|
|
'type': '//any', |
|
|
|
|
|
'of': [ |
|
|
|
|
|
plugin_schema, |
|
|
|
|
|
{'type': '//arr', 'contents':plugin_schema}, |
|
|
|
|
|
#{'type': '//nil'}, |
|
|
|
|
|
] |
|
|
|
|
|
} |
|
|
|
|
|
utils.validate_yaml_schema(content, content_schema) |
|
|
|
|
|
|
|
|
|
|
|
def process_content(content, config=None): |
|
|
def process_content(content, config=None): |
|
|
"""Process content, and return a list of ContentItem() objects. |
|
|
"""Process content, and return a list of ContentItem() objects. |
|
@ -269,19 +263,14 @@ def process_content(content, config=None): |
|
|
contentlist = ContentList() |
|
|
contentlist = ContentList() |
|
|
plugins = config.get('_content_plugins', {}) |
|
|
plugins = config.get('_content_plugins', {}) |
|
|
if not content: |
|
|
if not content: |
|
|
content = [{'song': ""}] |
|
|
content = [{'song': None}] |
|
|
try: |
|
|
|
|
|
validate_content(content, plugins) |
|
|
|
|
|
except SBFileError as error: |
|
|
|
|
|
contentlist.append_error(ContentError("Invalid content", str(error))) |
|
|
|
|
|
return contentlist |
|
|
|
|
|
for elem in content: |
|
|
for elem in content: |
|
|
if isinstance(elem, str): |
|
|
if isinstance(elem, str): |
|
|
elem = {'song': [elem]} |
|
|
elem = {'song': [elem]} |
|
|
if isinstance(elem, dict): |
|
|
if isinstance(elem, dict): |
|
|
for keyword, argument in elem.items(): |
|
|
for keyword, argument in elem.items(): |
|
|
if keyword not in plugins: |
|
|
if keyword not in plugins: |
|
|
contentlist.append_error(ContentError(keyword, "Unknown content type.")) |
|
|
contentlist.append_error(ContentError(keyword, "Unknown content keyword.")) |
|
|
continue |
|
|
continue |
|
|
contentlist.extend(plugins[keyword]( |
|
|
contentlist.extend(plugins[keyword]( |
|
|
keyword, |
|
|
keyword, |
|
|