Browse Source

Check the schema before generating ContentList

pull/190/head
Oliverpool 9 years ago
parent
commit
1eb211db0a
  1. 44
      patacrep/content/__init__.py
  2. 7
      patacrep/content/cwd.py
  3. 8
      patacrep/content/include.py
  4. 10
      patacrep/content/section.py
  5. 3
      patacrep/content/song.py
  6. 3
      patacrep/content/songsection.py
  7. 15
      patacrep/content/sorted.py
  8. 7
      patacrep/content/tex.py
  9. 2
      patacrep/utils.py

44
patacrep/content/__init__.py

@ -72,9 +72,10 @@ import re
import sys
import jinja2
import yaml
from patacrep import files
from patacrep.errors import SharedError
from patacrep import files, utils
from patacrep.errors import SBFileError, SharedError
LOGGER = logging.getLogger(__name__)
EOL = '\n'
@ -228,6 +229,38 @@ def render(context, content):
return rendered
def build_plugin_schema(plugins):
"""Builds the Rx schema for the ContentItem"""
plugin_schemas = {}
for keyword, parser in plugins.items():
subschema = getattr(parser, 'rxschema', '//any')
plugin_schemas[keyword] = yaml.load(subschema)
plugin_schema = [{
'type': '//rec',
'optional': plugin_schemas,
}]
song_schema = {
'type': '//str',
}
plugin_schema.append(song_schema)
return {
'type': '//any',
'of': plugin_schema,
}
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):
"""Process content, and return a list of ContentItem() objects.
@ -242,7 +275,12 @@ def process_content(content, config=None):
contentlist = ContentList()
plugins = config.get('_content_plugins', {})
if not content:
content = [{'song': None}]
content = [{'song': ""}]
try:
validate_content(content, plugins)
except SBFileError as error:
contentlist.append_error(ContentError("Invalid content", str(error)))
return contentlist
for elem in content:
if isinstance(elem, str):
elem = {'song': [elem]}

7
patacrep/content/cwd.py

@ -33,4 +33,11 @@ def parse(keyword, config, argument):
config['_songdir'] = old_songdir
return processed_content
parse.rxschema = """
type: //rec
required:
path: //str
content: //any
"""
CONTENT_PLUGINS = {'cwd': parse}

8
patacrep/content/include.py

@ -69,4 +69,12 @@ def parse(keyword, config, argument):
return new_contentlist
parse.rxschema = """
type: //any
of:
- type: //str
- type: //arr
contents: //str
"""
CONTENT_PLUGINS = {'include': parse}

10
patacrep/content/section.py

@ -47,6 +47,16 @@ def parse(keyword, argument, config):
argument = {'name': argument}
return ContentList([Section(keyword, **argument)])
parse.rxschema = """
type: //any
of:
- type: //str
- type: //rec
required:
name: //str
optional:
short: //str
"""
CONTENT_PLUGINS = dict([
(word, parse)

3
patacrep/content/song.py

@ -121,6 +121,9 @@ def parse(keyword, argument, config):
))
return sorted(songlist)
parse.rxschema = """
type: //str
"""
CONTENT_PLUGINS = {'song': parse}

3
patacrep/content/songsection.py

@ -30,6 +30,9 @@ def parse(keyword, argument, config):
"""
return ContentList([SongSection(keyword, argument)])
parse.rxschema = """
//str
"""
CONTENT_PLUGINS = dict([
(keyword, parse)

15
patacrep/content/sorted.py

@ -94,4 +94,19 @@ def parse(keyword, config, argument):
))])
return sorted(songlist, key=key_generator(sort))
parse.rxschema = """
type: //any
of:
- type: //nil
- type: //rec
optional:
key:
type: //any
of:
- //str
- type: //arr
contents: //str
content: //any
"""
CONTENT_PLUGINS = {'sorted': parse}

7
patacrep/content/tex.py

@ -62,5 +62,12 @@ def parse(keyword, argument, config):
return filelist
parse.rxschema = """
type: //any
of:
- type: //arr
contents: //str
- type: //str
"""
CONTENT_PLUGINS = {'tex': parse}

2
patacrep/utils.py

@ -86,7 +86,7 @@ def validate_yaml_schema(data, schema):
rx_checker = Rx.Factory({"register_core_types": True})
schema = rx_checker.make_schema(schema)
if not isinstance(data, dict):
if isinstance(data, DictOfDict):
data = dict(data)
try:

Loading…
Cancel
Save