Ejemplo n.º 1
0
def main(args=None):
    """Main function: run from command line."""
    if args is None:
        args = sys.argv
    if len(args) < 4:
        LOGGER.error("Invalid number of arguments.")
        LOGGER.error("Usage: %s", _usage())
        sys.exit(1)

    source = args[1]
    dest = args[2]
    song_files = args[3:]

    renderers = files.load_plugins(
        datadirs=DEFAULT_CONFIG.get('datadir', []),
        root_modules=['songs'],
        keyword='SONG_RENDERERS',
        )

    if dest not in renderers:
        LOGGER.error(
            "Unknown destination file format '%s'. Available ones are %s.",
            source,
            ", ".join(["'{}'".format(key) for key in renderers.keys()])
            )
        sys.exit(1)
    if source not in renderers[dest]:
        LOGGER.error(
            "Unknown source file format '%s'. Available ones are %s.",
            source,
            ", ".join(["'{}'".format(key) for key in renderers[dest].keys()])
            )
        sys.exit(1)

    for file in song_files:
        try:
            song = renderers[dest][source](file, DEFAULT_CONFIG)
            destname = "{}.{}".format(".".join(file.split(".")[:-1]), dest)
            if os.path.exists(destname):
                if not confirm(destname):
                    continue
            with open(destname, "w") as destfile:
                destfile.write(song.render())

        except ContentError:
            LOGGER.error("Cannot parse file '%s'.", file)
            sys.exit(1)
        except NotImplementedError:
            LOGGER.error("Cannot convert to format '%s'.", dest)
            sys.exit(1)
        except KeyboardInterrupt:
            print()
            LOGGER.info("Aborted by user.")
            sys.exit(0)

    sys.exit(0)
Ejemplo n.º 2
0
    def write_tex(self, output):
        """Build the '.tex' file corresponding to self.

        Arguments:
        - output: a file object, in which the file will be written.
        """
        # Updating configuration
        self._config = DEFAULT_CONFIG.copy()
        self._config.update(self._raw_config)
        renderer = TexBookRenderer(
            self._config["template"], self._config["datadir"], self._config["lang"], self._config["encoding"]
        )
        self._config.update(renderer.get_variables())
        self._config.update(self._raw_config)

        self._config["_compiled_authwords"] = authors.compile_authwords(copy.deepcopy(self._config["authwords"]))

        # Loading custom plugins
        self._config["_content_plugins"] = files.load_plugins(
            datadirs=self._config.get("datadir", []), root_modules=["content"], keyword="CONTENT_PLUGINS"
        )
        self._config["_song_plugins"] = files.load_plugins(
            datadirs=self._config.get("datadir", []), root_modules=["songs"], keyword="SONG_RENDERERS"
        )["tsg"]

        # Configuration set
        self._config["render"] = content.render
        self._config["content"] = content.process_content(self._config.get("content", []), self._config)
        self._config["filename"] = output.name[:-4]

        renderer.render_tex(output, self._config)

        # Get all errors, and maybe exit program
        self._errors.extend(renderer.errors)
        if self.config["_error"] == "failonbook":
            if self.has_errors():
                raise errors.SongbookError("Some songs contain errors. Stopping as requested.")
Ejemplo n.º 3
0
    def _generate_config(cls):
        """Generate the config to process the content"""

        config = DEFAULT_CONFIG.copy()

        datadirpaths = [os.path.join(os.path.dirname(__file__), 'datadir')]

        config['datadir'] = datadirpaths

        config['_songdir'] = [
            DataSubpath(path, 'songs')
            for path in datadirpaths
            ]
        config['_content_plugins'] = files.load_plugins(
            datadirs=datadirpaths,
            root_modules=['content'],
            keyword='CONTENT_PLUGINS',
            )
        config['_song_plugins'] = files.load_plugins(
            datadirs=datadirpaths,
            root_modules=['songs'],
            keyword='SONG_RENDERERS',
            )['tsg']
        cls.config = config