Browse Source

Merge branch 'include' of github.com:patacrep/patacrep into include

pull/53/head
Louis 11 years ago
parent
commit
2d8dda902f
  1. 30
      patacrep/content/include.py
  2. 6
      songbook

30
patacrep/content/include.py

@ -11,43 +11,47 @@ import os
import sys
import logging
from patacrep.content import process_content
from patacrep.content import process_content, ContentError
LOGGER = logging.getLogger(__name__)
def load_from_datadirs(path, config=None):
if not config or not config["datadir"]:
LOGGER.error("No datadir in the configuration.")
sys.exit(1)
for datadir in config["datadir"]:
filepath = os.path.join(datadir, path)
if os.path.exists(filepath):
return filepath
# File not found
raise ContentError("include", "The file '{0}' was not found in the "
"datadirs.".format(path))
#pylint: disable=unused-argument
def parse(keyword, config, argument, contentlist):
"""Include an external file content.
Arguments:
- keyword: the string 'sorted';
- keyword: the string 'include';
- config: the current songbook configuration dictionary;
- argument: None;
- contentlist: a list of file paths to be included.
"""
new_contentlist = []
if not os.path.isdir(config.get("_songbook_dir", "")):
LOGGER.warning("No songbook directory in configuration. 'include' "
"keyword may fail.")
for path in contentlist:
filepath = os.path.join(config["_songbook_dir"], path)
filepath = load_from_datadirs(path, config)
try:
with open(filepath, "r") as content_file:
old_songbook_dir = config["_songbook_dir"]
new_content = json.load(content_file)
except Exception as error: # pylint: disable=broad-except
LOGGER.error(error)
LOGGER.error("Error while loading file '{}'.".format(filepath))
sys.exit(1)
config["_songbook_dir"] = os.path.abspath(
os.path.dirname(filepath)
)
config["datadir"].append(os.path.abspath(os.path.dirname(filepath)))
new_contentlist += process_content(new_content, config)
config["_songbook_dir"] = old_songbook_dir
config["datadir"].pop()
return new_contentlist

6
songbook

@ -108,8 +108,6 @@ def main():
LOGGER.error("Error while loading file '{}'.".format(songbook_path))
sys.exit(1)
songbook["_songbook_dir"] = os.path.abspath(os.path.dirname(songbook_path))
# Gathering datadirs
datadirs = []
if options.datadir:
@ -126,9 +124,9 @@ def main():
)
for path in songbook['datadir']
]
if not datadirs:
# Default value
datadirs = [os.path.dirname(os.path.abspath(songbook_path))]
datadirs.append(os.path.dirname(os.path.abspath(songbook_path)))
songbook['datadir'] = datadirs
try:

Loading…
Cancel
Save