File indexing completed on 2024-05-12 16:28:26

0001 #!/usr/bin/python -Qwarnall
0002 
0003 # This is a simple script to check which mark-up'ed icon names are backed with real icons
0004 # Call it from the toplevel source dir of Calligra.
0005 
0006 import sys, subprocess, polib
0007 
0008 class MissingIcon:
0009     def __init__(self, iconName):
0010         self.iconName = iconName
0011         self.fileLocations = []
0012 
0013     def addPoFileLocations(self, poFileLocations, context):
0014         for filePath, lineNumber in poFileLocations:
0015             self.fileLocations.append((filePath, lineNumber, context))
0016 
0017     def addDesktopFileLocation(self, filePath):
0018         self.fileLocations.append((filePath, None, None))
0019 
0020 # TODO: make path a variable
0021 def createCalligraIconFile(calligraIconsFile):
0022     r = subprocess.call("find . \( -name '*.png' -o -name '*.svg' -o -name '*.svgz' \) -printf '%%f\n' | sed -e 's/\.png$//' -e 's/\.svg$//' -e 's/\.svgz$//' | sort -u > %s" % (calligraIconsFile), shell=True)
0023     return r
0024 
0025 
0026 # TODO: make path a variable
0027 def createDotDesktopIconFile():
0028     r = subprocess.call("GREP_OPTIONS= grep 'Icon=' . -R --include=\*.desktop > dotDesktopIconFiles.list", shell=True)
0029     return r
0030 
0031 # TODO: make path a variable
0032 def createPotFile():
0033     r = subprocess.call("find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.c' | sort > koIconFiles.list", shell=True)
0034     if r:
0035         return r
0036     r = subprocess.call("xgettext --from-code=UTF-8 -k " +
0037              "-kkoIcon:1 " +
0038              "-kkoIconName:1 " +
0039              "-kkoIconNameCStr:1 " +
0040              "-kkoSmallIcon:1 " +
0041              "-kkoDesktopIcon:1 " +
0042 
0043              "-kkoIconNeeded:1c,2 " +
0044              "-kkoIconNeededWithSubs:1c,2 " +
0045              "-kkoIconNameNeeded:1c,2 " +
0046              "-kkoIconNameNeededWithSubs:1c,2 " +
0047              "-kkoIconNameCStrNeeded:1c,2 " +
0048              "-kkoIconNameCStrNeededWithSubs:1c,2 " +
0049 
0050              "-kkoIconWanted:1c,2 " +
0051              "-kkoIconNameWanted:1c,2 " +
0052 
0053              "-D . --files-from=koIconFiles.list -o koIconNames.po -n", shell=True)
0054     return r
0055     #TODO: use Python pipes and/or clean-up helper files
0056 
0057 sizePrefixes = (
0058     "16-", "22-", "32-", "48-", "64-", "128-", "sc-"
0059 )
0060 groupPrefixes = [
0061     # KDE 3 compatibility
0062     "mime-", "filesys-", "device-", "app-", "action-",
0063     # KDE 4 / icon naming specification compatibility
0064     "mimetypes-", "places-", "devices-", "apps-", "actions-", "categories-",
0065     "status-", "emblems-", "emotes-", "animations-", "intl-"
0066 ]
0067 
0068 def readIcons(fileName):
0069     iconNames = []
0070     with open(fileName, "r") as f:
0071         for line in f:
0072             iconName = line.strip().lower()
0073             if iconName:
0074                 if iconName.startswith(sizePrefixes):
0075                     iconName = iconName.split('-',2)[2]
0076                 iconNames.append(iconName)
0077     return set(iconNames)
0078 
0079 
0080 def readDotDesktopIcons(missingIconData):
0081     with open("dotDesktopIconFiles.list", "r") as f:
0082         for line in f:
0083             line = line.strip().lower()
0084             if line:
0085                 fileName, entry = line.split(':', 1)
0086                 iconName = entry.split('=', 1)[1]
0087                 if iconName:
0088                     if iconName in missingIconData:
0089                         missingIconData[iconName].addDesktopFileLocation(fileName)
0090                     else:
0091                         missingIcon = MissingIcon(iconName)
0092                         missingIcon.addDesktopFileLocation(fileName)
0093                         missingIconData[iconName] = missingIcon
0094 
0095 
0096 def main():
0097     #if len(sys.argv) < 1:
0098         #print >> sys.stderr, "usage: %s directory" % sys.argv[0]
0099         #sys.exit(1)
0100 
0101     oxygenIcons = readIcons('devtools/iconcheck/breeze-icons-5.4.1.list')
0102 
0103     r = createCalligraIconFile('calligra-icons.list')
0104     if r:
0105         return r
0106 
0107     calligraIcons = readIcons('calligra-icons.list')
0108 
0109     r = createPotFile()
0110     if r:
0111         return r
0112 
0113     r = createDotDesktopIconFile()
0114     if r:
0115         return r
0116 
0117     po = polib.pofile('koIconNames.po')
0118 
0119     # collect icons and their occurrences
0120     missingIconData = {}
0121 
0122     for entry in po:
0123         iconName = entry.msgid
0124         if iconName in missingIconData:
0125             missingIconData[iconName].addPoFileLocations(entry.occurrences, entry.comment)
0126         else:
0127             missingIcon = MissingIcon(iconName)
0128             missingIcon.addPoFileLocations(entry.occurrences, entry.msgctxt)
0129             missingIconData[iconName] = missingIcon
0130 
0131     readDotDesktopIcons(missingIconData)
0132 
0133     # output missing icons
0134     for iconName, missingIcon in missingIconData.iteritems():
0135         if not iconName in oxygenIcons:
0136             if not iconName in calligraIcons:
0137                 print iconName
0138                 for filePath, lineNumber, comment in missingIcon.fileLocations:
0139                     if comment:
0140                         print '    # %s' % (comment)
0141                     if lineNumber:
0142                         print '    %s:%s' % (filePath, lineNumber)
0143                     else:
0144                         print '    %s' % (filePath)
0145 
0146     return 0
0147 
0148 if __name__ == '__main__':
0149     main()