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. 13
      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 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

2
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']
)

16
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)

6
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 = ""

13
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))

9
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)

Loading…
Cancel
Save