From 5bc7973cafccc2c31de866751f7c064bf703217a Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 1 Nov 2015 05:07:04 +0100 Subject: [PATCH] [songs.chordpro] Warn when using unknown directives --- patacrep/songs/chordpro/ast.py | 26 +++++++++++++++++++++ test/test_chordpro/invalid_directive.sgc | 1 + test/test_chordpro/invalid_directive.source | 2 ++ test/test_chordpro/invalid_directive.tex | 10 ++++++++ 4 files changed, 39 insertions(+) create mode 100644 test/test_chordpro/invalid_directive.sgc create mode 100644 test/test_chordpro/invalid_directive.source create mode 100644 test/test_chordpro/invalid_directive.tex diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index 9213ed37..87ce2a34 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/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: diff --git a/test/test_chordpro/invalid_directive.sgc b/test/test_chordpro/invalid_directive.sgc new file mode 100644 index 00000000..768ee9ee --- /dev/null +++ b/test/test_chordpro/invalid_directive.sgc @@ -0,0 +1 @@ +{lang: en} diff --git a/test/test_chordpro/invalid_directive.source b/test/test_chordpro/invalid_directive.source new file mode 100644 index 00000000..4f582c43 --- /dev/null +++ b/test/test_chordpro/invalid_directive.source @@ -0,0 +1,2 @@ +{foo: bar} +{bar} diff --git a/test/test_chordpro/invalid_directive.tex b/test/test_chordpro/invalid_directive.tex new file mode 100644 index 00000000..bd99a922 --- /dev/null +++ b/test/test_chordpro/invalid_directive.tex @@ -0,0 +1,10 @@ +\selectlanguage{english} + +\beginsong{}[ + by={ + }, +] + + + +\endsong