From 710ffe5467f1e6f20f099f07ec3d2716ca7d8352 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 17 Jun 2014 10:12:45 +0200 Subject: [PATCH] Unification du traitement des chemins relatifs --- songbook_core/content/__init__.py | 3 ++- songbook_core/content/cwd.py | 2 +- songbook_core/content/song.py | 16 +++++++--------- songbook_core/content/sorted.py | 6 +++--- songbook_core/content/tex.py | 13 ++++++------- songbook_core/files.py | 9 +++++++++ 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/songbook_core/content/__init__.py b/songbook_core/content/__init__.py index 18757f73..6096c6dc 100644 --- a/songbook_core/content/__init__.py +++ b/songbook_core/content/__init__.py @@ -74,6 +74,7 @@ import logging import os import re +from songbook_core import files from songbook_core.errors import SongbookError LOGGER = logging.getLogger(__name__) @@ -150,7 +151,7 @@ def load_plugins(): if key in plugins: LOGGER.warning( "File %s: Keyword '%s' is already used. Ignored.", - os.path.relpath(name), + files.relpath(name), key, ) continue diff --git a/songbook_core/content/cwd.py b/songbook_core/content/cwd.py index e5b40862..c1d3cc36 100644 --- a/songbook_core/content/cwd.py +++ b/songbook_core/content/cwd.py @@ -28,7 +28,7 @@ def parse(keyword, config, argument, contentlist): """ old_songdir = config['_songdir'] config['_songdir'] = ( - [os.path.relpath(argument)] + + [argument] + [os.path.join(path, argument) for path in config['_songdir']] + config['_songdir'] ) diff --git a/songbook_core/content/song.py b/songbook_core/content/song.py index 885aba6d..432efdad 100644 --- a/songbook_core/content/song.py +++ b/songbook_core/content/song.py @@ -9,7 +9,7 @@ import logging import os from songbook_core.content import Content, process_content, ContentError -from songbook_core.files import recursive_find +from songbook_core import files from songbook_core.songs import Song LOGGER = logging.getLogger(__name__) @@ -34,12 +34,10 @@ class SongRenderer(Content, Song): def render(self, context): """Return the string that will render the song.""" - outdir = os.path.dirname(context['filename']) - if os.path.abspath(self.path).startswith(os.path.abspath(outdir)): - path = os.path.relpath(self.path, outdir) - else: - path = os.path.abspath(self.path) - return r'\input{{{}}}'.format(path) + return r'\input{{{}}}'.format(files.relpath( + self.path, + os.path.dirname(context['filename']) + )) #pylint: disable=unused-argument def parse(keyword, argument, contentlist, config): @@ -61,9 +59,9 @@ def parse(keyword, argument, contentlist, config): if contentlist: break contentlist = [ - os.path.relpath(filename, songdir) + files.relpath(filename, songdir) for filename - in recursive_find(songdir, "*.sg") + in files.recursive_find(songdir, "*.sg") ] for elem in contentlist: before = len(songlist) diff --git a/songbook_core/content/sorted.py b/songbook_core/content/sorted.py index 4873cb3c..61a7499f 100644 --- a/songbook_core/content/sorted.py +++ b/songbook_core/content/sorted.py @@ -9,8 +9,8 @@ to a songbook. import locale import logging -import os +from songbook_core import files from songbook_core.content import ContentError from songbook_core.content.song import OnlySongsError, process_songs @@ -57,9 +57,9 @@ def key_generator(sort): field = song.args[key] except KeyError: LOGGER.debug( - "Ignoring non-existent key '{}' for song {}.".format( + "Ignoring unknown key '{}' for song {}.".format( key, - os.path.relpath(song.path), + files.relpath(song.path), ) ) field = "" diff --git a/songbook_core/content/tex.py b/songbook_core/content/tex.py index a5b5f2da..a1c6ec41 100644 --- a/songbook_core/content/tex.py +++ b/songbook_core/content/tex.py @@ -6,6 +6,7 @@ import logging import os +from songbook_core import files from songbook_core.content import Content LOGGER = logging.getLogger(__name__) @@ -17,12 +18,10 @@ class LaTeX(Content): self.filename = filename def render(self, context): - outdir = os.path.dirname(context['filename']) - if os.path.abspath(self.filename).startswith(os.path.abspath(outdir)): - filename = os.path.relpath(self.filename, outdir) - else: - filename = os.path.abspath(self.filename) - return r'\input{{{}}}'.format(filename) + return r'\input{{{}}}'.format(files.relpath( + self.filename, + os.path.dirname(context['filename']), + )) #pylint: disable=unused-argument def parse(keyword, argument, contentlist, config): @@ -48,7 +47,7 @@ def parse(keyword, argument, contentlist, config): if not checked_file: LOGGER.warning( ("Cannot find file '{}' in '{}'. Compilation may fail " - "later.").format( filename, str(config['_songdir'])) + "later.").format(filename, str(config['_songdir'])) ) continue filelist.append(LaTeX(checked_file)) diff --git a/songbook_core/files.py b/songbook_core/files.py index 65acc153..31cc4e2e 100644 --- a/songbook_core/files.py +++ b/songbook_core/files.py @@ -17,3 +17,12 @@ def recursive_find(root_directory, pattern): for filename in fnmatch.filter(filenames, pattern): matches.append(os.path.join(root, filename)) return matches + +def relpath(path, start=None): + """Return relative filepath to path if a subpath of start.""" + if start is None: + start = os.curdir + if os.path.abspath(path).startswith(os.path.abspath(start)): + return os.path.relpath(path, start) + else: + return os.path.abspath(path)