Browse Source

Merge schema and default arguments

pull/184/head
Oliverpool 9 years ago
parent
commit
727c6a20c6
  1. 28
      patacrep/build.py
  2. 41
      patacrep/data/templates/default_songbook.sb.yml
  3. 121
      patacrep/data/templates/songbook_model.yml
  4. 78
      patacrep/data/templates/songbook_schema.yml
  5. 10
      patacrep/songbook/__main__.py
  6. 5
      test/test_song/test_parser.py

28
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

41
patacrep/data/templates/default_songbook.sb.yml

@ -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

121
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

78
patacrep/data/templates/songbook_schema.yml

@ -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

10
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)

5
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'] = []

Loading…
Cancel
Save