Esempio n. 1
0
def parse(keyword, argument, config):
    """Parse the tex files.

    Arguments:
    - keyword: unused;
    - argument:
        a list of tex files to include
        or a string of the tex file to include;
    - config: configuration dictionary of the current songbook.
    """
    if isinstance(argument, str):
        argument = [argument]

    filelist = ContentList()
    basefolders = [path.fullpath for path in config['_songdir']] + list(
        files.iter_datadirs(config['_datadir'], 'latex'))
    for filename in argument:
        checked_file = None
        for path in basefolders:
            if os.path.exists(os.path.join(path, filename)):
                checked_file = os.path.relpath(os.path.join(
                    path,
                    filename,
                ))
                break
        if not checked_file:
            filelist.append_error(
                ContentError(
                    keyword="tex",
                    message=errors.notfound(filename, basefolders),
                ))
            continue
        filelist.append(LaTeX(checked_file))

    return filelist
Esempio n. 2
0
def parse(keyword, argument, contentlist, config):
    """Parse the contentlist.

    Arguments:
    - keyword: unused;
    - argument: unused;
    - contentlist: a list of name of tex files;
    - config: configuration dictionary of the current songbook.
    """
    if not contentlist:
        LOGGER.warning(
            "Useless 'tex' content: list of files to include is empty."
            )
    filelist = ContentList()
    basefolders = itertools.chain(
        (path.fullpath for path in config['_songdir']),
        files.iter_datadirs(config['datadir']),
        files.iter_datadirs(config['datadir'], 'latex'),
        )
    for filename in contentlist:
        checked_file = None
        for path in basefolders:
            if os.path.exists(os.path.join(path, filename)):
                checked_file = os.path.relpath(os.path.join(
                    path,
                    filename,
                    ))
                break
        if not checked_file:
            filelist.append_error(
                ContentError(
                    keyword="tex",
                    message=errors.notfound(filename, basefolders),
                    )
                )
            continue
        filelist.append(LaTeX(checked_file))

    return filelist
Esempio n. 3
0
def parse(keyword, argument, config):
    """Parse the tex files.

    Arguments:
    - keyword: unused;
    - argument:
        a list of tex files to include
        or a string of the tex file to include;
    - config: configuration dictionary of the current songbook.
    """
    if isinstance(argument, str):
        argument = [argument]

    filelist = ContentList()
    basefolders = itertools.chain(
        (path.fullpath for path in config['_songdir']),
        files.iter_datadirs(config['_datadir'], 'latex'),
        )
    for filename in argument:
        checked_file = None
        for path in basefolders:
            if os.path.exists(os.path.join(path, filename)):
                checked_file = os.path.relpath(os.path.join(
                    path,
                    filename,
                    ))
                break
        if not checked_file:
            filelist.append_error(
                ContentError(
                    keyword="tex",
                    message=errors.notfound(filename, basefolders),
                    )
                )
            continue
        filelist.append(LaTeX(checked_file))

    return filelist
