From d480550bab994e423a3e7dde985816a80ade7e43 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 11 Apr 2014 21:52:16 +0200 Subject: [PATCH] UNifying loggerg (and adding option --verbose) --- songbook | 21 ++++++++++++++++----- songbook_core/build.py | 9 +++++++-- songbook_core/songs.py | 1 + 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/songbook b/songbook index 1dc98586..14b48f43 100755 --- a/songbook +++ b/songbook @@ -16,6 +16,9 @@ from songbook_core.build import SongbookBuilder, DEFAULT_STEPS from songbook_core import __STR_VERSION__ from songbook_core import errors +# Logging configuration +logging.basicConfig(level=logging.INFO) +LOGGER = logging.getLogger() # pylint: disable=too-few-public-methods class ParseStepsAction(argparse.Action): @@ -32,6 +35,10 @@ class ParseStepsAction(argparse.Action): ), ) +class VerboseAction(argparse.Action): + def __call__(self, *_args, **_kwargs): + LOGGER.setLevel(logging.DEBUG) + def argument_parser(args): """Parse argumnts""" parser = argparse.ArgumentParser(description="A song book compiler") @@ -49,6 +56,11 @@ def argument_parser(args): subdirectories are 'songs', 'img', 'latex', 'templates'. """)) + parser.add_argument('--verbose', '-v', nargs=0, action=VerboseAction, + help=textwrap.dedent("""\ + Show details about the compilation process. + """)) + parser.add_argument('--steps', '-s', nargs=1, type=str, action=ParseStepsAction, help=textwrap.dedent("""\ @@ -74,10 +86,6 @@ def argument_parser(args): def main(): """Main function:""" - # Logging configuration - logging.basicConfig(name='songbook', level=logging.INFO) - logger = logging.getLogger(__name__) - # set script locale to match user's try: locale.setlocale(locale.LC_ALL, '') @@ -111,7 +119,10 @@ def main(): sb_builder.build_steps(options.steps) except errors.SongbookError as error: - logger.error(error) + LOGGER.error(error) + LOGGER.error( + "Running again with option '-v' may give more information." + ) sys.exit(1) sys.exit(0) diff --git a/songbook_core/build.py b/songbook_core/build.py index 75e1d00e..c43b8780 100644 --- a/songbook_core/build.py +++ b/songbook_core/build.py @@ -220,6 +220,7 @@ class SongbookBuilder(object): def build_tex(self): """Build .tex file from templates""" + LOGGER.info("Building '{}.tex'…".format(self.basename)) with codecs.open( "{}.tex".format(self.basename), 'w', 'utf-8', ) as output: @@ -227,6 +228,7 @@ class SongbookBuilder(object): def build_pdf(self): """Build .pdf file from .tex file""" + LOGGER.info("Building '{}.pdf'…".format(self.basename)) self._run_once(self._set_latex) p = Popen( ["pdflatex"] + self._pdflatex_options + [self.basename], @@ -237,16 +239,17 @@ class SongbookBuilder(object): while line: log += line line = p.stdout.readline() - LOGGER.info(log) + LOGGER.debug(log) if p.returncode: raise errors.LatexCompilationError(self.basename) def build_sbx(self): """Make index""" + LOGGER.info("Building indexes…") sxd_files = glob.glob("%s_*.sxd" % self.basename) for sxd_file in sxd_files: - LOGGER.info("processing " + sxd_file) + LOGGER.debug("Processing " + sxd_file) idx = process_sxd(sxd_file) with open(sxd_file[:-3] + "sbx", "w") as index_file: index_file.write(idx.entries_to_str().encode('utf8')) @@ -254,6 +257,7 @@ class SongbookBuilder(object): @staticmethod def build_custom(command): """Run a shell command""" + LOGGER.info("Running '{}'…".format(command)) exit_code = call(command, shell=True) if exit_code: raise errors.StepCommandError(command, exit_code) @@ -263,6 +267,7 @@ class SongbookBuilder(object): Depending of the LaTeX modules used in the template, there may be others that are not deleted by this function.""" + LOGGER.info("Cleaning…") for ext in GENERATED_EXTENSIONS: if os.path.isfile(self.basename + ext): try: diff --git a/songbook_core/songs.py b/songbook_core/songs.py index 5f73e8ed..ccb9c7fc 100644 --- a/songbook_core/songs.py +++ b/songbook_core/songs.py @@ -103,6 +103,7 @@ class SongsList(object): pour en extraire et traiter certaines information (titre, langue, album, etc.). """ + LOGGER.debug('Parsing file "{}"…'.format(filename)) # Exécution de PlasTeX data = parsetex(filename)