MichaelUmmels

Attachment 'qtfonts.py'

Download

   1 #!/usr/bin/python
   2 
   3 # qtfonts.py: Sets up font substitution for QT3
   4 # Copyright (C) 2007 Michael Ummels (michael@ummels.de)
   5 #
   6 # This program is free software: you can redistribute it and/or modify
   7 # it under the terms of the GNU General Public License as published by
   8 # the Free Software Foundation, either version 3 of the License, or
   9 # (at your option) any later version.
  10 #
  11 # This program is distributed in the hope that it will be useful,
  12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 # GNU General Public License for more details.
  15 #
  16 # You should have received a copy of the GNU General Public License
  17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18 
  19 import commands
  20 import os.path
  21 import sys
  22 
  23 helpmessage = """Usage: qtfonts.py [options]
  24 Sets up font substitution for QT3.
  25 
  26 Options:
  27   --help       Prints this help message
  28   --system     Writes to global qtrc instead of one in $HOME/.qt
  29   --verbose    Prints verbose messages"""
  30 
  31 verboseopt = False
  32 systemopt = False
  33 
  34 # Parse arguments
  35 while len(sys.argv) > 1:
  36 	arg = sys.argv.pop()
  37 	if arg == '-s' or arg == '--system':
  38 		systemopt = True
  39 	elif arg == '-v' or arg == '--verbose':
  40 		verboseopt = True
  41 	elif arg == '-h' or arg == '--help':
  42 		print helpmessage
  43 		sys.exit(0)
  44 	else:
  45 		sys.exit('Wrong argument. Try `qtfonts.py --help\' for more information.')
  46 
  47 # Check whether config file is writable
  48 if systemopt:
  49 	path = '/etc/qt3/qtrc'
  50 else:
  51 	path = os.path.expanduser('~/.qt/qtrc')
  52 try:
  53 	file = open(path, 'a')
  54 except IOError:
  55 	sys.exit('Cannot write to ' + path + '.')
  56 else:
  57 	file.close()
  58 try:
  59 	file = open(path + '.old', 'a')
  60 except IOError:
  61 	sys.exit('Cannot write backup file. Leaving ' + path + ' unchanged.')
  62 
  63 fonts = set([])
  64 allsubs = []
  65 
  66 # List all fonts
  67 fclist = commands.getstatusoutput('fc-list : family')
  68 if not(fclist[0] == 0):
  69 	sys.exit('Cannot execute `fc-list\'. Check whether fontconfig is installed.')
  70 fontlist = fclist[1].splitlines()
  71 for x in fontlist:
  72 	# Get family name
  73 	font = x.partition(',')[0].replace('\-', '-')
  74 	# Check whether family has not already been processed
  75 	if not(font in fonts):
  76 		if verboseopt:
  77 			print 'Processing ' + font + '...'
  78 		fonts.add(font)
  79 		# Get list of substitutions
  80 		fcmatch = commands.getoutput('fc-match -sv :family=\"' + font + '\" | egrep \"family:|familylang:\"').split('family: ')[1:]
  81 		subsstring = ''
  82 		subsset = set([])
  83 		first = True
  84 		# For each substitution
  85 		for y in fcmatch:
  86 			# Get index of English family name (if any, 0 otherwise)
  87 			pos = y.find('\"en\"', y.find('familylang:'))
  88 			if pos >= 0:
  89 				index = y.count('\"', y.find('familylang:'), pos)
  90 			else:
  91 				index = 0
  92 			# Get family name
  93 			sub = y.split('\"', index + 2)[index + 1]
  94 			# Check whether family is not already in the list of substitutions
  95 			if not(sub in subsset):
  96 				subsset.add(sub)
  97 				# Write substitution string except for the first one (which is the font itself)
  98 				if not(first):
  99 					subsstring = subsstring + sub.replace('-', ' ') + '^e'
 100 				else:
 101 					first = False
 102 		if len(subsstring) > 0:
 103 			allsubs.append([font.replace('-', ' '), subsstring])
 104 
 105 # Read config file
 106 result = []
 107 file = open(path)
 108 newconfig = ''
 109 oldconfig = ''
 110 
 111 # Copy beginning of config file
 112 lastline = ''
 113 line = file.readline()
 114 while not(line == '[Font Substitutions]\n' or line == ''):
 115 	lastline = line
 116 	newconfig = newconfig + line
 117 	oldconfig = oldconfig + line
 118 	line = file.readline()
 119 if line == '' and lastline.endswith('\n'):
 120 	newconfig = newconfig + '\n'
 121 elif line == '':
 122 	newconfig = newconfig + '\n\n'
 123 
 124 # Add new font substitution section
 125 newconfig = newconfig + '[Font Substitutions]\n'
 126 for sub in allsubs:
 127 	newconfig = newconfig + sub[0] + '=' + sub[1] + '\n'
 128 
 129 # Copy rest of config file
 130 oldconfig = oldconfig + line
 131 line = file.readline()
 132 while not(line == '' or line[:1] == '['):
 133 	oldconfig = oldconfig + line
 134 	line = file.readline();
 135 if not(line == ''):
 136 	newconfig = newconfig + '\n'
 137 while not(line == ''):
 138 	oldconfig = oldconfig + line
 139 	newconfig = newconfig + line
 140 	line = file.readline()
 141 file.close()
 142 
 143 # Write backup file
 144 file = open(path + '.old', 'w')
 145 file.write(oldconfig)
 146 file.close
 147 if verboseopt:
 148 	print 'Backup of old config file written to ' + path + '.old.'
 149 
 150 # Write config file
 151 file = open(path, 'w')
 152 file.write(newconfig)
 153 file.close()
 154 if verboseopt:
 155 	print 'New config file written to ' + path + '.'

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-12-27 21:59:42, 186.5 KB) [[attachment:latex-cleveref_0.8-0ubuntu1_i386.deb]]
  • [get | view] (2007-12-27 21:58:54, 4.3 KB) [[attachment:qtfonts.py]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.