Browse Source

Section is now full yaml

pull/190/head
Oliverpool 9 years ago
parent
commit
e1e6b6d302
  1. 51
      patacrep/content/__init__.py
  2. 26
      patacrep/content/section.py
  3. 12
      test/test_content/sections.source
  4. 14
      test/test_content/sections_short.source
  5. 8
      test/test_content/test_content.py
  6. 15
      test/test_songbook/content.sb
  7. 2
      test/test_songbook/content_datadir/songs/include.sbc

51
patacrep/content/__init__.py

@ -232,8 +232,8 @@ def process_content(content, config=None):
"""Process content, and return a list of ContentItem() objects. """Process content, and return a list of ContentItem() objects.
Arguments are: Arguments are:
- content: the content field of the .sb file, which should be a list, and - content: the content field of the .sb file, which should be an imbricated list
describe what is to be included in the songbook; and describe what is to be included in the songbook;
- config: the configuration dictionary of the current songbook. - config: the configuration dictionary of the current songbook.
Return: a list of ContentItem objects, corresponding to the content to be Return: a list of ContentItem objects, corresponding to the content to be
@ -245,21 +245,34 @@ def process_content(content, config=None):
if not content: if not content:
content = [["song"]] content = [["song"]]
for elem in content: for elem in content:
if isinstance(elem, str): if isinstance(elem, dict):
elem = ["song", elem] # new way
try: for keyword, argument in elem.items():
match = keyword_re.match(elem[0]).groupdict() if keyword not in plugins:
except AttributeError: contentlist.append_error(ContentError(keyword, "Unknown content type."))
contentlist.append_error(ContentError(elem[0], "Cannot parse content type.")) continue
continue contentlist.extend(plugins[keyword](
(keyword, argument) = (match['keyword'], match['argument']) keyword,
if keyword not in plugins: argument=argument,
contentlist.append_error(ContentError(keyword, "Unknown content type.")) config=config,
continue ))
contentlist.extend(plugins[keyword]( else:
keyword, # old way
argument=argument, if isinstance(elem, str):
contentlist=elem[1:], elem = ["song", elem]
config=config, try:
)) match = keyword_re.match(elem[0]).groupdict()
except AttributeError:
contentlist.append_error(ContentError(elem[0], "Cannot parse content type."))
continue
(keyword, argument) = (match['keyword'], match['argument'])
if keyword not in plugins:
contentlist.append_error(ContentError(keyword, "Unknown content type."))
continue
contentlist.extend(plugins[keyword](
keyword,
argument=argument,
contentlist=elem[1:],
config=config,
))
return contentlist return contentlist

26
patacrep/content/section.py

