コード例 #1
0
ファイル: include.py プロジェクト: takuyozora/patacrep
def parse(keyword, config, argument):
    """Include an external file content.

    Arguments:
        - keyword: the string 'include';
        - config: the current songbook configuration dictionary;
        - argument:
            a list of file paths to be included
            or a string of the file to include

    """
    new_contentlist = ContentList()
    if isinstance(argument, str):
        argument = [argument]

    for filename in argument:
        try:
            filepath = load_from_datadirs(
                filename,
                config['_songdir'],
                config.get('_songbookfile_dir')
            )
        except ContentError as error:
            new_contentlist.append_error(error)
            continue
        content_file = None
        try:
            with encoding.open_read(
                filepath,
                encoding=config['book']['encoding']
                ) as content_file:
                new_content = yaml.load(content_file)
        except Exception as error: # pylint: disable=broad-except
            new_contentlist.append_error(ContentError(
                keyword="include",
                message="Error while loading file '{}': {}".format(filepath, error),
                ))
            continue

        config['_datadir'].append(os.path.abspath(os.path.dirname(filepath)))
        new_contentlist.extend(process_content(new_content, config))
        config['_datadir'].pop()

    return new_contentlist
コード例 #2
0
ファイル: include.py プロジェクト: tintou/patacrep
def parse(keyword, config, argument, contentlist):
    """Include an external file content.

    Arguments:
        - keyword: the string 'include';
        - config: the current songbook configuration dictionary;
        - argument: None;
        - contentlist: a list of file paths to be included.
    """
    new_contentlist = ContentList()

    for path in contentlist:
        try:
            filepath = load_from_datadirs(path, config.get('datadir', []))
        except ContentError as error:
            new_contentlist.append_error(error)
            continue
        content_file = None
        try:
            with encoding.open_read(
                filepath,
                encoding=config['encoding']
                ) as content_file:
                new_content = json.load(content_file)
        except Exception as error: # pylint: disable=broad-except
            new_contentlist.append_error(ContentError(
                keyword="include",
                message="Error while loading file '{}': {}".format(filepath, error),
                ))
            continue

        config["datadir"].append(os.path.abspath(os.path.dirname(filepath)))
        new_contentlist.extend(process_content(new_content, config))
        config["datadir"].pop()

    return new_contentlist