From 727c6a20c6c3bb087019937715be9c2746ada028 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Fri, 18 Dec 2015 12:06:52 +0100 Subject: [PATCH] Merge schema and default arguments --- patacrep/build.py | 28 ++-- .../data/templates/default_songbook.sb.yml | 41 ------ patacrep/data/templates/songbook_model.yml | 121 ++++++++++++++++++ patacrep/data/templates/songbook_schema.yml | 78 ----------- patacrep/songbook/__main__.py | 10 +- test/test_song/test_parser.py | 5 +- 6 files changed, 143 insertions(+), 140 deletions(-) delete mode 100644 patacrep/data/templates/default_songbook.sb.yml create mode 100644 patacrep/data/templates/songbook_model.yml delete mode 100644 patacrep/data/templates/songbook_schema.yml diff --git a/patacrep/build.py b/patacrep/build.py index 29b2502e..01553bc0 100644 --- a/patacrep/build.py +++ b/patacrep/build.py @@ -44,21 +44,15 @@ class Songbook(object): def __init__(self, raw_songbook, basename): super().__init__() self.config = raw_songbook - self.validate_config_schema(raw_songbook) + + # Validate config + schema = config_model('schema') + utils.validate_yaml_schema(raw_songbook, schema) + self.basename = basename # Some special keys have their value processed. self._set_datadir() - @staticmethod - def validate_config_schema(raw_songbook): - """ - Check that the songbook config respects the excepted songbook schema - """ - schema_path = pkg_datapath('templates', 'songbook_schema.yml') - with encoding.open_read(schema_path) as schema_file: - schema_struct = yaml.load(schema_file) - utils.validate_yaml_schema(raw_songbook, schema_struct) - def _set_datadir(self): """Set the default values for datadir""" abs_datadir = [] @@ -335,3 +329,15 @@ class SongbookBuilder(object): os.unlink(self.basename + ext) except Exception as exception: raise errors.CleaningError(self.basename + ext, exception) + + +def config_model(*args): + """Get the model structure with schema and default options""" + model_path = pkg_datapath('templates', 'songbook_model.yml') + with encoding.open_read(model_path) as model_file: + data = yaml.load(model_file) + + while data and args: + name, *args = args + data = data.get(name) + return data diff --git a/patacrep/data/templates/default_songbook.sb.yml b/patacrep/data/templates/default_songbook.sb.yml deleted file mode 100644 index 67a9a464..00000000 --- a/patacrep/data/templates/default_songbook.sb.yml +++ /dev/null @@ -1,41 +0,0 @@ -book: - lang: en - encoding: utf-8 - pictures: yes - template: default.tex - onesongperpage: no - -chords: # Options relatives aux accords - show: yes - diagramreminder: important - diagrampage: yes - repeatchords: yes - lilypond: no - tablatures: no - instrument: guitar - notation: alphascale - -authors: # Comment sont analysés les auteurs - separators: - - and - ignore: - - unknown - after: - - by - -titles: # Comment sont analysés les titres - prefix: - - The - - Le - - La - - "L'" - - A - - Au - - Ces - - De - - Des - - El - - Les - - Ma - - Mon - - Un diff --git a/patacrep/data/templates/songbook_model.yml b/patacrep/data/templates/songbook_model.yml new file mode 100644 index 00000000..1f588996 --- /dev/null +++ b/patacrep/data/templates/songbook_model.yml @@ -0,0 +1,121 @@ +schema: + type: //rec + optional: + content: //str + template: //any + required: + _cache: //bool + _datadir: + type: //arr + contents: //str + book: + type: //rec + required: + encoding: //str + lang: //str + pictures: //bool + template: //str + onesongperpage: //bool + chords: + type: //rec + required: + show: //bool + diagrampage: //bool + repeatchords: //bool + lilypond: //bool + tablatures: //bool + diagramreminder: + type: //any + of: + - type: //str + value: "none" + - type: //str + value: "important" + - type: //str + value: "all" + instrument: + type: //any + of: + - type: //str + value: "guitar" + - type: //str + value: "ukulele" + notation: + type: //any + of: + - type: //str + value: "alphascale" + - type: //str + value: "solfedge" + authors: + type: //rec + required: + separators: + type: //any + of: + - type: //arr + contents: //str + - type: //nil + ignore: + type: //any + of: + - type: //arr + contents: //str + - type: //nil + after: + type: //any + of: + - type: //arr + contents: //str + - type: //nil + titles: + type: //rec + required: + prefix: + type: //any + of: + - type: //arr + contents: //str + - type: //nil +default: + book: + lang: en + encoding: utf-8 + pictures: yes + template: default.tex + onesongperpage: no + + chords: # Options relatives aux accords + show: yes + diagramreminder: important + diagrampage: yes + repeatchords: yes + lilypond: no + tablatures: no + instrument: guitar + notation: alphascale + + authors: # Comment sont analysés les auteurs + separators: + - and + ignore: + - unknown + after: + - by + + titles: # Comment sont analysés les titres + prefix: + - The + - Le + - La + - "L'" + - A + - Au + - Ces + - De + - Des + - El + - Les + - Ma + - Mon + - Un diff --git a/patacrep/data/templates/songbook_schema.yml b/patacrep/data/templates/songbook_schema.yml deleted file mode 100644 index ee75be64..00000000 --- a/patacrep/data/templates/songbook_schema.yml +++ /dev/null @@ -1,78 +0,0 @@ -type: //rec -optional: - content: //str - template: //any -required: - _cache: //bool - _datadir: - type: //arr - contents: //str - book: - type: //rec - required: - encoding: //str - lang: //str - pictures: //bool - template: //str - onesongperpage: //bool - chords: - type: //rec - required: - show: //bool - diagrampage: //bool - repeatchords: //bool - lilypond: //bool - tablatures: //bool - diagramreminder: - type: //any - of: - - type: //str - value: "none" - - type: //str - value: "important" - - type: //str - value: "all" - instrument: - type: //any - of: - - type: //str - value: "guitar" - - type: //str - value: "ukulele" - notation: - type: //any - of: - - type: //str - value: "alphascale" - - type: //str - value: "solfedge" - authors: - type: //rec - required: - separators: - type: //any - of: - - type: //arr - contents: //str - - type: //nil - ignore: - type: //any - of: - - type: //arr - contents: //str - - type: //nil - after: - type: //any - of: - - type: //arr - contents: //str - - type: //nil - titles: - type: //rec - required: - prefix: - type: //any - of: - - type: //arr - contents: //str - - type: //nil diff --git a/patacrep/songbook/__main__.py b/patacrep/songbook/__main__.py index 587b1d8d..adc581c5 100644 --- a/patacrep/songbook/__main__.py +++ b/patacrep/songbook/__main__.py @@ -8,10 +8,10 @@ import textwrap import sys import yaml -from patacrep.build import SongbookBuilder, DEFAULT_STEPS +from patacrep.build import SongbookBuilder, DEFAULT_STEPS, config_model from patacrep.utils import yesno, DictOfDict from patacrep import __version__ -from patacrep import errors, pkg_datapath +from patacrep import errors import patacrep.encoding # Logging configuration @@ -133,11 +133,6 @@ def main(): basename = os.path.basename(songbook_path)[:-3] - # Load the default songbook config - default_songbook_path = pkg_datapath('templates', 'default_songbook.sb.yml') - with patacrep.encoding.open_read(default_songbook_path) as default_songbook_file: - default_songbook = DictOfDict(yaml.load(default_songbook_file)) - # Load the user songbook config try: with patacrep.encoding.open_read(songbook_path) as songbook_file: @@ -154,6 +149,7 @@ def main(): sys.exit(1) # Merge the default and user configs + default_songbook = DictOfDict(config_model('default')) default_songbook.update(user_songbook) songbook = dict(default_songbook) diff --git a/test/test_song/test_parser.py b/test/test_song/test_parser.py index 6eefe382..0e7f4dcd 100644 --- a/test/test_song/test_parser.py +++ b/test/test_song/test_parser.py @@ -12,6 +12,7 @@ import yaml from patacrep import files, pkg_datapath from patacrep.encoding import open_read +from patacrep.build import config_model from .. import logging_reduced from .. import dynamic # pylint: disable=unused-import @@ -76,9 +77,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): """Iterate over song files to test.""" # Setting datadir # Load the default songbook config - default_songbook_path = pkg_datapath('templates', 'default_songbook.sb.yml') - with open_read(default_songbook_path) as default_songbook_file: - cls.config = yaml.load(default_songbook_file) + cls.config = config_model('default') if '_datadir' not in cls.config: cls.config['_datadir'] = []