-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_scene.py
269 lines (254 loc) · 8.82 KB
/
class_scene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
import os
import shutil
import webbrowser
from collections import defaultdict
from datetime import datetime
from random import choice, randint, random, sample, seed, uniform
import pdfkit
import dominate
from dominate.tags import (
caption,
div,
h1,
h2,
h3,
h4,
h5,
h6,
link,
p,
span,
style,
table,
tbody,
td,
th,
thead,
tr,
)
from graphviz import Digraph, nohtml
from tqdm import tqdm
from class_label import label
from class_person import person
from class_wordList import wordList
yearNow = datetime.today().year
class scene(object):
def __init__(
self,
seedInt,
numLabels,
yearStart,
viewGv,
numArtists,
numIncarnations,
numAlbums,
numTracks,
viewLog,
):
self.seed = seedInt
seed(self.seed)
self.yearNow = datetime.today().year
self.viewLog = viewLog
self.years = []
self.labels = []
self.yearStart = yearStart
self.labelPeople = [] # people collected from the labels
self.wordList = wordList()
self.numLabels = randint(1, 6) if numLabels == 0 else randint(1, numLabels)
self.numArtists = numArtists
self.numAlbums = numAlbums
self.numTracks = numTracks
self.numIncarnations = numIncarnations
self.people = self.scenePeople(randint(40, 100))
self.pathName = f"Scene_{self.seed}"
self.path = "..\\seeded_tunes_output\\" + self.pathName.replace(" ", "_")
if os.path.exists(self.path):
shutil.rmtree(self.path, ignore_errors=True)
os.mkdir(self.path)
# shutil.rmtree(self.path, ignore_errors=True)
self.initGraph()
# add labels
for num in tqdm(
range(1, self.numLabels + 1), desc="02 Labels".ljust(18), position=2
):
self.labels.append(label(self, num))
self.labelPeople += self.labels[num - 1].people
# find first and last years
self.yearFirst = min(a.yearFirst for a in self.labels)
self.yearLast = max(a.yearLast for a in self.labels)
self.people = list(set(self.labelPeople))
self.name = f'The "{self.seed}" Music Scene of {self.yearFirst}'
if self.yearFirst == self.yearLast:
self.name += " CE"
else:
self.name += f" to {self.yearLast} CE"
self.initHTML()
self.logScene()
self.graphScene()
self.graphPeopleLinks()
if viewGv==True:
self.gvGraph.render(self.gvGraph.filename, view=True)
else:
self.gvGraph.render(self.gvGraph.filename, view=False)
def scenePeople(self, numPeople):
""" Returns list of people """
p = []
for _ in tqdm(range(numPeople), desc="01 Scene People".ljust(18), position=1):
p.append(person())
return p
def __str__(self):
""" prints Scene info """
return f"Scene: {self.name}\n # of Labels: {self.numLabels}"
def initGraph(self):
""" Initialise GraphViz graph """
# gvFont = "Oswald"
gvFont = "Arial"
self.gvGraph = Digraph(
name=self.pathName,
filename=f"{self.path}\\{self.seed}_{self.pathName}_Family_Tree.gv",
format="svg",
)
self.gvGraph.graph_attr.update(
# fontname=gvFont,
fontname=gvFont,
fontsize="14",
nodesep="0",
splines="ortho",
rankdir="TB",
compound="true",
labelloc="top",
forcelabels="true",
)
self.gvGraph.node_attr.update(
margin="0",
# height="0.3",
fontname=gvFont,
style="filled",
color="black",
fillcolor="grey95",
shape="plaintext",
fontsize="10",
# labeljust="l",
)
self.gvGraph.edge_attr.update(arrowhead="none", fontname=gvFont, fontsize="6")
def initHTML(self):
self.doc = dominate.document(title=f"Scene Log for {self.name}")
self.docFileBase = f"Scene_{self.seed}_Log"
self.docPathHtml = f"{self.path}\\{self.docFileBase}.html"
self.docPathPdf = f"{self.path}\\{self.docFileBase}.pdf"
with self.doc.head:
style(
"""
body { font-family: Arial Nova Condensed, FreeSans, sans-serif; margin: 3em 1em; }
p { font-size: 1em; }
div#label { margin-left: 2%;}
div#artist { margin-left: 4%; }
div#incarn { margin-left: 6%; }
div#album { margin-left: 8%; }
div#format { margin-left: 10%; }
h1 { font-size: 3em; font-weight: bold; }
h2 { font-size: 2.6em; font-weight: bold; }
h3 { font-size: 2.2em; font-weight: bold; }
h4 { font-size: 1.8em; font-weight: bold; }
h5 { font-size: 1.4em; font-weight: bold; }
h6 { font-size: 1em; font-weight: bold; }
table { border-collapse: collapse; }
th { border-bottom: 1px solid black; }
table#albumTrackTable tbody tr td:nth-child(even) { background: #bababa }
table#albumFormatTable tbody tr td:nth-child(even) { background: #bababa }
"""
)
def graphPeopleLinks(self):
for p in self.people:
for _ in p.incarnList:
return
# return
def logScene(self):
strWidth = 18
d = self.doc
d += h1(self.name)
for label in tqdm(
self.labels, desc="12 Log Labels".ljust(strWidth), position=12
):
d += label.html()
# loop through artists
for artist in tqdm(
label.artists, desc="13 Log Artists".ljust(strWidth), position=13
):
d += artist.html()
# loop through incarnations
for incarn in tqdm(
artist.incarnations,
desc="14 Log Incarns".ljust(strWidth),
position=14,
):
d += incarn.html()
# loop through albums
# TODO include artwork in log
for album in tqdm(
incarn.albums, desc="15 Log Albums".ljust(strWidth), position=15
):
d += album.html()
with open(self.docPathHtml, "w", encoding="utf8") as file:
file.write(self.doc.render())
pdfkit.from_file(
self.docPathHtml,
self.docPathPdf,
options={"log-level": "info", "page-size": "A4",},
)
if self.viewLog:
os.startfile(self.docPathHtml)
os.startfile(self.docPathPdf)
else:
pass
def graphScene(self):
""" Scene GraphViz, including artists and albums """
self.gvGraph.attr(
label=self.name,
labelloc="top",
# fontsize="16",
# fontweight="bold",
# compound="true",
)
# self.gvGraph.subgraph(graph_attr['rank']='same')
self.albums = []
self.incarnsAll = []
for (
l
) in (
self.labels
): # assemble list of release years across all labels. AND list of incarnations
for ar in l.artists:
self.incarnsAll += ar.incarnations
for incarn in ar.incarnations:
self.albums += incarn.albums
for al in incarn.albums:
self.years.append(al.year)
self.years = list(dict.fromkeys(self.years))
yearGroups = defaultdict(list)
for obj in self.incarnsAll:
yearGroups[obj.yearFirst].append(obj)
# new_list = groups.values()
self.incarnsAllGroupedByYear = sorted(
yearGroups.values(), key=lambda i: i[0].yearFirst
)
# TODO rank same incarns by year in subgraphs.
# for yearGroup in self.incarnsAllGroupedByYear:
# sg = Digraph(name=yearGroup[0].yearFirst)
# sg.attr(kw="graph", rank="same")
# for incarn in yearGroup:
# incarn.graphIncarnation(sg)
# self.gvGraph.subgraph(sg)
# link people who joined new group
for p in self.people:
p.incarnList = sorted(p.incarnList, key=lambda i: i.yearFirst)
for i, j in zip(p.incarnList, p.incarnList[1:]):
if i.artist == j.artist:
pass
else:
self.gvGraph.edge(
tail_name=i.name,
head_name=j.name,
xlabel=f"{p.fullname} joined",
)