Skip to content

Commit

Permalink
Improve song display
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverpool committed Sep 8, 2015
1 parent 6f2a68f commit a1c00d7
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 56 deletions.
1 change: 1 addition & 0 deletions generator/management/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
def import_song(filepath, song_directory):
'''Import a song in the database'''
with open(filepath) as song:
#TODO: PROBABLY BROKEN
data = parse_song(song.read(), filepath)
LOGGER.info("Processing " +
pprint.pformat(data['@titles'][0]))
Expand Down
25 changes: 1 addition & 24 deletions generator/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,7 @@ class Meta:
ordering = ["title"]

def content(self):
# TODO: correct parsing
path = os.path.join(SONGS_LIBRARY_DIR, 'songs', self.file_path)
return parse_song(path)

def chords(self):
# TODO: correct parsing and rendering
return [Chord(), Chord()] * 3

def album_name(self):
# TODO: correct parsing
return "En attendant les caravanes"

@cached_property
def album_cover_url(self):
# TODO: correct parsing
from random import randint
if randint(0,1):
return "http://loic-lantoine.wifeo.com/images/e/ena/En-Attendant-Les-Caravanes-rueketcaravanes.jpg"
else:
return ""

def website_url(self):
# TODO: correct parsing
return "http://www.sinsemilia.com/"
return parse_song(self.file_path)

###############################################################

