2022-12-06 22:17:53 -08:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
|
|
|
|
|
|
|
|
EDITOR_TEMPLATE = """
|
|
|
|
{name}:
|
|
|
|
description: {description}
|
|
|
|
entries:
|
2022-12-07 23:10:41 -08:00
|
|
|
{entries}
|
2022-12-06 22:17:53 -08:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Groove On Demand Playlist Editor
|
|
|
|
#
|
|
|
|
# This file is in YAML format. Blank lines and lines beginning with # are
|
2022-12-07 17:05:44 -08:00
|
|
|
# ignored, and the following structure is expected:
|
|
|
|
#
|
|
|
|
# PLAYLIST_TITLE:
|
|
|
|
# description: STRING_DESCRIPTION
|
|
|
|
# entries:
|
|
|
|
# - TRACK_ARTIST - TRACK_TITLE
|
|
|
|
# ...
|
|
|
|
#
|
|
|
|
# Here's a complete example, with a multi-line description:
|
2022-12-06 22:17:53 -08:00
|
|
|
#
|
|
|
|
# My Awesome Jams, Vol. 2:
|
|
|
|
# description: |
|
|
|
|
# These jams are totally awesome, yo.
|
|
|
|
# Totally.
|
|
|
|
#
|
|
|
|
# yo.
|
|
|
|
# entries:
|
2022-12-07 17:05:44 -08:00
|
|
|
# - Beastie Boys: Help Me, Rhonda
|
|
|
|
# - Bob and Doug McKenzie: Messiah (Hallelujah Eh)
|
|
|
|
#
|
|
|
|
# Tracks can be reordered or removed. You can also add a track, if the artist/title
|
|
|
|
# combination exactly matches precisely one Track entry your database. Searches are
|
|
|
|
# case-sensitive.
|
2022-12-06 22:17:53 -08:00
|
|
|
#
|
2022-12-07 17:05:44 -08:00
|
|
|
# Playlists can be renamed and descriptions can be updated or removed. If you rename a
|
|
|
|
# playlist, its slug will be regnenerated, breaking previous web links to said playlist.
|
2022-12-06 22:17:53 -08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class PlaylistEditor:
|
|
|
|
"""
|
|
|
|
A custom ConfigParser that only supports specific headers and ignores all other square brackets.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self._path = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
if not self._path:
|
|
|
|
self._path = NamedTemporaryFile(prefix='groove_on_demand-', delete=False)
|
|
|
|
return self._path
|
|
|
|
|
|
|
|
def edit(self, playlist):
|
|
|
|
with self.path as fh:
|
|
|
|
fh.write(playlist.as_yaml.encode())
|
|
|
|
subprocess.check_call([os.environ['EDITOR'], self.path.name])
|
|
|
|
edits = self.read()
|
|
|
|
self.cleanup()
|
|
|
|
return edits
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
with open(self.path.name, 'rb') as fh:
|
|
|
|
return yaml.safe_load(fh)
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
if self._path:
|
|
|
|
os.unlink(self._path.name)
|
|
|
|
self._path = None
|