Posted by kahrn on Tue 4 Sep 19:09
report abuse | download | new post
- ## xfce4-appentry-optimize [0.2.1]
- ##
- ## Licensed under the GNU GPL
- ## Programming by Ricky Hewitt [kahrn]
- ##
- ## Changes in 0.2.1:
- ## - Cleaned up and optimized the source code a little.
- ##
- ## Mon 27 Aug 2007 11:28:04 BST
- ## kahrny@gmail.com / kahrn.co.uk / kahrn.wordpress.com
- ## Import
- import os, sys
- ##########################
- # User definable variables
- # Do not forget to update the locale section.
- # locale defines the locale you wish to KEEP in the optimization process. All other locales
- # will be removed. If no locale is specified (or invalid), then it will remove ALL locales apart from the
- # one with no locale specification. (example: en_GB, fr, ca)
- locale = "en_GB"
- # app_path defines where to look for the .desktop files used for the application menu
- app_path = "/usr/share/applications/" # Don't forget ending forward slash (/)!
- # Enable backup (to disable backup, set to 0). Backups are saved in app_path/backup/backup.tar.gz..
- enable_backup = 0
- ##########################
- class AppEntry:
- # Set any variables we might need..
- workingData = []
- workingNameLocale = []
- def CheckDir(self):
- """Search for the location of the .desktop files being used for the application menu."""
- if os.access(app_path, 1) == True:
- return 1
- else:
- return 0
- def ListFiles(self):
- """List all the .desktop files used in app_path"""
- return os.listdir(app_path)
- def Optimize(self, filename):
- """Perform the optimization routines in the preferred order (and additional stuff)"""
- self.OptimizeLocale(filename, "Comment")
- self.OptimizeLocale(filename, "GenericName")
- self.OptimizeLocale(filename, "Name")
- def OptimizeLocale(self, filename, field_type):
- """Remove all locales (Name=) except from the one specified.
- filename specified the filename we want to optimize (the .desktop file).."""
- # Strip of Name entries and leave only the locales we want to keep.
- file = open(filename, 'r')
- for line in file.readlines():
- if line.startswith(field_type+'=') or line.startswith(field_type+"["+locale+"]"):
- self.workingNameLocale.append(line[:-1])
- file.close()
- # Now grab all other lines so we can save them to the new file with the newly created locale data.
- file = open(filename, 'r')
- for line in file.readlines():
- if not line.startswith(field_type):
- self.workingData.append(line[:-1])
- file.close()
- # Write the new file..
- file = open(filename, 'w')
- file.write(self.workingData.pop(0)+"\n")
- file.write(self.workingData.pop(0)+"\n")
- for i in self.workingNameLocale:
- file.write(i+"\n")
- for i in self.workingData:
- file.write(i+"\n")
- file.close()
- # Reset variables
- self.workingData = []
- self.workingNameLocale = []
- def CheckRoot():
- """This function will check to see if the user is running under root priviledges"""
- if not os.geteuid()==0:
- sys.exit("\n Please run as root!\n")
- else:
- return 1
- def CreateBackup():
- """This function will create a backup and put it into a gziped tape archive."""
- if enable_backup == 1:
- try:
- print "Attempting to create backup in "+app_path+"backup"
- os.chdir(app_path)
- os.system("mkdir ./backup/")
- os.system("tar -czf backup.tar.gz ./*.desktop")
- os.system("mv ./backup.tar.gz ./backup/backup.tar.gz")
- print "Backup created in "+app_path+"backup/backup.tar.gz"
- return 1
- except:
- print "Failed to create backup in CreateBackup().."
- return 0
- else:
- print "Backup was disabled. Backup was not created."
- return 1
- def main():
- # Show starting messages and version information..
- print "xfce4-appentry-optimize [0.2.1]"
- print "Programmed by Ricky Hewitt [kahrn] - Licensed under GNU GPL."
- # Create instance of class AppEntry..
- AppEntryInstance = AppEntry()
- # Set some variables..
- filelist = []
- currentFile = ""
- FileOriginalSize = 0
- FileEndSize = 0
- TotalSavedBytes = 0
- x = 0 # number of shortcuts (to work out how many have been optimized at end)
- # Search for the location of the .desktop files..
- if AppEntryInstance.CheckDir() and CheckRoot() and CreateBackup():
- for i in AppEntryInstance.ListFiles():
- # Create a list of files (with full path) to optimize..
- filelist.append(app_path+i)
- currentFile=filelist.pop(0)
- print " Optimizing \""+currentFile+"\":"
- # Deal with byte stat counting
- FileOriginalSize = os.path.getsize(currentFile)
- print " Current size is: "+str(FileOriginalSize)+" bytes"
- # Optimize
- print " Optimizing locale data.."
- try:
- AppEntryInstance.Optimize(currentFile)
- except:
- "Could not optimize.. probably not a .desktop file."
- # Deal with byte stat counting
- FileEndSize = os.path.getsize(currentFile)
- FileSavedBytes = (FileOriginalSize-FileEndSize)
- print " Bytes Saved: "+str(FileSavedBytes)+" bytes"
- TotalSavedBytes = (TotalSavedBytes+FileSavedBytes)
- # Reset and increment some variables..
- currentFile = ""
- x = x+1
- # Display results
- print "\n RESULTS:\n Saved a total of "+str(TotalSavedBytes)+" bytes across "+str(x)+" shortcuts."
- sys.exit("\n Something went wrong!\n")
- if __name__ == "__main__":
- main()
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.