annotate light9/showconfig.py @ 2187:ccdfdc8183ad

remove py2-style 'object' superclass
author drewp@bigasterisk.com
date Fri, 19 May 2023 21:14:01 -0700
parents 17bee25a20cb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1038
96faf94bc5f4 dead code
Drew Perttula <drewp@bigasterisk.com>
parents: 1028
diff changeset
1 import logging, warnings
832
231a8b3aedc3 showconfig still reads n3 files on its own for legacy code. reads anything in top dir, not just config.n3 and patch.n3
Drew Perttula <drewp@bigasterisk.com>
parents: 717
diff changeset
2 from twisted.python.filepath import FilePath
356
c6aabf5bd3bc big KC speedup from not reloading config.n3 constantly
Drew Perttula <drewp@bigasterisk.com>
parents: 334
diff changeset
3 from os import path, getenv
686
a301a0039c66 buildout and rdflib updates
Drew Perttula <drewp@bigasterisk.com>
parents: 653
diff changeset
4 from rdflib import Graph
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
5 from rdflib import URIRef, Literal
1866
3c523c71da29 pyflakes cleanups and some refactors
Drew Perttula <drewp@bigasterisk.com>
parents: 1865
diff changeset
6 from .namespaces import L9
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
7 from typing import List, cast
537
1929e0e1053e showconfig logs about what n3 files it's reading
drewp@bigasterisk.com
parents: 532
diff changeset
8 log = logging.getLogger('showconfig')
304
5f9cf6174e62 ascoltami now uses rdf for config
Drew Perttula <drewp@bigasterisk.com>
parents: 227
diff changeset
9
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
10 _config = None # graph
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
11
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
12
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
13 def getGraph() -> Graph:
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
14 warnings.warn(
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
15 "code that's using showconfig.getGraph should be "
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
16 "converted to use the sync graph",
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
17 stacklevel=2)
356
c6aabf5bd3bc big KC speedup from not reloading config.n3 constantly
Drew Perttula <drewp@bigasterisk.com>
parents: 334
diff changeset
18 global _config
832
231a8b3aedc3 showconfig still reads n3 files on its own for legacy code. reads anything in top dir, not just config.n3 and patch.n3
Drew Perttula <drewp@bigasterisk.com>
parents: 717
diff changeset
19 if _config is None:
231a8b3aedc3 showconfig still reads n3 files on its own for legacy code. reads anything in top dir, not just config.n3 and patch.n3
Drew Perttula <drewp@bigasterisk.com>
parents: 717
diff changeset
20 graph = Graph()
1038
96faf94bc5f4 dead code
Drew Perttula <drewp@bigasterisk.com>
parents: 1028
diff changeset
21 # note that logging is probably not configured the first time
96faf94bc5f4 dead code
Drew Perttula <drewp@bigasterisk.com>
parents: 1028
diff changeset
22 # we're in here
1215
0d295af23c4b new nginx router
Drew Perttula <drewp@bigasterisk.com>
parents: 1038
diff changeset
23 warnings.warn("reading n3 files around %r" % root())
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
24 for f in FilePath(root()).globChildren("*.n3") + FilePath(
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
25 root()).globChildren("build/*.n3"):
832
231a8b3aedc3 showconfig still reads n3 files on its own for legacy code. reads anything in top dir, not just config.n3 and patch.n3
Drew Perttula <drewp@bigasterisk.com>
parents: 717
diff changeset
26 graph.parse(location=f.path, format='n3')
231a8b3aedc3 showconfig still reads n3 files on its own for legacy code. reads anything in top dir, not just config.n3 and patch.n3
Drew Perttula <drewp@bigasterisk.com>
parents: 717
diff changeset
27 _config = graph
231a8b3aedc3 showconfig still reads n3 files on its own for legacy code. reads anything in top dir, not just config.n3 and patch.n3
Drew Perttula <drewp@bigasterisk.com>
parents: 717
diff changeset
28 return _config
219
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
29
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
30
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
31 def root() -> bytes:
219
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
32 r = getenv("LIGHT9_SHOW")
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
33 if r is None:
220
13c089886f61 added the forgotten curve.py; mpd song path fix
drewp@bigasterisk.com
parents: 219
diff changeset
34 raise OSError(
13c089886f61 added the forgotten curve.py; mpd song path fix
drewp@bigasterisk.com
parents: 219
diff changeset
35 "LIGHT9_SHOW env variable has not been set to the show root")
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
36 return r.encode('ascii')
219
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
37
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
38
1461
a7de122a7b18 clients were reading this file a LOT. Probably not a performance issue, but distracting in strace output
drewp@bigasterisk.com
parents: 1224
diff changeset
39 _showUri = None
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
40
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
41
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
42 def showUri() -> URIRef:
617
94039df5cdd9 create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
David McClosky <dmcc@bigasterisk.com>
parents: 537
diff changeset
43 """Return the show URI associated with $LIGHT9_SHOW."""
1461
a7de122a7b18 clients were reading this file a LOT. Probably not a performance issue, but distracting in strace output
drewp@bigasterisk.com
parents: 1224
diff changeset
44 global _showUri
a7de122a7b18 clients were reading this file a LOT. Probably not a performance issue, but distracting in strace output
drewp@bigasterisk.com
parents: 1224
diff changeset
45 if _showUri is None:
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
46 _showUri = URIRef(open(path.join(root(), b'URI')).read().strip())
1461
a7de122a7b18 clients were reading this file a LOT. Probably not a performance issue, but distracting in strace output
drewp@bigasterisk.com
parents: 1224
diff changeset
47 return _showUri
617
94039df5cdd9 create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
David McClosky <dmcc@bigasterisk.com>
parents: 537
diff changeset
48
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
49
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
50 def songOnDisk(song: URIRef) -> bytes:
423
2b2f5da47c5d config file now lists paths in the same form you'd give to mpd. New cmdline on ascoltami to specify the song list from config.n3
drewp@bigasterisk.com
parents: 420
diff changeset
51 """given a song URI, where's the on-disk file that mpd would read?"""
318
fcf9755d9a75 patch showconfig again
drewp@bigasterisk.com
parents: 317
diff changeset
52 graph = getGraph()
644
98ec0fbbe980 switch song config to factor out the music dir
Drew Perttula <drewp@bigasterisk.com>
parents: 638
diff changeset
53 root = graph.value(showUri(), L9['musicRoot'])
98ec0fbbe980 switch song config to factor out the music dir
Drew Perttula <drewp@bigasterisk.com>
parents: 638
diff changeset
54 if not root:
98ec0fbbe980 switch song config to factor out the music dir
Drew Perttula <drewp@bigasterisk.com>
parents: 638
diff changeset
55 raise ValueError("%s has no :musicRoot" % showUri())
1038
96faf94bc5f4 dead code
Drew Perttula <drewp@bigasterisk.com>
parents: 1028
diff changeset
56
644
98ec0fbbe980 switch song config to factor out the music dir
Drew Perttula <drewp@bigasterisk.com>
parents: 638
diff changeset
57 name = graph.value(song, L9['songFilename'])
98ec0fbbe980 switch song config to factor out the music dir
Drew Perttula <drewp@bigasterisk.com>
parents: 638
diff changeset
58 if not name:
98ec0fbbe980 switch song config to factor out the music dir
Drew Perttula <drewp@bigasterisk.com>
parents: 638
diff changeset
59 raise ValueError("Song %r has no :songFilename" % song)
98ec0fbbe980 switch song config to factor out the music dir
Drew Perttula <drewp@bigasterisk.com>
parents: 638
diff changeset
60
1883
17bee25a20cb reformat
Drew Perttula <drewp@bigasterisk.com>
parents: 1873
diff changeset
61 return path.abspath(
17bee25a20cb reformat
Drew Perttula <drewp@bigasterisk.com>
parents: 1873
diff changeset
62 path.join(
17bee25a20cb reformat
Drew Perttula <drewp@bigasterisk.com>
parents: 1873
diff changeset
63 cast(Literal, root).toPython(),
17bee25a20cb reformat
Drew Perttula <drewp@bigasterisk.com>
parents: 1873
diff changeset
64 cast(Literal, name).toPython()))
220
13c089886f61 added the forgotten curve.py; mpd song path fix
drewp@bigasterisk.com
parents: 219
diff changeset
65
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
66
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
67 def songFilenameFromURI(uri: URIRef) -> bytes:
357
7771f37252da curvecalc persistence, wavecurve -a option
Drew Perttula <drewp@bigasterisk.com>
parents: 356
diff changeset
68 """
7771f37252da curvecalc persistence, wavecurve -a option
Drew Perttula <drewp@bigasterisk.com>
parents: 356
diff changeset
69 'http://light9.bigasterisk.com/show/dance2007/song8' -> 'song8'
7771f37252da curvecalc persistence, wavecurve -a option
Drew Perttula <drewp@bigasterisk.com>
parents: 356
diff changeset
70
7771f37252da curvecalc persistence, wavecurve -a option
Drew Perttula <drewp@bigasterisk.com>
parents: 356
diff changeset
71 everything that uses this should be deprecated for real URIs
7771f37252da curvecalc persistence, wavecurve -a option
Drew Perttula <drewp@bigasterisk.com>
parents: 356
diff changeset
72 everywhere"""
7771f37252da curvecalc persistence, wavecurve -a option
Drew Perttula <drewp@bigasterisk.com>
parents: 356
diff changeset
73 assert isinstance(uri, URIRef)
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
74 return str(uri).split('/')[-1].encode('ascii')
357
7771f37252da curvecalc persistence, wavecurve -a option
Drew Perttula <drewp@bigasterisk.com>
parents: 356
diff changeset
75
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
76
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
77 def getSongsFromShow(graph: Graph, show: URIRef) -> List[URIRef]:
617
94039df5cdd9 create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
David McClosky <dmcc@bigasterisk.com>
parents: 537
diff changeset
78 playList = graph.value(show, L9['playList'])
94039df5cdd9 create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
David McClosky <dmcc@bigasterisk.com>
parents: 537
diff changeset
79 if not playList:
94039df5cdd9 create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
David McClosky <dmcc@bigasterisk.com>
parents: 537
diff changeset
80 raise ValueError("%r has no l9:playList" % show)
1224
42656c3b31f4 update jquery-ui, remove rdflib bnode workaround
Drew Perttula <drewp@bigasterisk.com>
parents: 1215
diff changeset
81 # The patch in https://github.com/RDFLib/rdflib/issues/305 fixed a
42656c3b31f4 update jquery-ui, remove rdflib bnode workaround
Drew Perttula <drewp@bigasterisk.com>
parents: 1215
diff changeset
82 # serious bug here.
42656c3b31f4 update jquery-ui, remove rdflib bnode workaround
Drew Perttula <drewp@bigasterisk.com>
parents: 1215
diff changeset
83 songs = list(graph.items(playList))
617
94039df5cdd9 create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
David McClosky <dmcc@bigasterisk.com>
parents: 537
diff changeset
84
94039df5cdd9 create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
David McClosky <dmcc@bigasterisk.com>
parents: 537
diff changeset
85 return songs
94039df5cdd9 create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
David McClosky <dmcc@bigasterisk.com>
parents: 537
diff changeset
86
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
87
219
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
88 def curvesDir():
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
89 return path.join(root(), b"curves")
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
90
219
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
91
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
92 def subFile(subname):
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
93 return path.join(root(), b"subs", subname)
1865
1aa91a31c0e2 reformat some missed files
Drew Perttula <drewp@bigasterisk.com>
parents: 1861
diff changeset
94
219
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
95
21b458127924 adding showconfig.py, which should have been added a while ago
drewp@bigasterisk.com
parents:
diff changeset
96 def subsDir():
1873
f001d689b3e2 more py3 and typing fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 1866
diff changeset
97 return path.join(root(), b'subs')