diff --git a/examples/example-all.yaml.sb b/examples/example-all.yaml.sb index 3a167565..4f287c6d 100644 --- a/examples/example-all.yaml.sb +++ b/examples/example-all.yaml.sb @@ -1,17 +1,23 @@ -bookoptions: - - "diagram" - - "repeatchords" - - "lilypond" - - "pictures" -booktype: "chorded" -datadir: "." -template: "patacrep.tex" -lang: "fr" -encoding: "utf8" -authwords: - sep: +# bookoptions: +# - "diagram" +# - "repeatchords" +# - "pictures" +# booktype: "chorded" +# datadir: "." +# template: "patacrep.tex" +# lang: "fr" +# encoding: "utf8" +book: + lang: fr + datadir: "." + pictures: yes + #type: chorded +chords: + show: true + diagramreminder: none + +authors: + separators: - "and" - "et" -content: - - - - "sorted" \ No newline at end of file +content: '[["sorted"]]' \ No newline at end of file diff --git a/patacrep/build.py b/patacrep/build.py index 60480ff1..065c8604 100644 --- a/patacrep/build.py +++ b/patacrep/build.py @@ -10,7 +10,7 @@ from subprocess import Popen, PIPE, call, check_call import yaml -from patacrep import authors, content, errors, encoding, files, pkg_datapath, Rx, utils +from patacrep import authors, content, errors, encoding, files, utils from patacrep.index import process_sxd from patacrep.templates import TexBookRenderer from patacrep.songs import DataSubpath, DEFAULT_CONFIG @@ -43,7 +43,8 @@ class Songbook(object): def __init__(self, raw_songbook, basename): super().__init__() - self.config = check_config_schema(raw_songbook) + self.config = raw_songbook + utils.validate_config_schema(raw_songbook) self.basename = basename # Some special keys have their value processed. self._set_datadir() @@ -328,20 +329,3 @@ class SongbookBuilder(object): os.unlink(self.basename + ext) except Exception as exception: raise errors.CleaningError(self.basename + ext, exception) - - -def check_config_schema(data): - """ - Check that the data respect the excepted songbook schema - - """ - rx_checker = Rx.Factory({"register_core_types": True}) - schema_path = pkg_datapath('templates', 'songbook_schema.yml') - with encoding.open_read(schema_path) as schema_file: - schema_struct = yaml.load(schema_file) - schema_struct = utils.remove_keys(schema_struct, ['_default', '_description']) - schema = rx_checker.make_schema(schema_struct) - - if schema.check(data): - return data - raise errors.SBFileError('The songbook file does not respect the schema') diff --git a/patacrep/data/templates/default_songbook.sb.yml b/patacrep/data/templates/default_songbook.sb.yml new file mode 100644 index 00000000..54a8291d --- /dev/null +++ b/patacrep/data/templates/default_songbook.sb.yml @@ -0,0 +1,46 @@ +content: + +book: + lang: en + datadir: + encoding: utf-8 + pictures: yes + +chords: # Options relatives aux accords + diagramreminder: ENUM[important, none, all] (anciennement importantdiagramonly) + diagrampage: BOOLEAN Montrer la page d'accords + repeatchords: BOOLEAN + lilypond: BOOLEAN + instruments: ENUM[guitar, ukulele] + show: BOOLEAN (anciennement booktype=chorded ou booktype=lyrics) + notation: ENUM[alphascale, solfedge] + +authors: # Comment sont analysés les auteurs + separators: LISTE + ignore: LISTE + by: LISTE + +titles: # Comment sont analysés les titres + prefix: LISTE + +template: # Des choses spécifiques au template + file: STRING + latex: # Des choses spécifiques au LaTeX + classoptions: STRING + + # Peut dépendre fortement du template + color: # Des couleurs + songnumber: STRING + notebg: STRING + indexbg: STRING + + titlepage: #Configuration de la page de garde + title: STRING + author: STRING + subtitle: STRING + version: STRING + url: STRING + email: STRING + picture: STRING + picturecopyright: STRING + footer: STRING \ No newline at end of file diff --git a/patacrep/data/templates/songbook_schema.yml b/patacrep/data/templates/songbook_schema.yml index c41d2caa..6bbb7ba3 100644 --- a/patacrep/data/templates/songbook_schema.yml +++ b/patacrep/data/templates/songbook_schema.yml @@ -1,2 +1,89 @@ -type: //any -_default: "test" \ No newline at end of file +type: //rec +optional: + content: //str + book: + type: //rec + optional: + encoding: //str + lang: //str + pictures: //bool + datadir: + type: //arr + contents: //str + chords: + type: //rec + optional: + show: //bool + diagrampage: //bool + repeatchords: //bool + lilypond: //bool + diagramreminder: + type: //any + of: + - type: //str + value: "none" + - type: //str + value: "important" + - type: //str + value: "all" + instruments: + type: //any + of: + - type: //str + value: "guitar" + - type: //str + value: "ukulele" + notation: + type: //any + of: + - type: //str + value: "alphascale" + - type: //str + value: "solfedge" + authors: + _description: "Comment sont analysés les auteurs" + type: //rec + optional: + separators: + type: //arr + contents: //str + ignore: + type: //arr + contents: //str + by: + type: //arr + contents: //str + titles: + _description: "Comment sont analysés les titres" + type: //rec + optional: + prefix: + type: //arr + contents: //str + template: + _description: "Des choses spécifiques au template" + type: //rec + optional: + file: //str + latex: + type: //rec + optional: + classoptions: //str + color: + type: //rec + optional: + songnumber: //str + notebg: //str + indexbg: //str + titlepage: + type: //rec + optional: + title: //str + author: //str + subtitle: //str + version: //str + url: //str + email: //str + picture: //str + picturecopyright: //str + footer: //str diff --git a/patacrep/utils.py b/patacrep/utils.py index d7a4142d..54855b9f 100644 --- a/patacrep/utils.py +++ b/patacrep/utils.py @@ -2,6 +2,10 @@ from collections import UserDict +import yaml + +from patacrep import encoding, errors, pkg_datapath, Rx + class DictOfDict(UserDict): """Dictionary, with a recursive :meth:`update` method. @@ -91,3 +95,25 @@ def remove_keys(data, keys=None, recursive=True): elif isinstance(data, list) and recursive: return [remove_keys(elt, keys, True) for elt in data] return data + +def validate_config_schema(config): + """ + Check that the songbook config respects the excepted songbook schema + + """ + data = config.copy() + data = remove_keys(data, ['_cache']) + + rx_checker = Rx.Factory({"register_core_types": True}) + schema_path = pkg_datapath('templates', 'songbook_schema.yml') + with encoding.open_read(schema_path) as schema_file: + schema_struct = yaml.load(schema_file) + schema_struct = remove_keys(schema_struct, ['_description']) + schema = rx_checker.make_schema(schema_struct) + + try: + schema.validate(data) + except Rx.SchemaMismatch as exception: + msg = 'Could not parse songbook file:\n' + str(exception) + raise errors.SBFileError(msg) + return True diff --git a/test/test_songbook/datadir.sb b/test/test_songbook/datadir.sb index f44c3dba..0763bc7a 100644 --- a/test/test_songbook/datadir.sb +++ b/test/test_songbook/datadir.sb @@ -1,6 +1,6 @@ -bookoptions: -- pictures -datadir: -- datadir_datadir -- datadir_datadir2 -lang: en +book: + pictures: yes + datadir: + - datadir_datadir + - datadir_datadir2 + lang: en