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