Browse Source

Code factorisation, and better error message when file not found.

pull/58/head
Louis 11 years ago
parent
commit
5ab5bfded4
  1. 7
      patacrep/content/include.py
  2. 10
      patacrep/content/song.py
  3. 11
      patacrep/content/tex.py
  4. 11
      patacrep/errors.py
  5. 12
      patacrep/templates.py

7
patacrep/content/include.py

@ -13,6 +13,7 @@ import logging
from patacrep.content import process_content, ContentError from patacrep.content import process_content, ContentError
from patacrep import encoding from patacrep import encoding
from patacrep import errors
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -26,8 +27,10 @@ def load_from_datadirs(path, config=None):
if os.path.exists(filepath): if os.path.exists(filepath):
return filepath return filepath
# File not found # File not found
raise ContentError("include", "The file '{0}' was not found in the " raise ContentError(
"datadirs.".format(path)) "include",
errors.notfound(path, config.get("datadir", [])),
)
#pylint: disable=unused-argument #pylint: disable=unused-argument
def parse(keyword, config, argument, contentlist): def parse(keyword, config, argument, contentlist):

10
patacrep/content/song.py

@ -9,7 +9,7 @@ import logging
import os import os
from patacrep.content import Content, process_content, ContentError from patacrep.content import Content, process_content, ContentError
from patacrep import files from patacrep import files, errors
from patacrep.songs import Song from patacrep.songs import Song
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -86,9 +86,11 @@ def parse(keyword, argument, contentlist, config):
break break
if len(songlist) == before: if len(songlist) == before:
# No songs were added # No songs were added
LOGGER.warning( LOGGER.warning(errors.notfound(
"Expression '{}' did not match any file".format(elem) elem,
) [item.fullpath for item in config['_songdir']],
message='Ignoring "{name}": did not match any file in {paths}.',
))
return songlist return songlist

11
patacrep/content/tex.py

@ -6,7 +6,7 @@
import logging import logging
import os import os
from patacrep import files from patacrep import files, errors
from patacrep.content import Content from patacrep.content import Content
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -51,13 +51,8 @@ def parse(keyword, argument, contentlist, config):
)) ))
break break
if not checked_file: if not checked_file:
LOGGER.warning( LOGGER.warning("{} Compilation may fail later.".format(
('Cannot find file "{}" in {}. Compilation may fail ' errors.notfound(filename, basefolders))
'later.').format(
filename,
", ".join(
['"{}"'.format(folder) for folder in basefolders]
))
) )
continue continue
filelist.append(LaTeX(checked_file)) filelist.append(LaTeX(checked_file))

11
patacrep/errors.py

@ -84,3 +84,14 @@ class UnknownStep(SongbookError):
def __str__(self): def __str__(self):
return """Compilation step "{step}" unknown.""".format(step=self.step) return """Compilation step "{step}" unknown.""".format(step=self.step)
def notfound(filename, paths, message=None):
"""Return a string saying that file was not found in paths."""
if message is None:
message = 'File "{name}" not found in directories {paths}.'
unique_paths = []
#pylint: disable=expression-not-assigned
[unique_paths.append(item) for item in paths if item not in unique_paths]
return message.format(
name=filename,
paths=", ".join(['"{}"'.format(item) for item in unique_paths]),
)

12
patacrep/templates.py

@ -100,10 +100,18 @@ class TexRenderer(object):
try: try:
self.template = self.texenv.get_template(template) self.template = self.texenv.get_template(template)
except TemplateNotFound as exception: except TemplateNotFound as exception:
# Only works if all loaders are FileSystemLoader().
paths = [
item
for loader in self.texenv.loader.loaders
for item in loader.searchpath
]
raise errors.TemplateError( raise errors.TemplateError(
exception, exception,
"""Template "{template}" not found.""".format( errors.notfound(
template=exception.name exception.name,
paths,
message='Template "{name}" not found in {paths}.'
), ),
) )

Loading…
Cancel
Save