Spaces:
Sleeping
Sleeping
File size: 8,783 Bytes
f65fe85 |
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 |
# ref-check.py
#
# This file is part of LilyPond, the GNU music typesetter.
#
# Copyright (C) 2010--2020 Trevor Daniels <[email protected]>
#
# LilyPond is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LilyPond is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
"""
*** RefCheck
Flow
Read files
On @defManual manual refManual eg @defManual LM rlearning
Associate manual name 'manual' with reference @'refManual'{ ... }
Add 'refManual' to list of keywords
On @setManual manual set current manual name to 'manual'
On @include open and process new file
On @node add node to Nodes list with current manual name and current file name
On @ref add contents to References list with refManual, current manual name and current file name
On one of refManual keywords add contents to References list with ditto
Match refs and nodes
Process References list:
Check reference is contained in Nodes list with refManual in Nodes = refManual in References
Print results
Known issues
Node names containing commas are only checked up to the comma
Spurious warnings "Xref should be internal" for files in /included
"""
##################################################
class CrossRefs:
""" Holds References and Nodes """
def __init__(self):
self.Manuals = {}
self.Nodes = {}
self.nodeNames = {} # used to check for duplicates only
self.Refs = []
self.nodeCount = 0
def addManual(self, manualName, refManual):
self.Manuals[refManual] = manualName
def getRefManuals(self):
return list(self.Manuals.keys())
def getManualName(self, refManual):
return self.Manuals[refManual]
def addNode(self, nodeName, manualName, fileName):
global returnCode
# print "Node: ", nodeName, " in ", manualName, " found in ", fileName
if "\\" in nodeName:
returnCode = 1
print("nodeName: ", nodeName, " in ",
fileName, " contains backslash")
if manualName+"/"+nodeName in list(self.Nodes.keys()):
print("Error: Duplicate nodename ", nodeName, " in ",
fileName, " and ", self.Nodes[manualName+"/"+nodeName][1])
returnCode = 1
self.Nodes[manualName + "/" + nodeName] = [manualName, fileName]
self.nodeNames[nodeName] = fileName
def addRef(self, toManualName, toHeading, inFileName):
global returnCode
if "\\" in toHeading:
returnCode = 1
print("ref to: ", toHeading, " in ",
inFileName, " contains backslash")
# if inFileName == "notation/vocal.itely":
# print "Ref to ", toManualName, "/",toHeading, " found in ", inFileName
self.Refs.append([toManualName + "/" + toHeading, inFileName])
def check(self):
noErrors = True
for [refHeading, refFileName] in self.Refs:
try:
targetFileName = self.Nodes[refHeading]
# if refFileName == "notation/vocal.itely":
# print "ref to: ", refHeading, " in ", refFileName, " found in ", targetFileName
except KeyError:
noErrors = False
print("ref to: ", refHeading, " in ",
refFileName, " not found")
if noErrors:
print(" All references satisfied")
else:
returnCode = 1
##################################################
class File:
""" Process an included file """
# Class variables
CurrentManualName = ""
DefaultPath = ""
Excludes = []
Paths = {}
# Methods
def __init__(self, fileName):
self.fileName = fileName
try:
self.fullFileName = File.Paths[fileName] + fileName
except KeyError:
self.fullFileName = File.DefaultPath + fileName
def read(self, crossRefs):
""" Process File """
skip = False
try:
myfile = open(self.fullFileName, 'r', encoding='utf8')
except IOError:
print("File ", self.fullFileName, " referenced in ",
File.CurrentManualName, " but not found")
return
remainderLine = ""
lineNo = 0
for line in myfile:
lineNo += 1
words = line.split()
if len(words) > 0:
if words[0] == "@ignore" or words[0] == "@macro":
skip = True
if skip and len(words) > 1:
if words[0] == "@end" and (words[1].find("ignore") >= 0 or words[1].find("macro") >= 0):
skip = False
if not skip and words[0] != "@c":
if words[0].find("@defManual") >= 0:
# Manual definition found - extract manual name and refManual string
manualName = words[1]
refManual = words[2]
crossRefs.addManual(manualName, refManual)
# print manualName, refManual
elif words[0].find("@defaultPath") >= 0:
File.DefaultPath = words[1]
elif words[0].find("@path") >= 0:
File.Paths[words[1]] = words[2]
elif words[0].find("@setManual") >= 0:
File.CurrentManualName = words[1]
# print " Checking ", File.CurrentManualName
elif words[0].find("@exclude") >= 0:
File.Excludes.append(words[1])
elif words[0].find("@include") >= 0:
if words[1] not in File.Excludes:
currentFileName = words[1]
# print " File: ", currentFileName
currentFile = File(currentFileName)
currentFile.read(crossRefs)
elif words[0] == "@node":
nodeName = line[6:-1]
crossRefs.addNode(
nodeName, File.CurrentManualName, self.fileName)
# Find references
twoLines = remainderLine + ' ' + line.strip()
manualRefStrings = crossRefs.getRefManuals()
refFound = False
for manualRefString in manualRefStrings:
toManualName = crossRefs.getManualName(manualRefString)
actualToManualName = toManualName
if toManualName == "this":
toManualName = File.CurrentManualName
refString = "@" + manualRefString + "{"
refStart = twoLines.find(refString)
if refStart >= 0:
refFound = True
if actualToManualName == File.CurrentManualName:
print("Warning: should xref be internal around line ",
lineNo, " in ", self.fileName, "?")
twoLines = twoLines[refStart:]
refNodeStart = twoLines.find("{") + 1
# TODO Need to check here for nested {}
refNodeEnd = twoLines.find("}")
refNodeEndComma = twoLines.find(",")
if refNodeEndComma > 0:
refNodeEnd = min(refNodeEnd, refNodeEndComma)
if refNodeEnd >= 0:
crossRefs.addRef(
toManualName, twoLines[refNodeStart:refNodeEnd], self.fileName)
remainderLine = twoLines[refNodeEnd+1:]
if refFound:
refFound = False
break
if not refFound:
remainderLine = ""
myfile.close()
return
topFile = File("scripts/auxiliar/ref_check.tely") # TODO get from input params
print("RefCheck ver 0.1")
returnCode = 0
crossRefs = CrossRefs()
topFile.read(crossRefs)
crossRefs.check()
if returnCode > 0:
print("Errors found: status code: ", returnCode)
|