@ -23,35 +23,29 @@ class Section(ContentItem):
self.short = short self.short = short
def render(self, __context): def render(self, __context):
if self.short is None: if self.short is None or self.keyword not in KEYWORDS:
return r'\{}{{{}}}'.format(self.keyword, self.name) return r'\{}{{{}}}'.format(self.keyword, self.name)
else: else:
return r'\{}[{}]{{{}}}'.format(self.keyword, self.short, self.name) return r'\{}[{}]{{{}}}'.format(self.keyword, self.short, self.name)
#pylint: disable=unused-argument #pylint: disable=unused-argument
def parse(keyword, argument, contentlist, config): def parse(keyword, argument, config):
"""Parse the contentlist. """Parse the contentlist.
Arguments: Arguments:
- keyword (one of "part", "chapter", "section", ... , "subparagraph", and - keyword (one of "part", "chapter", "section", ... , "subparagraph", and
their starred versions "part*", "chapter*", ... , "subparagraph*"): the their starred versions "part*", "chapter*", ... , "subparagraph*"): the
section to use; section to use;
- argument: unused; - argument:
- contentlist: a list of one or two strings, which are the names (long either a string describing the section name
and short) of the section; or a dict
name: Name of the section
short: Shortname of the section (only for non starred sections)
- config: configuration dictionary of the current songbook. - config: configuration dictionary of the current songbook.
""" """
try: if isinstance(argument, str):
if (keyword not in KEYWORDS) and (len(contentlist) != 1): argument = {'name': argument}
raise ContentError( return ContentList([Section(keyword, **argument)])
keyword,
"Starred section names must have exactly one argument."
)
if (len(contentlist) not in [1, 2]):
raise ContentError(keyword, "Section can have one or two arguments.")
return ContentList([Section(keyword, *contentlist)])
except ContentError as error:
return EmptyContentList(errors=[error])
CONTENT_PLUGINS = dict([ CONTENT_PLUGINS = dict([

12
test/test_content/sections.source

@ -1,6 +1,6 @@
[["section", "Traditional"], - section: Traditional
"exsong.sg", - "exsong.sg"
["section", "Example"], - section: Example
"texsong.tsg", - "texsong.tsg"
"chordpro.csg", - "chordpro.csg"
"exsong.sg"] - "exsong.sg"

14
test/test_content/sections_short.source

@ -1,6 +1,8 @@
[["section", "Traditional", "tradi"], - section:
"exsong.sg", name: Traditional
["section*", "Example"], short: tradi
"texsong.tsg", - "exsong.sg"
"chordpro.csg", - section*: Example
"exsong.sg"] - "texsong.tsg"
- "chordpro.csg"
- "exsong.sg"

8
test/test_content/test_content.py

@ -5,7 +5,7 @@
import glob import glob
import os import os
import unittest import unittest
import json import yaml
from patacrep.songs import DataSubpath from patacrep.songs import DataSubpath
from patacrep import content, files from patacrep import content, files
@ -18,7 +18,7 @@ from .. import dynamic # pylint: disable=unused-import
class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest): class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
"""Test of the content plugins. """Test of the content plugins.
For any given `foo.source`, it parses the content as a json "content" For any given `foo.source`, it parses the content as a yaml "content"
argument of a .sb file. argument of a .sb file.
It controls that the generated file list is equal to the one in `foo.control`. It controls that the generated file list is equal to the one in `foo.control`.
""" """
@ -51,7 +51,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
"""Test that `base.source` produces the correct file list""" """Test that `base.source` produces the correct file list"""
sourcename = "{}.source".format(base) sourcename = "{}.source".format(base)
with open(sourcename, mode="r", encoding="utf8") as sourcefile: with open(sourcename, mode="r", encoding="utf8") as sourcefile:
sbcontent = json.load(sourcefile) sbcontent = yaml.load(sourcefile)
with logging_reduced('patacrep.content.song'): with logging_reduced('patacrep.content.song'):
expandedlist = content.process_content(sbcontent, cls.config.copy()) expandedlist = content.process_content(sbcontent, cls.config.copy())
@ -61,7 +61,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
if not os.path.exists(controlname): if not os.path.exists(controlname):
raise Exception("Missing control:" + str(sourcelist).replace("'", '"')) raise Exception("Missing control:" + str(sourcelist).replace("'", '"'))
with open(controlname, mode="r", encoding="utf8") as controlfile: with open(controlname, mode="r", encoding="utf8") as controlfile:
controllist = json.load(controlfile) controllist = yaml.load(controlfile)
self.assertEqual(controllist, sourcelist) self.assertEqual(controllist, sourcelist)

15
test/test_songbook/content.sb

@ -6,13 +6,12 @@ chords:
repeatchords: no repeatchords: no
diagramreminder: all diagramreminder: all
content: [ content:
["section", "Test of section"], - section: Test of section
["sorted"], - ["sorted"]
["songsection", "Test of song section"], - ["songsection", "Test of song section"]
["cwd(content_datadir/content)", - ["cwd(content_datadir/content)",
"song.csg", "song.tsg", "song.csg", "song.tsg",
["tex", "foo.tex"] ["tex", "foo.tex"]
], ]
["include", "include.sbc"] - ["include", "include.sbc"]
]

2
test/test_songbook/content_datadir/songs/include.sbc

@ -1 +1 @@
[["section", "This is an included section"]] [{"section": "This is an included section"}]

Loading…
Cancel
Save