Browse Source

UNifying loggerg (and adding option --verbose)

pull/27/head
Louis 11 years ago
parent
commit
d480550bab
  1. 21
      songbook
  2. 9
      songbook_core/build.py
  3. 1
      songbook_core/songs.py

21
songbook

@ -16,6 +16,9 @@ from songbook_core.build import SongbookBuilder, DEFAULT_STEPS
from songbook_core import __STR_VERSION__ from songbook_core import __STR_VERSION__
from songbook_core import errors from songbook_core import errors
# Logging configuration
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger()
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class ParseStepsAction(argparse.Action): 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): def argument_parser(args):
"""Parse argumnts""" """Parse argumnts"""
parser = argparse.ArgumentParser(description="A song book compiler") parser = argparse.ArgumentParser(description="A song book compiler")
@ -49,6 +56,11 @@ def argument_parser(args):
subdirectories are 'songs', 'img', 'latex', 'templates'. 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, parser.add_argument('--steps', '-s', nargs=1, type=str,
action=ParseStepsAction, action=ParseStepsAction,
help=textwrap.dedent("""\ help=textwrap.dedent("""\
@ -74,10 +86,6 @@ def argument_parser(args):
def main(): def main():
"""Main function:""" """Main function:"""
# Logging configuration
logging.basicConfig(name='songbook', level=logging.INFO)
logger = logging.getLogger(__name__)
# set script locale to match user's # set script locale to match user's
try: try:
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
@ -111,7 +119,10 @@ def main():
sb_builder.build_steps(options.steps) sb_builder.build_steps(options.steps)
except errors.SongbookError as error: 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(1)
sys.exit(0) sys.exit(0)

9
songbook_core/build.py

@ -220,6 +220,7 @@ class SongbookBuilder(object):
def build_tex(self): def build_tex(self):
"""Build .tex file from templates""" """Build .tex file from templates"""
LOGGER.info("Building '{}.tex'".format(self.basename))
with codecs.open( with codecs.open(
"{}.tex".format(self.basename), 'w', 'utf-8', "{}.tex".format(self.basename), 'w', 'utf-8',
) as output: ) as output:
@ -227,6 +228,7 @@ class SongbookBuilder(object):
def build_pdf(self): def build_pdf(self):
"""Build .pdf file from .tex file""" """Build .pdf file from .tex file"""
LOGGER.info("Building '{}.pdf'".format(self.basename))
self._run_once(self._set_latex) self._run_once(self._set_latex)
p = Popen( p = Popen(
["pdflatex"] + self._pdflatex_options + [self.basename], ["pdflatex"] + self._pdflatex_options + [self.basename],
@ -237,16 +239,17 @@ class SongbookBuilder(object):
while line: while line:
log += line log += line
line = p.stdout.readline() line = p.stdout.readline()
LOGGER.info(log) LOGGER.debug(log)
if p.returncode: if p.returncode:
raise errors.LatexCompilationError(self.basename) raise errors.LatexCompilationError(self.basename)
def build_sbx(self): def build_sbx(self):
"""Make index""" """Make index"""
LOGGER.info("Building indexes…")
sxd_files = glob.glob("%s_*.sxd" % self.basename) sxd_files = glob.glob("%s_*.sxd" % self.basename)
for sxd_file in sxd_files: for sxd_file in sxd_files:
LOGGER.info("processing " + sxd_file) LOGGER.debug("Processing " + sxd_file)
idx = process_sxd(sxd_file) idx = process_sxd(sxd_file)
with open(sxd_file[:-3] + "sbx", "w") as index_file: with open(sxd_file[:-3] + "sbx", "w") as index_file:
index_file.write(idx.entries_to_str().encode('utf8')) index_file.write(idx.entries_to_str().encode('utf8'))
@ -254,6 +257,7 @@ class SongbookBuilder(object):
@staticmethod @staticmethod
def build_custom(command): def build_custom(command):
"""Run a shell command""" """Run a shell command"""
LOGGER.info("Running '{}'".format(command))
exit_code = call(command, shell=True) exit_code = call(command, shell=True)
if exit_code: if exit_code:
raise errors.StepCommandError(command, 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 Depending of the LaTeX modules used in the template, there may be others
that are not deleted by this function.""" that are not deleted by this function."""
LOGGER.info("Cleaning…")
for ext in GENERATED_EXTENSIONS: for ext in GENERATED_EXTENSIONS:
if os.path.isfile(self.basename + ext): if os.path.isfile(self.basename + ext):
try: try:

1
songbook_core/songs.py

@ -103,6 +103,7 @@ class SongsList(object):
pour en extraire et traiter certaines information (titre, langue, pour en extraire et traiter certaines information (titre, langue,
album, etc.). album, etc.).
""" """
LOGGER.debug('Parsing file "{}"'.format(filename))
# Exécution de PlasTeX # Exécution de PlasTeX
data = parsetex(filename) data = parsetex(filename)

Loading…
Cancel
Save