Browse Source

Merge pull request #45 from patacrep/datadirs

Datadirs
pull/48/head
Luthaf 11 years ago
parent
commit
60d4a77fcb
  1. 29
      songbook
  2. 42
      songbook_core/build.py
  3. 14
      songbook_core/data/examples/example-all.sb
  4. 7
      songbook_core/data/templates/layout.tex
  5. 8
      songbook_core/data/templates/songs.tex
  6. 4
      songbook_core/plastex_songs.py
  7. 18
      songbook_core/songs.py
  8. 18
      songbook_core/templates.py

29
songbook

@ -51,7 +51,7 @@ def argument_parser(args):
Book to compile.
"""))
parser.add_argument('--datadir', '-d', nargs=1, type=str, action='store',
parser.add_argument('--datadir', '-d', nargs='+', type=str, action='append',
help=textwrap.dedent("""\
Data location. Expected (not necessarily required)
subdirectories are 'songs', 'img', 'latex', 'templates'.
@ -108,16 +108,23 @@ def main():
LOGGER.error("Error while loading file '{}'.".format(songbook_path))
sys.exit(1)
if options.datadir is not None:
songbook['datadir'] = options.datadir[0]
elif 'datadir' in songbook.keys():
if not os.path.isabs(songbook['datadir']):
songbook['datadir'] = os.path.join(os.path.dirname(songbook_path),
songbook['datadir']
)
else:
songbook['datadir'] = os.path.dirname(songbook_path)
# Gathering datadirs
datadirs = []
if options.datadir:
# Command line options
datadirs += [item[0] for item in options.datadir]
if 'datadir' in songbook:
# .sg file
if isinstance(songbook['datadir'], basestring):
songbook['datadir'] = [songbook['datadir']]
datadirs += [
os.path.join(os.path.dirname(songbook_path), path)
for path in songbook['datadir']
]
if not datadirs:
# Default value
datadirs = [os.path.dirname(songbook_path)]
songbook['datadir'] = datadirs
try:
sb_builder = SongbookBuilder(songbook, basename)

42
songbook_core/build.py

