Browse Source

Unification du traitement des chemins relatifs

pull/47/head
Louis 10 years ago
parent
commit
710ffe5467
  1. 3
      songbook_core/content/__init__.py
  2. 2
      songbook_core/content/cwd.py
  3. 16
      songbook_core/content/song.py
  4. 6
      songbook_core/content/sorted.py
  5. 11
      songbook_core/content/tex.py
  6. 9
      songbook_core/files.py

3
songbook_core/content/__init__.py

@ -74,6 +74,7 @@ import logging
import os import os
import re import re
from songbook_core import files
from songbook_core.errors import SongbookError from songbook_core.errors import SongbookError
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -150,7 +151,7 @@ def load_plugins():
if key in plugins: if key in plugins:
LOGGER.warning( LOGGER.warning(
"File %s: Keyword '%s' is already used. Ignored.", "File %s: Keyword '%s' is already used. Ignored.",
os.path.relpath(name), files.relpath(name),
key, key,
) )
continue continue

2
songbook_core/content/cwd.py

@ -28,7 +28,7 @@ def parse(keyword, config, argument, contentlist):
""" """
old_songdir = config['_songdir'] old_songdir = config['_songdir']
config['_songdir'] = ( config['_songdir'] = (
[os.path.relpath(argument)] + [argument] +
[os.path.join(path, argument) for path in config['_songdir']] + [os.path.join(path, argument) for path in config['_songdir']] +
config['_songdir'] config['_songdir']
) )

16
songbook_core/content/song.py

@ -9,7 +9,7 @@ import logging
import os import os
from songbook_core.content import Content, process_content, ContentError 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 from songbook_core.songs import Song
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -34,12 +34,10 @@ class SongRenderer(Content, Song):
def render(self, context): def render(self, context):
"""Return the string that will render the song.""" """Return the string that will render the song."""
outdir = os.path.dirname(context['filename']) return r'\input{{{}}}'.format(files.relpath(
if os.path.abspath(self.path).startswith(os.path.abspath(outdir)): self.path,
path = os.path.relpath(self.path, outdir) os.path.dirname(context['filename'])
else: ))
path = os.path.abspath(self.path)
return r'\input{{{}}}'.format(path)
#pylint: disable=unused-argument #pylint: disable=unused-argument
def parse(keyword, argument, contentlist, config): def parse(keyword, argument, contentlist, config):
@ -61,9 +59,9 @@ def parse(keyword, argument, contentlist, config):
if contentlist: if contentlist:
break break
contentlist = [ contentlist = [
os.path.relpath(filename, songdir) files.relpath(filename, songdir)
for filename for filename
in recursive_find(songdir, "*.sg") in files.recursive_find(songdir, "*.sg")
] ]
for elem in contentlist: for elem in contentlist:
before = len(songlist) before = len(songlist)

6
songbook_core/content/sorted.py

@ -9,8 +9,8 @@ to a songbook.
import locale import locale
import logging import logging
import os
from songbook_core import files
from songbook_core.content import ContentError from songbook_core.content import ContentError
from songbook_core.content.song import OnlySongsError, process_songs from songbook_core.content.song import OnlySongsError, process_songs
@ -57,9 +57,9 @@ def key_generator(sort):
field = song.args[key] field = song.args[key]
except KeyError: except KeyError:
LOGGER.debug( LOGGER.debug(
"Ignoring non-existent key '{}' for song {}.".format( "Ignoring unknown key '{}' for song {}.".format(
key, key,
os.path.relpath(song.path), files.relpath(song.path),
) )
) )
field = "" field = ""

11
songbook_core/content/tex.py

@ -6,6 +6,7 @@
import logging import logging
import os import os
from songbook_core import files
from songbook_core.content import Content from songbook_core.content import Content
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -17,12 +18,10 @@ class LaTeX(Content):
self.filename = filename self.filename = filename
def render(self, context): def render(self, context):
outdir = os.path.dirname(context['filename']) return r'\input{{{}}}'.format(files.relpath(
if os.path.abspath(self.filename).startswith(os.path.abspath(outdir)): self.filename,
filename = os.path.relpath(self.filename, outdir) os.path.dirname(context['filename']),
else: ))
filename = os.path.abspath(self.filename)
return r'\input{{{}}}'.format(filename)
#pylint: disable=unused-argument #pylint: disable=unused-argument
def parse(keyword, argument, contentlist, config): def parse(keyword, argument, contentlist, config):

9
songbook_core/files.py

@ -17,3 +17,12 @@ def recursive_find(root_directory, pattern):
for filename in fnmatch.filter(filenames, pattern): for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename)) matches.append(os.path.join(root, filename))
return matches 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)

Loading…
Cancel
Save