Example #1
0
def main():
    options = argument_parser(sys.argv[1:])
    
    datadir = os.path.abspath(str(options.datadir[0]))

    if not os.path.isdir(datadir):
        print("Error: '{}' is not a directory.".format(datadir))
        sys.exit(1)
    
    if not options.output:
        output = os.path.basename(datadir) + ".sbc"
    else:
        output = str(options.output)
    
    songdir = os.path.join(datadir, "songs")
    
    if not os.path.isdir(songdir):
        print("Error: no songs directory in '{}'.".format(datadir))
        sys.exit(1)
    
    songs = recursive_find(songdir, "*.sg")
    
    songspath = []
    
    for path in songs:
        songspath.append(relpath(path, datadir))

    with open(output, "w") as out:
        json.dump(songspath, out, indent=4)
Example #2
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 SongRenderer() instances.
    """
    if 'languages' not in config:
        config['_languages'] = set()
    songlist = []
    for songdir in config['_songdir']:
        if contentlist:
            break
        contentlist = [
                files.relpath(filename, songdir)
                for filename
                in files.recursive_find(songdir, "*.sg")
                ]
    for elem in contentlist:
        before = len(songlist)
        for songdir in config['_songdir']:
            for filename in glob.iglob(os.path.join(songdir, elem)):
                LOGGER.debug('Parsing file "{}"…'.format(filename))
                song = SongRenderer(filename, config)
                songlist.append(song)
                config["_languages"].update(song.languages)
            if len(songlist) > before:
                break
        if len(songlist) == before:
            # No songs were added
            LOGGER.warning(
                    "Expression '{}' did not match any file".format(elem)
                    )
    return songlist
Example #3
0
$ python setup.py install
"""
from patacrep import __version__

from setuptools import setup, find_packages
import sys

setup_kwargs = {
    'setup_requires': [],
    }

if sys.platform[0:3] == 'win':
    from patacrep import __DATADIR__, files

    # List the data files
    data_files = files.recursive_find(__DATADIR__)
    data_files = ["data/" + d for d in data_files]
    setup_kwargs['package_data'] = {'patacrep': data_files}
else:
    setup_kwargs['setup_requires'].append('hgtools')
    setup_kwargs['include_package_data'] = True

setup(
    name='patacrep',
    version=__version__,
    description='Songbook compilation chain',
    author='The Songbook team',
    author_email='*****@*****.**',
    url='https://github.com/patacrep/patacrep',
    packages=find_packages(exclude=["test*"]),
    license="GPLv2 or any later version",
Example #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)
Example #5
0
$ python setup.py install
"""
from patacrep import __version__

from setuptools import setup, find_packages
import sys

setup_kwargs = {
    'setup_requires': [],
}

if sys.platform[0:3] == 'win':
    from patacrep import __DATADIR__, files

    # List the data files
    data_files = files.recursive_find(__DATADIR__)
    data_files = ["data/" + d for d in data_files]
    setup_kwargs['package_data'] = {'patacrep': data_files}
else:
    setup_kwargs['setup_requires'].append('hgtools')
    setup_kwargs['include_package_data'] = True

setup(
    name='patacrep',
    version=__version__,
    description='Songbook compilation chain',
    author='The Songbook team',
    author_email='*****@*****.**',
    url='https://github.com/patacrep/patacrep',
    packages=find_packages(exclude=["test*"]),
    license="GPLv2 or any later version",
Example #6
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)