Files @ cc69faa87c27
Branch filter:

Location: light9/src/light9/showconfig.py

drewp@bigasterisk.com
tear up and rewrite ascoltami to emit player state into the graph. web ui works but displays nothing but songs
import logging
import os
import warnings
from os import getenv
from pathlib import Path
from typing import cast

from rdfdb.syncedgraph.syncedgraph import SyncedGraph
from rdflib import Graph, Literal, URIRef
from twisted.python.filepath import FilePath

from .namespaces import L9

log = logging.getLogger('showconfig')

_config = None  # graph


def getGraph() -> Graph:
    warnings.warn("code that's using showconfig.getGraph should be "
                  "converted to use the sync graph", stacklevel=2)
    global _config
    if _config is None:
        graph = Graph()
        # note that logging is probably not configured the first time
        # we're in here
        warnings.warn("reading n3 files around %r" % root())
        for f in FilePath(root()).globChildren("*.n3") + FilePath(root()).globChildren("build/*.n3"):
            graph.parse(location=f.path, format='n3')
        _config = graph
    return _config


def root() -> bytes:
    r = getenv("LIGHT9_SHOW")
    if r is None:
        raise OSError("LIGHT9_SHOW env variable has not been set to the show root")
    return r.encode('ascii')


_showUri = None


def showUri() -> URIRef:
    """Return the show URI associated with $LIGHT9_SHOW."""
    global _showUri
    if _showUri is None:
        _showUri = URIRef(open(os.path.join(root(), b'URI')).read().strip())
    return _showUri


def songOnDisk(graph: SyncedGraph, song: URIRef) -> Path:
    """given a song URI, where's the on-disk file that mpd would read?"""
    root = graph.value(showUri(), L9['musicRoot'])
    if not root:
        raise ValueError("%s has no :musicRoot" % showUri())

    name = graph.value(song, L9['songFilename'])
    if not name:
        raise ValueError("Song %r has no :songFilename" % song)

    return (Path(cast(Literal, root).toPython()) / cast(Literal, name).toPython()).absolute()


def songFilenameFromURI(uri: URIRef) -> bytes:
    """
    'http://light9.bigasterisk.com/show/dance2007/song8' -> 'song8'

    everything that uses this should be deprecated for real URIs
    everywhere"""
    assert isinstance(uri, URIRef)
    return str(uri).split('/')[-1].encode('ascii')


def getSongsFromShow(graph: Graph, show: URIRef) -> list[URIRef]:
    playList = graph.value(show, L9['playList'])
    if not playList:
        raise ValueError("%r has no l9:playList" % show)
    # The patch in https://github.com/RDFLib/rdflib/issues/305 fixed a
    # serious bug here.
    songs = list(graph.items(playList))

    return cast(list[URIRef], songs)


def curvesDir():
    return os.path.join(root(), b"curves")


def subFile(subname):
    return os.path.join(root(), b"subs", subname)


def subsDir():
    return os.path.join(root(), b'subs')