Esempio n. 4
0
def parse(keyword, argument, config):
    """Parse data associated with keyword 'song'.

    Arguments:
    - keyword: unused;
    - argument: a list of strings, which are interpreted as regular
      expressions (interpreted using the glob module), referring to songs.
    - config: the current songbook configuration dictionary.

    Return a list of Song() instances.
    """
    contentlist = argument
    if isinstance(contentlist, str):
        contentlist = [contentlist]
    plugins = files.load_renderer_plugins(config['_datadir'])['tsg']
    if '_langs' not in config:
        config['_langs'] = set()
    songlist = ContentList()
    for songdir in config['_songdir']:
        if contentlist:
            break
        contentlist = files.recursive_find(songdir.fullpath, plugins.keys())
    if contentlist is None:
        contentlist = []  # No content was set or found
    for elem in contentlist:
        before = len(songlist)
        for songdir in config['_songdir']:
            if not os.path.isdir(songdir.datadir):
                continue
            with files.chdir(songdir.datadir):
                # Starting with Python 3.5 glob can be recursive: **/*.csg for instance
                # for filename in glob.iglob(os.path.join(songdir.subpath, elem), recursive=True):
                for filename in glob.iglob(os.path.join(songdir.subpath,
                                                        elem)):
                    LOGGER.debug('Parsing file "{}"…'.format(filename))
                    extension = filename.split(".")[-1]
                    if extension not in plugins:
                        LOGGER.info(
                            ('Cannot parse "%s": name does not end with one '
                             'of %s. Ignored.'),
                            os.path.join(songdir.datadir, filename),
                            ", ".join([
                                "'.{}'".format(key) for key in plugins.keys()
                            ]))
                        continue
                    try:
                        renderer = SongRenderer(plugins[extension](
                            filename,
                            config,
                            datadir=songdir.datadir,
                        ))
                    except ContentError as error:
                        songlist.append_error(error)
                        if config['_error'] == "failonsong":
                            raise errors.SongbookError(
                                "Error in song '{}'. Stopping as requested.".
                                format(os.path.join(songdir.fullpath,
                                                    filename)))
                        continue
                    if renderer.has_errors(
                    ) and config['_error'] == "failonsong":
                        raise errors.SongbookError(
                            "Error in song '{}'. Stopping as requested.".
                            format(os.path.join(songdir.fullpath, filename)))
                    songlist.append(renderer)
                    config["_langs"].add(renderer.song.lang)
            if len(songlist) > before:
                break
        if len(songlist) == before:
            # No songs were added
            LOGGER.warning(
                errors.notfound(
                    elem,
                    [item.fullpath for item in config['_songdir']],
                    message=
                    'Ignoring "{name}": did not match any file in {paths}.',
                ))
    return sorted(songlist)
Esempio n. 5
0
def parse(keyword, argument, contentlist, config):
    """Parse data associated with keyword 'song'.

    Arguments:
    - keyword: unused;
    - argument: unused;
    - contentlist: a list of strings, which are interpreted as regular
      expressions (interpreted using the glob module), referring to songs.
    - config: the current songbook configuration dictionary.

    Return a list of Song() instances.
    """
    plugins = config['_song_plugins']
    if '_langs' not in config:
        config['_langs'] = set()
    songlist = ContentList()
    for songdir in config['_songdir']:
        if contentlist:
            break
        contentlist = files.recursive_find(songdir.fullpath, plugins.keys())
    for elem in contentlist:
        before = len(songlist)
        for songdir in config['_songdir']:
            if not os.path.isdir(songdir.datadir):
                continue
            with files.chdir(songdir.datadir):
                # Starting with Python 3.5 glob can be recursive: **/*.csg for instance
                # for filename in glob.iglob(os.path.join(songdir.subpath, elem), recursive=True):
                for filename in glob.iglob(os.path.join(songdir.subpath, elem)):
                    LOGGER.debug('Parsing file "{}"…'.format(filename))
                    extension = filename.split(".")[-1]
                    if extension not in plugins:
                        LOGGER.info(
                            (
                                'Cannot parse "%s": name does not end with one '
                                'of %s. Ignored.'
                            ),
                            os.path.join(songdir.datadir, filename),
                            ", ".join(["'.{}'".format(key) for key in plugins.keys()])
                        )
                        continue
                    try:
                        renderer = SongRenderer(plugins[extension](
                            filename,
                            config,
                            datadir=songdir.datadir,
                            ))
                    except ContentError as error:
                        songlist.append_error(error)
                        if config['_error'] == "failonsong":
                            raise errors.SongbookError(
                                "Error in song '{}'. Stopping as requested."
                                .format(os.path.join(songdir.fullpath, filename))
                                )
                        continue
                    if renderer.has_errors() and config['_error'] == "failonsong":
                        raise errors.SongbookError(
                            "Error in song '{}'. Stopping as requested."
                            .format(os.path.join(songdir.fullpath, filename))
                            )
                    songlist.append(renderer)
                    config["_langs"].add(renderer.song.lang)
            if len(songlist) > before:
                break
        if len(songlist) == before:
            # No songs were added
            LOGGER.warning(errors.notfound(
                elem,
                [item.fullpath for item in config['_songdir']],
                message='Ignoring "{name}": did not match any file in {paths}.',
                ))
    return sorted(songlist)