Browse Source

Complete schema

pull/184/head
Oliverpool 9 years ago
parent
commit
02551f16d0
  1. 36
      examples/example-all.yaml.sb
  2. 22
      patacrep/build.py
  3. 46
      patacrep/data/templates/default_songbook.sb.yml
  4. 91
      patacrep/data/templates/songbook_schema.yml
  5. 26
      patacrep/utils.py
  6. 12
      test/test_songbook/datadir.sb

36
examples/example-all.yaml.sb

@ -1,17 +1,23 @@
bookoptions: # bookoptions:
- "diagram" # - "diagram"
- "repeatchords" # - "repeatchords"
- "lilypond" # - "pictures"
- "pictures" # booktype: "chorded"
booktype: "chorded" # datadir: "."
datadir: "." # template: "patacrep.tex"
template: "patacrep.tex" # lang: "fr"
lang: "fr" # encoding: "utf8"
encoding: "utf8" book:
authwords: lang: fr
sep: datadir: "."
pictures: yes
#type: chorded
chords:
show: true
diagramreminder: none
authors:
separators:
- "and" - "and"
- "et" - "et"
content: content: '[["sorted"]]'
-
- "sorted"

22
patacrep/build.py

@ -10,7 +10,7 @@ from subprocess import Popen, PIPE, call, check_call
import yaml 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.index import process_sxd
from patacrep.templates import TexBookRenderer from patacrep.templates import TexBookRenderer
from patacrep.songs import DataSubpath, DEFAULT_CONFIG from patacrep.songs import DataSubpath, DEFAULT_CONFIG
@ -43,7 +43,8 @@ class Songbook(object):
def __init__(self, raw_songbook, basename): def __init__(self, raw_songbook, basename):
super().__init__() super().__init__()
self.config = check_config_schema(raw_songbook) self.config = raw_songbook
utils.validate_config_schema(raw_songbook)
self.basename = basename self.basename = basename
# Some special keys have their value processed. # Some special keys have their value processed.
self._set_datadir() self._set_datadir()
@ -328,20 +329,3 @@ class SongbookBuilder(object):
os.unlink(self.basename + ext) os.unlink(self.basename + ext)
except Exception as exception: except Exception as exception:
raise errors.CleaningError(self.basename + ext, 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')

46
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

91
patacrep/data/templates/songbook_schema.yml

@ -1,2 +1,89 @@
type: //any type: //rec
_default: "test" 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

26
patacrep/utils.py

@ -2,6 +2,10 @@
from collections import UserDict from collections import UserDict
import yaml
from patacrep import encoding, errors, pkg_datapath, Rx
class DictOfDict(UserDict): class DictOfDict(UserDict):
"""Dictionary, with a recursive :meth:`update` method. """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: elif isinstance(data, list) and recursive:
return [remove_keys(elt, keys, True) for elt in data] return [remove_keys(elt, keys, True) for elt in data]
return 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

12
test/test_songbook/datadir.sb

@ -1,6 +1,6 @@
bookoptions: book:
- pictures pictures: yes
datadir: datadir:
- datadir_datadir - datadir_datadir
- datadir_datadir2 - datadir_datadir2
lang: en lang: en

Loading…
Cancel
Save