@ -57,9 +57,7 @@ class Songbook(object):
'lang': 'english',
'sort': [u"by", u"album", u"@title"],
'content': None,
'datadir': os.path.abspath('.'),
}
self.songslist = None
self._parse_raw(raw_songbook)
@staticmethod
@ -94,7 +92,7 @@ class Songbook(object):
are stored verbatim in self.config.
"""
self.config.update(raw_songbook)
self.config['datadir'] = os.path.abspath(self.config['datadir'])
self._set_datadir()
# Compute song list
if self.config['content'] is None:
@ -102,11 +100,11 @@ class Songbook(object):
"song",
os.path.relpath(
filename,
os.path.join(self.config['datadir'], 'songs'),
os.path.join(self.config['datadir'][0], 'songs'),
))
for filename
in recursive_find(
os.path.join(self.config['datadir'], 'songs'),
os.path.join(self.config['datadir'][0], 'songs'),
'*.sg',
)
]
@ -114,7 +112,7 @@ class Songbook(object):
content = self.config["content"]
self.config["content"] = []
for elem in content:
if isinstance(elem, str) or isinstance(elem, unicode):
if isinstance(elem, basestring):
self.config["content"].append(("song", elem))
elif isinstance(elem, list):
self.config["content"].append((elem[0], elem[1]))
@ -129,6 +127,25 @@ class Songbook(object):
if key not in self.config['authwords']:
self.config['authwords'][key] = value
def _set_datadir(self):
"""Set the default values for datadir"""
try:
if isinstance(self.config['datadir'], basestring):
self.config['datadir'] = [self.config['datadir']]
except KeyError: # No datadir in the raw_songbook
self.config['datadir'] = [os.path.abspath('.')]
abs_datadir = []
for path in self.config['datadir']:
if os.path.exists(path) and os.path.isdir(path):
abs_datadir.append(os.path.abspath(path))
else:
LOGGER.warning("Ignoring non-existent datadir '{}'.".format(path))
abs_datadir.append(__DATADIR__)
self.config['datadir'] = abs_datadir
def _parse_songs(self):
"""Parse content included in songbook."""
self.contentlist = SongbookContent(self.config['datadir'])
@ -186,18 +203,7 @@ class SongbookBuilder(object):
return self._called_functions[function]
def _set_latex(self):
"""Set TEXINPUTS and LaTeX options."""
if not 'TEXINPUTS' in os.environ.keys():
os.environ['TEXINPUTS'] = ''
os.environ['TEXINPUTS'] += os.pathsep + os.path.join(
__DATADIR__,
'latex',
)
os.environ['TEXINPUTS'] += os.pathsep + os.path.join(
self.songbook.config['datadir'],
'latex',
)
"""Set LaTeX options."""
if self.unsafe:
self._pdflatex_options.append("--shell-escape")
if not self.interactive:

14
songbook_core/data/examples/example-all.sb

@ -0,0 +1,14 @@
{
"bookoptions" : [
"importantdiagramonly",
"repeatchords",
"lilypond",
"pictures"
],
"booktype" : "chorded",
"lang" : "french",
"authwords" : {
"sep" : ["and", "et"]
},
"datadir" : "."
}

7
songbook_core/data/templates/layout.tex

@ -26,6 +26,13 @@
%%
%% Generated using Songbook <http://www.patacrep.com>
\makeatletter
\def\input@path{(* for dir in datadir *)
{((dir))/latex/} %
(* endfor *)
}
\makeatother
(* block documentclass *)
\documentclass{article}
(* endblock *)

8
songbook_core/data/templates/songs.tex

@ -48,7 +48,7 @@
"values": {"chorded": {"english": "With guitar chords", "french": "Avec accords de guitare" },
"lyric": {"english": "Lyrics only", "french": "Paroles uniquement"}
},
"default": {"default":"chorded"},
"default": {"default":"chorded"},
"mandatory": true
},
"lang": {"description": {"english": "Language", "french": "Langue"},
@ -77,7 +77,11 @@
\lang{((lang))}
\usepackage{graphicx}
\graphicspath{{((datadir))/img/}}
\graphicspath{(* for dir in datadir *)
{((dir))/img/} %
(* endfor *)
}
\makeatletter
\@ifpackageloaded{hyperref}{}{\newcommand{\phantomsection}{}}

4
songbook_core/plastex_songs.py

@ -56,9 +56,7 @@ class beginsong(plasTeX.Command): # pylint: disable=invalid-name,too-many-public
for (key, val) in self.attributes['args'].iteritems():
if isinstance(val, plasTeX.DOM.Element):
args[key] = process_unbr_spaces(val).textContent.encode('utf-8')
elif isinstance(val, unicode):
args[key] = val.encode('utf-8')
elif isinstance(val, str):
elif isinstance(val, basestring):
args[key] = val.encode('utf-8')
else:
args[key] = unicode(val)

18
songbook_core/songs.py

@ -90,11 +90,10 @@ def unprefixed_title(title, prefixes):
class SongbookContent(object):
"""Manipulation et traitement de liste de chansons"""
def __init__(self, library):
self._songdir = os.path.join(library, 'songs')
# Sorted list of the content
self.content = []
def __init__(self, datadirs):
self.songdirs = [os.path.join(d, 'songs')
for d in datadirs]
self.content = [] # Sorted list of the content
def append_song(self, filename):
"""Ajout d'une chanson à la liste
@ -124,12 +123,15 @@ class SongbookContent(object):
if type == "song":
# Add all the songs matching the regex
before = len(self.content)
for filename in glob.iglob(os.path.join(self._songdir, elem)):
self.append_song(filename)
for songdir in self.songdirs:
for filename in glob.iglob(os.path.join(songdir, elem)):
self.append_song(filename)
if len(self.content) > before:
break
if len(self.content) == before:
# No songs were added
LOGGER.warning(
"Expression '{}' did not match any file".format(regexp)
"Expression '{}' did not match any file".format(elem)
)
else:
self.append(type, elem)

18
songbook_core/templates.py

@ -68,25 +68,21 @@ def _escape_tex(value):
class TexRenderer(object):
"""Render a template to a LaTeX file."""
def __init__(self, template, datadir, lang):
def __init__(self, template, datadirs, lang):
'''Start a new jinja2 environment for .tex creation.
Arguments:
- template: name of the template to use.
- datadir: location of the data directory (which max contain
file <datadir>/templates/<template>).
- datadirs: list of locations of the data directory
(which may contain file <datadir>/templates/<template>).
- lang: main language of songbook.
'''
self.lang = lang
# Load templates in filesystem ...
loaders = [FileSystemLoader(os.path.join(datadir, 'templates'))
for datadir in datadirs]
self.texenv = Environment(
loader=ChoiceLoader([
FileSystemLoader(
os.path.join(datadir, 'templates')
),
PackageLoader(
'songbook_core', os.path.join('data', 'templates')
),
]),
loader=ChoiceLoader(loaders),
extensions=[VariablesExtension],
)
self.texenv.block_start_string = '(*'

Loading…
Cancel
Save