forked from cuthbertLab/music21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriters.py
471 lines (396 loc) · 17.3 KB
/
writers.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
# Name: docbuild/writers.py
# Purpose: music21 documentation writer to rst
#
# Authors: Josiah Wolf Oberholtzer
# Christopher Ariza
# Michael Scott Asato Cuthbert
#
# Copyright: Copyright © 2013-22 Michael Scott Asato Cuthbert
# License: BSD, see license.txt
# ------------------------------------------------------------------------------
from __future__ import annotations
import logging
import os
import pathlib
import re
import shutil
from more_itertools import windowed
from music21 import common
from music21 import exceptions21
from music21 import environment
from . import documenters
from . import iterators
environLocal = environment.Environment('docbuild.writers')
class DocumentationWritersException(exceptions21.Music21Exception):
pass
class _BuildDirectoryFilter(logging.Filter):
def filter(self, record):
msg = record.getMessage()
if not msg.startswith('Making directory'):
return True
dirMade = msg[len('Making directory '):].strip()
if not os.path.exists(dirMade):
return True
return False
class DocumentationWriter:
'''
Abstract base class for writers.
Call .run() on the object to make it work.
'''
def __init__(self):
self.outputDirectory = None
self.docBasePath = common.getRootFilePath() / 'documentation'
self.docSourcePath = self.docBasePath / 'source'
self.docGeneratedPath = self.docBasePath / 'autogenerated'
def run(self):
raise NotImplementedError
# PUBLIC METHODS #
def sourceToAutogenerated(self, sourcePath):
'''
converts a sourcePath to an outputPath
generally speaking, substitutes "source" for "autogenerated"
'''
outputPath = str(sourcePath.resolve()).replace(
str(self.docSourcePath), str(self.docGeneratedPath))
return pathlib.Path(outputPath)
def setupOutputDirectory(self, outputDirectory=None):
'''
creates outputDirectory (a pathlib.Path) if it does not exist.
Looks at self.outputDirectory if not there.
'''
if outputDirectory is None:
outputDirectory = self.outputDirectory
if outputDirectory is None:
raise DocumentationWritersException(
'Cannot setup output directory without guidance'
)
if outputDirectory.exists():
return
outputDirectory.mkdir()
class StaticFileCopier(DocumentationWriter):
'''
Copies static files into the autogenerated directory.
'''
def run(self):
excludedFiles = ['.ipynb', '__pycache__', '.pyc', '.gitignore', 'conf.py', '.DS_Store']
for subPath in sorted(self.docSourcePath.rglob('*')):
if subPath.is_dir():
self.setupOutputDirectory(self.sourceToAutogenerated(subPath))
continue
runIt = True
for ex in excludedFiles:
if subPath.name.endswith(ex):
runIt = False
if runIt is False:
continue
outputFilePath = self.sourceToAutogenerated(subPath)
if (outputFilePath.exists()
and outputFilePath.stat().st_mtime > subPath.stat().st_mtime):
print(f'\tSKIPPED {common.relativepath(outputFilePath)}')
else:
shutil.copyfile(str(subPath), str(outputFilePath))
print(f'\tWROTE {common.relativepath(outputFilePath)}')
class ReSTWriter(DocumentationWriter):
'''
Abstract base class for all ReST writers.
'''
def run(self):
raise NotImplementedError
def write(self, filePath, rst):
'''
Write ``rst`` (a unicode string) to ``filePath``, a pathlib.Path()
only overwriting an existing file if the content differs.
'''
shouldWrite = True
if filePath.exists():
oldRst = common.readFileEncodingSafe(filePath, firstGuess='utf-8')
if rst == oldRst:
shouldWrite = False
else:
pass
# # uncomment for help in figuring out why a file keeps being different
# import difflib
# print(common.relativepath(filePath))
# print('\n'.join(difflib.ndiff(rst.split('\n'), oldRst.split('\n'))))
if shouldWrite:
with filePath.open('w', encoding='utf-8') as f:
try:
f.write(rst)
except UnicodeEncodeError as uee:
raise DocumentationWritersException(
f'Could not write {filePath} with rst:\n{rst}'
) from uee
print(f'\tWROTE {common.relativepath(filePath)}')
else:
print(f'\tSKIPPED {common.relativepath(filePath)}')
class ModuleReferenceReSTWriter(ReSTWriter):
'''
Writes module reference ReST files, and their index.rst file.
'''
def __init__(self):
super().__init__()
self.outputDirectory = self.docGeneratedPath / 'moduleReference'
self.setupOutputDirectory()
def run(self):
moduleReferenceDirectoryPath = self.outputDirectory
referenceNames = []
for module in list(iterators.ModuleIterator()):
moduleDocumenter = documenters.ModuleDocumenter(module)
if (not moduleDocumenter.classDocumenters
and not moduleDocumenter.functionDocumenters):
continue
rst = '\n'.join(moduleDocumenter.run())
referenceName = moduleDocumenter.referenceName
referenceNames.append(referenceName)
fileName = f'{referenceName}.rst'
rstFilePath = moduleReferenceDirectoryPath / fileName
try:
self.write(rstFilePath, rst)
except TypeError as te: # pragma: no cover
raise TypeError(f'File failed: {rstFilePath}, reason: {te}')
self.writeIndexRst(referenceNames)
def writeIndexRst(self, referenceNames):
'''
Write the index.rst file from the list of reference names
'''
lines = []
lines.append('.. _moduleReference:')
lines.append('')
lines.append('.. WARNING: DO NOT EDIT THIS FILE:')
lines.append(' AUTOMATICALLY GENERATED.')
lines.append('')
lines.append('**Module Reference**')
lines.append('=========================')
lines.append('')
lines.append('.. toctree::')
lines.append(' :maxdepth: 1')
lines.append('')
for referenceName in sorted(referenceNames):
lines.append(f' {referenceName}')
rst = '\n'.join(lines)
indexFilePath = self.outputDirectory / 'index.rst'
self.write(indexFilePath, rst)
class CorpusReferenceReSTWriter(ReSTWriter):
'''
Write the corpus reference ReST file: referenceCorpus.rst
into about/
'''
def __init__(self):
super().__init__()
self.outputDirectory = self.docGeneratedPath / 'about'
self.setupOutputDirectory()
def run(self):
corpusReferenceFilePath = self.outputDirectory / 'referenceCorpus.rst'
lines = documenters.CorpusDocumenter().run()
rst = '\n'.join(lines)
self.write(corpusReferenceFilePath, rst)
class JupyterNotebookReSTWriter(ReSTWriter):
'''
Converts Jupyter notebooks into ReST, and handles their associated image
files.
This class wraps the 3rd-party ``nbconvert`` Python script.
'''
def __init__(self):
from .iterators import JupyterNotebookIterator
super().__init__()
self.jupyterNotebookFilePaths = list(JupyterNotebookIterator())
# Do not run self.setupOutputDirectory()
def run(self):
for jupyterNotebookFilePath in self.jupyterNotebookFilePaths:
nbConvertReturnCode = self.convertOneNotebook(jupyterNotebookFilePath)
if nbConvertReturnCode is True:
self.cleanupNotebookAssets(jupyterNotebookFilePath)
print(f'\tWROTE {common.relativepath(jupyterNotebookFilePath)}')
else:
print(f'\tSKIPPED {common.relativepath(jupyterNotebookFilePath)}')
# do not print anything for skipped -checkpoint files
self.writeIndexRst()
def writeIndexRst(self):
'''
Writes out the index.rst file for the usersGuide directory.
Does not do any other index.rst files. Just from the links in the
user's guide. I added this because keeping up the visual
table of contents and the index.rst was making my life miserable.
>>> ipRestWriter = IPythonNotebookReSTWriter()
>>> #_DOCS_HIDE ipRestWriter.writeIndexRst()
WROTE autogenerated/usersGuide/index.rst
'''
tocFile = 'usersGuide_99_Table_of_Contents'
ipFilePaths = [x for x in self.jupyterNotebookFilePaths
if 'usersGuide' in x.name and 'usersGuide' in x.parent.name]
if not ipFilePaths:
raise DocumentationWritersException(
'No Jupyter Notebook files were converted; '
+ 'you probably have a problem with pandoc or nbconvert not being installed.'
)
usersGuideDir = self.notebookFilePathToRstFilePath(ipFilePaths[0]).parent
tocFp = usersGuideDir / (tocFile + '.rst')
# '/Users/cuthbert/git/music21base/documentation/autogenerated/usersGuide'
usersGuideInOrder = [tocFile]
with tocFp.open('r', encoding='utf-8') as tocFileHandler:
for line in tocFileHandler:
matched = re.search(r'<(usersGuide.*)>', line)
if matched:
usersGuideInOrder.append(matched.group(1))
lines = []
lines.append('.. usersGuide:')
lines.append('')
lines.append('.. WARNING: DO NOT EDIT THIS FILE:')
lines.append(' AUTOMATICALLY GENERATED.')
lines.append('')
lines.append("User's Guide")
lines.append('================')
lines.append('')
lines.append('.. toctree::')
lines.append(' :maxdepth: 1')
lines.append('')
for referenceName in usersGuideInOrder:
lines.append(f' {referenceName}')
rst = '\n'.join(lines)
indexFilePath = usersGuideDir / 'index.rst'
self.write(indexFilePath, rst)
def cleanupNotebookAssets(self, jupyterNotebookFilePath):
'''
Deletes all .text files in the directory of jupyterNotebookFilePath
(a pathlib.Path).
'''
notebookFileNameWithoutExtension = jupyterNotebookFilePath.stem
notebookParentDirectoryPath = jupyterNotebookFilePath.parent
imageFileDirectoryPath = notebookParentDirectoryPath / notebookFileNameWithoutExtension
imageFileDirectoryPath = self.sourceToAutogenerated(imageFileDirectoryPath)
if not imageFileDirectoryPath.exists():
return
for filePath in sorted(imageFileDirectoryPath.glob('*.text')):
filePath.unlink()
@property
def rstEditingWarningFormat(self):
result = []
result.append('.. WARNING: DO NOT EDIT THIS FILE:')
result.append(' AUTOMATICALLY GENERATED.')
result.append(' PLEASE EDIT THE .py FILE DIRECTLY.')
result.append('')
return result
def notebookFilePathToRstFilePath(self, jupyterNotebookFilePath):
if not jupyterNotebookFilePath.exists():
raise DocumentationWritersException(
f'No Jupyter Notebook with filePath {jupyterNotebookFilePath}')
notebookFileNameWithoutExtension = jupyterNotebookFilePath.stem
notebookParentDirectoryPath = jupyterNotebookFilePath.parent
rstFileName = notebookFileNameWithoutExtension + '.rst'
rstFilePath = self.sourceToAutogenerated(notebookParentDirectoryPath / rstFileName)
return rstFilePath
def convertOneNotebook(self, jupyterNotebookFilePath):
'''
converts one .ipynb file to .rst using nbconvert.
returns True if IPythonNotebook was converted.
returns False if IPythonNotebook's converted .rst file is newer than the .ipynb file.
sends AssertionError if jupyterNotebookFilePath does not exist.
'''
rstFilePath = self.notebookFilePathToRstFilePath(jupyterNotebookFilePath)
if rstFilePath.exists():
# print(rstFilePath + ' exists')
# rst file is newer than .ipynb file, do not convert.
if rstFilePath.stat().st_mtime > jupyterNotebookFilePath.stat().st_mtime:
return False
self.runNBConvert(jupyterNotebookFilePath)
# uses this convoluted way of reading because 'encoding' was an invalid keyword argument
# for the built-in 'open' in old python, and never upgraded.
with rstFilePath.open('r', encoding='utf8') as f:
oldLines = f.read().splitlines()
lines = self.cleanConvertedNotebook(oldLines, jupyterNotebookFilePath)
with rstFilePath.open('w', encoding='utf8') as f:
f.write('\n'.join(lines))
return True
def cleanConvertedNotebook(self, oldLines, jupyterNotebookFilePath):
'''
Take a notebook directly as parsed and make it look better for HTML
Fixes up the internal references to class, ref, func, meth, attr.
'''
notebookFileNameWithoutExtension = jupyterNotebookFilePath.stem
# imageFileDirectoryName = self.sourceToAutogenerated(notebookFileNameWithoutExtension)
jupyterPromptPattern = re.compile(r'^In\[[\d ]+]:')
mangledInternalReference = re.compile(
r':(class|ref|func|meth|attr):``?(.*?)``?')
newLines = [f'.. _{notebookFileNameWithoutExtension}:',
'']
newLines += self.rstEditingWarningFormat
currentLineNumber = 0
while currentLineNumber < len(oldLines):
currentLine = oldLines[currentLineNumber]
# Remove all IPython prompts and the blank line that follows:
if jupyterPromptPattern.match(currentLine) is not None:
currentLineNumber += 2
continue
# Correct the image path in each ReST image directive:
elif currentLine.startswith('.. image:: '):
imageFileName = currentLine.partition('.. image:: ')[2]
imageFileShort = imageFileName.split(os.path.sep)[-1]
if notebookFileNameWithoutExtension in currentLine:
newImageDirective = f'.. image:: {imageFileShort}'
newLines.append(newImageDirective)
else:
newLines.append(currentLine)
currentLineNumber += 1
elif '# ignore this' in currentLine:
if '.. code:: ' in newLines[-2]:
# print('STOP HERE!')
newLines.pop() # remove blank line
newLines.pop() # remove '.. code:: python'
# compensate for:
# -- # ignore this
# -- %load_ext music21.ipython21.ipExtension
# by skipping two lines.
currentLineNumber += 2
# TODO: Skip all % lines, without looking for '#ignore this'
# Otherwise, nothing special to do, just add the line to our results:
else:
# fix cases of inline :class:`~music21.stream.Stream` being
# converted by markdown to :class:``~music21.stream.Stream``
newCurrentLine = mangledInternalReference.sub(
r':\1:`\2`',
currentLine
)
newLines.append(newCurrentLine)
currentLineNumber += 1
lines = self.blankLineAfterLiteral(newLines)
return lines
def blankLineAfterLiteral(self, oldLines):
'''
Guarantee a blank line after literal blocks.
'''
lines = [oldLines[0]] # start with first line.
for first, second in windowed(oldLines, 2):
if (first.strip()
and first[0].isspace()
and second.strip()
and not second[0].isspace()):
lines.append('')
lines.append(second)
if '.. parsed-literal::' in second:
lines.append(' :class: ipython-result')
return lines
def runNBConvert(self, jupyterNotebookFilePath):
try:
# noinspection PyPackageRequirements
from nbconvert import nbconvertapp as nb
except ImportError:
environLocal.warn('nbconvert is not installed, run pip3 install nbconvert')
raise
outputPath = os.path.splitext(
str(self.sourceToAutogenerated(jupyterNotebookFilePath))
)[0]
app = nb.NbConvertApp.instance()
app.initialize(argv=['--to', 'rst', '--output', outputPath,
str(jupyterNotebookFilePath)])
app.writer.build_directory = str(jupyterNotebookFilePath.parent)
app.writer.log.addFilter(_BuildDirectoryFilter())
app.start()
return True
if __name__ == '__main__':
i = JupyterNotebookReSTWriter()
p5 = i.jupyterNotebookFilePaths[5]
i.convertOneNotebook(p5)
import music21
music21.mainTest('moduleRelative')