Expand Down
97 changes: 93 additions & 4 deletions generator/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,100 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
Functions for song file (.sg) rendering.
Functions for song file (.sgc) rendering.
"""


from patanet.settings import SONGS_LIBRARY_DIR


from patacrep.songs.chordpro import ChordproSong
from patacrep.build import DEFAULT_CONFIG
from patacrep.songs.chordpro import ChordproSong

from pathlib import PurePosixPath

import os

from django.conf import settings


def parse_song(filename):
"""Parse song 'filename', and return the corresponding HTML code."""
# TODO
with open(filename) as fd:
return fd.read()
# TODO: Clean this file

# Hack to read the .sgc file
filename += "c"

relpath = os.path.join('songs', filename)

datadir = settings.SONGS_LIBRARY_DIR
config = DEFAULT_CONFIG.copy()
song_model = ChordproSong(datadir, relpath, config)
song_model.parse(config)
failed = song_model.titles == []

This comment has been minimized.

Copy link
@paternal

paternal Sep 17, 2015

Contributor

Je reconnais que c'est bizare, mais rien n'interdit dans les non-spécifications de Chordpro d'avoir une chanson sans titre, non ?

This comment has been minimized.

Copy link
@oliverpool

oliverpool Sep 17, 2015

Author Contributor

Effectivement: je propose donc de l'interdire ^^

Plus sérieusement, c'est juste un hack (assez sale) pour savoir si le parsing a échoué ou non (et l'afficher dans la page HTML: https://github.com/patacrep/patanet/blob/song_edition/generator/templates/generator/show_song.html#L38).



metadata = song_model.data

basedir = os.path.dirname(song_model.fullpath)

song = {
'album': get_data(metadata, 'album'),
'cover_url': get_cover_url(metadata, basedir, datadir),
'url': get_data(metadata, 'url'),
'chords': get_chords(metadata),
'fullpath': song_model.fullpath,
'capo': None, #Not implemented by patacrep yet
'languages': song_model.languages,
}


if failed:
song['failed'] = True

with open(song['fullpath']) as fd:
song['full_content'] = fd.read()
return song

def get_data(metadata, key, default=None):
if key in metadata:
return metadata[key]
return default

def get_chords(metadata):
raw_chords = get_data(metadata, 'define', [])
chords = []

def string_pos(strings):
if not strings:
return ''
# Need a fix on the JS lib to join with a space
return ''.join([str(pos) if pos else 'x' for pos in strings])

for raw_chord in raw_chords:
chord = {
'key': raw_chord.key.chord,
'basefret': raw_chord.basefret if raw_chord.basefret else 0,
'frets': string_pos(raw_chord.frets),
'fingers': string_pos(raw_chord.fingers),
}
chords.append(chord)
return chords

def get_cover_path(file_without_ext):
exts = ['', '.jpg', '.png']
for ext in exts:
print(file_without_ext + ext)
if os.path.isfile(file_without_ext + ext):
return file_without_ext + ext
raise FileNotFoundError()

def get_cover_url(metadata, basedir, datadir):
cov = get_data(metadata, 'cov')
if not cov:
return None
cover_without_ext = os.path.join(basedir, str(cov))
coverfile = get_cover_path(cover_without_ext)
relfile = str(PurePosixPath(coverfile).relative_to(datadir))
return relfile
54 changes: 35 additions & 19 deletions generator/templates/generator/show_song.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,62 @@
</form>
{% endif %}

{% comment %}
{% get_language_info for song.language as lang %}
<p class="infos">en {{lang.name_local}}<br />
{% if song.capo != None %}
Utiliser un capodastre en position {{ song.capo }}.
{% else %}
Pas de capo pour ce chant
{% endif %}
</p>
{% endcomment %}
{% if user.is_authenticated %}
{% if content.failed %}
<strong style="color:red;font-size:1.5em">
The parsing of the song failed!
</strong>

{% endif %}
<div class="song_information">
<ul>
{% if song.album_cover_url or song.album_name %}
{% if content.cover_url or content.album %}
<li class="cover">
{% if song.album_cover_url %}
<img src="{{ song.album_cover_url }}">
{% if content.cover_url %}
<img src="{% static content.cover_url %}">
{% endif %}
{% if song.album_name %}
{% if content.album %}
<span>
{% blocktrans with album_name=song.album_name %}
{% blocktrans with album_name=content.album %}
Album: <em>{{ album_name }}</em>
{% endblocktrans %}
<span>
{% endif %}
</li>
{% endif %}
{% if song.website_url %}
{% if content.url %}
<li>
<a href="{{ content.url }}" class="song_url">{{ content.url }}</a>
</li>
{% endif %}

{% if content.languages %}
<li>
<a href="{{ song.website_url }}" class="song_url">{{ song.website_url }}</a>
Languages:
{% for language in content.languages %}
{{language}}
{% endfor %}
{% comment %}
get_language_info works only with 'fr' (not 'french')
{% get_language_info for language as lang %}
<p class="infos">en {{lang.name_local}}<br />
</p>
{% endcomment %}
</li>
{% endif %}
</ul>
</div>
{% if content.capo != None %}
{% blocktrans with capo=content.capo %}
Utiliser un capodastre en position {{ capo }}.
{% endblocktrans %}
{% endif %}
<div class="song_chords">
{% for chord in song.chords %}
{% for chord in content.chords %}
{% include 'generator/widgets/chord.html' %}
{% endfor %}
</div>
<div class="song_content"><pre>{{ song.content }}</pre></div>
<div class="song_content"><pre>{{ content.full_content }}</pre></div>
{% endif %}

{% endblock %}
Expand Down
10 changes: 5 additions & 5 deletions generator/templates/generator/widgets/chord.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div
class="chord-diagram"
data-shift="{{ chord.shift }}"
data-frets="{{ chord.frets_str }}"
{% if chord.fingers_str %}
data-fingers="{{ chord.fingers_str }}"
data-shift="{{ chord.basefret }}"
data-frets="{{ chord.frets }}"
{% if chord.fingers %}
data-fingers="{{ chord.fingers }}"
{% endif %}
data-name="{{ chord.name }}"
data-name="{{ chord.key }}"
>
</div>
5 changes: 1 addition & 4 deletions generator/views/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
from generator.views.utils import LetterListView


from patanet.settings import SONGS_LIBRARY_DIR

class SongList(CurrentSongbookMixin, LetterListView):
model = Song
context_object_name = "song_list"
Expand All @@ -51,8 +49,7 @@ class ArtistView(CurrentSongbookMixin, DetailView):


def _read_song(song):
path = os.path.join(SONGS_LIBRARY_DIR, 'songs', song.file_path)
return parse_song(path)
return parse_song(song.file_path)


class SongView(CurrentSongbookMixin, DetailView):
Expand Down

0 comments on commit a1c00d7

Please sign in to comment.