Browse Source

[songs.chordpro] Warn when using unknown directives

pull/152/head
Louis 9 years ago
parent
commit
5bc7973caf
  1. 26
      patacrep/songs/chordpro/ast.py
  2. 1
      test/test_chordpro/invalid_directive.sgc
  3. 2
      test/test_chordpro/invalid_directive.source
  4. 10
      test/test_chordpro/invalid_directive.tex

26
patacrep/songs/chordpro/ast.py

@ -11,6 +11,27 @@ def _indent(string):
"""Return and indented version of argument."""
return "\n".join([" {}".format(line) for line in string.split('\n')])
#: Available directives, that is, directives that we know how to deal with
AVAILABLE_DIRECTIVES = [
"album",
"artist",
"capo",
"comment",
"copyright",
"columns",
"cov",
"define",
"guitar_comment",
"image",
"key",
"lang",
"newline",
"partition",
"subtitle",
"tag",
"title",
]
#: List of properties that are to be displayed in the flow of the song (not as
#: metadata at the beginning or end of song.
INLINE_DIRECTIVES = {
@ -207,6 +228,7 @@ class Song(AST):
def add(self, data):
"""Add an element to the song"""
# pylint: disable=too-many-branches
if isinstance(data, Error):
pass
elif data is None:
@ -221,6 +243,8 @@ class Song(AST):
self.content[0].prepend(data.strip())
elif isinstance(data, Directive) and data.inline:
# Add a directive in the content of the song.
# It is useless to check if directive is in AVAILABLE_DIRECTIVES,
# since it is in INLINE_DIRECTIVES.
self.content.append(data)
elif data.inline:
# Add an object in the content of the song.
@ -228,6 +252,8 @@ class Song(AST):
elif isinstance(data, Directive):
# Add a metadata directive. Some of them are added using special
# methods listed in ``METADATA_ADD``.
if data.keyword not in AVAILABLE_DIRECTIVES:
LOGGER.warning("Ignoring unknown directive '{}'.".format(data.keyword))
if data.keyword in self.METADATA_ADD:
getattr(self, self.METADATA_ADD[data.keyword])(data)
else:

1
test/test_chordpro/invalid_directive.sgc

@ -0,0 +1 @@
{lang: en}

2
test/test_chordpro/invalid_directive.source

@ -0,0 +1,2 @@
{foo: bar}
{bar}

10
test/test_chordpro/invalid_directive.tex

@ -0,0 +1,10 @@
\selectlanguage{english}
\beginsong{}[
by={
},
]
\endsong
Loading…
Cancel
Save