- Timestamp:
- 10/18/07 10:08:40 (1 year ago)
- Files:
-
- branches/baptiste/fontypython/config.py (modified) (3 diffs)
- branches/baptiste/fontypython/db.py (added)
- branches/baptiste/fontypython/folder.py (modified) (2 diffs)
- branches/baptiste/fontypython/fontitem.py (modified) (2 diffs)
- branches/baptiste/fontypython/start.py (modified) (1 diff)
- branches/baptiste/fontypython/ttlib.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/baptiste/fontypython/config.py
r11 r13 80 80 sys.exit(_("The fontypython config file is damaged.\nPlease remove it and start again")) 81 81 self.__write() 82 82 83 def dontSaveNumInPage(self, flag): 83 84 self.__dontSaveNumInPage = flag 85 84 86 def __setData(self): 85 87 self.__data = {"size":self.size, … … 93 95 "lastdir": self.lastdir 94 96 } 97 95 98 def __write(self): 96 99 #If we are NOT to save the numinpage, then fetch it from what was there before. … … 104 107 except IOError: 105 108 print _("Could not write to the config file.") 109 106 110 def save(self): 107 111 self.__write() #Go write the file branches/baptiste/fontypython/folder.py
r12 r13 23 23 import fontybugs 24 24 import sys 25 import os 25 import os, os.path 26 import db 27 import cStringIO 28 import config 29 import datetime 26 30 27 31 ## i18n imports … … 41 45 ## Public var 42 46 self.path = os.path.abspath(path) # fix relative paths 47 cursor = db.database.cursor() 48 mtime = datetime.datetime.fromtimestamp(os.path.getmtime(self.path)) #Last modification 49 query = cursor.execute("select id, path, mtime from folders where path = ? \ 50 and mtime = ?", (self.path, mtime.isoformat())) #Let's see if the folder is in the db and if it has not been modified 51 result = [i for i in cursor] 43 52 44 ## Fill my tanks! 45 for f in os.listdir(self.path): 46 if f.upper().endswith(".TTF") or f.upper().endswith(".OTF") or \ 47 f.upper().endswith(".PFB"): 48 #print "f is ", f 49 try: 50 fi = FontItem(os.path.join(self.path, f), False) 51 except UnicodeDecodeError, e: 52 sys.exit(_("The file %s has caused a unicode error.\nPlease rename it, removing any strange characters, and try again.") % f) 53 if result: #The folder has already been checked... and has not been modified. let's not scan all the fonts again. 54 self.id = result[0][0] 55 cursor.execute("select id,folder_id,name,family,filename,rating,filename_preview,\ 56 preview_settings,has_tag from fonts where fonts.folder_id=?", (self.id,)) 57 for row in cursor: 58 #We build the list of font with the db, not with the real files. 59 #No big deal since they have not been modified. 60 fi = FontItem(row[4], False, self.id, data=row) 53 61 self.append(fi) 62 else: #The folder has never been recored in db, or it has been modified. 63 try: 64 cursor.execute("insert into folders (path, mtime) values (?, ?)", (self.path, mtime.isoformat()))#Let's add it to the db 65 db.database.commit() 66 self.id = cursor.lastrowid 67 except:#Error because folder is already in the db and that path is unique. So we seek the id of the existing record. 68 query = cursor.execute("select id, path, mtime from folders where path = ?", 69 (self.path,)) 70 result = [i for i in cursor] 71 self.id = result[0][0] 72 73 cursor.close() 74 print _("Scanning folder %s...") % self.path #We need to scan (sometimes again) the folder. 75 SAVEOUT = sys.stdout 76 capture = cStringIO.StringIO() 77 sys.stdout = capture #We redirect stdout because ttquery can raises warning, and we need to catch them, not to display them 78 79 for f in os.listdir(self.path): 80 if f.upper().endswith(".TTF") or f.upper().endswith(".OTF") or \ 81 f.upper().endswith(".PFB"): 82 #print "f is ", f 83 try: 84 fi = FontItem(os.path.join(self.path, f), False, self.id, capture=capture) 85 except UnicodeDecodeError, e: 86 sys.exit(_("The file %s has caused a unicode error.\nPlease rename it, removing any strange characters, and try again.") % f) 87 self.append(fi) 88 sys.stdout = SAVEOUT #ttquery is done, we use the classic stdout now. 89 capture.close() 90 54 91 if len(self) == 0: 55 92 raise fontybugs.FolderHasNoFonts(self.path) 93 try: 94 db.database.commit() 95 except Exception, e: 96 print _("I'm sorry, but something is wrong. Writing in database failed.") 97 print e 98 print _("Please contact the author.") 99 else: 100 print _("Done.") 56 101 57 102 def label(self): branches/baptiste/fontypython/fontitem.py
r6 r13 20 20 ## 21 21 22 import os 22 import os, sys 23 import db 24 25 try: 26 from fontTools import ttLib 27 import ttlib 28 ttlib_active = True 29 except: 30 ttlib_active = False 23 31 24 32 #### … … 26 34 class FontItem: 27 35 """Represents a single font""" 28 def __init__(self, paf, ticked, inactive = False, msg = ""):36 def __init__(self, paf, ticked, folder_id, inactive = False, msg = "", capture=False, data=None): 29 37 self.paf = paf 30 38 if self.paf == "EMPTY": self.name = "EMPTY" 31 else: self.name = os.path.basename(paf)32 39 self.ticked = ticked 33 40 self.inactive = inactive #Means it shows, but cannot be ticked. 34 self.msg = msg #Say something unique when I draw this item. 35 self.family = "" #The font family :: added Nov 2006 36 self.style = "" # font.getname()[1] = found in ImageFont.py 41 self.msg = msg #Say something unique when I draw this item. 42 self.capture = capture 43 self.folder_id = folder_id 44 if data: 45 self.name, self.family, self.filename = data[1], data[2], data[3] 46 self.rating, self.filename_preview = data[4], data[5] 47 self.has_tag, self.preview_settings = data[6], data[7] 48 else: 49 self.__database_init() 50 51 def __database_init(self): 52 if self.paf == "EMPTY": 53 return 54 cursor = db.database.cursor() 55 cursor.execute("select id,name,family,filename,rating,filename_preview,\ 56 preview_settings,has_tag from fonts where fonts.filename=?", (self.paf,)) 57 result = [i for i in cursor] 58 if not result: 59 try: 60 if ttlib_active: 61 font = ttlib.openFont(self.paf) 62 full_name = ttlib.shortName(font) 63 else: 64 raise Exception 65 if self.capture and 'Warning' in self.capture.get_value():#ttlib has raised a warning 66 raise Exception 67 except: #Error... ttlib failed or it is not installed. We try with PIL. 68 import ImageFont 69 try: 70 self.font = ImageFont.truetype(self.paf, 2) 71 full_name = self.font.getname() 72 except IOError: #PIL can't read the font 73 self.inactive = True 74 return 75 76 self.name = full_name[0] 77 self.family = ((full_name[0] != full_name[1]) and full_name[1]) or '' 78 79 self.rating, self.filename_preview = 'NA', '' 80 self.preview_settings, self.has_tag = '', 0 81 cursor.execute("insert into fonts (folder_id,name,family,filename,rating,\ 82 filename_preview,preview_settings,has_tag) values (?, ?, ?, ?, ?, ?, ?, ?)", 83 (self.folder_id, self.name, self.family, self.paf, self.rating, 84 self.filename_preview, self.preview_settings, self.has_tag)) 85 else: 86 result = result[0] #Sqlite returns a list of matchs, but there is just one font for each path, so the one we want is the first 87 self.name, self.family, self.filename = result[1], result[2], result[3] 88 self.rating, self.filename_preview = result[4], result[5] 89 self.has_tag, self.preview_settings = result[6], result[7] branches/baptiste/fontypython/start.py
r11 r13 22 22 import sys 23 23 import fpsys 24 import db 24 25 25 26 ## PIL : Is it there? 26 27 try: import Image, ImageFont, ImageDraw 27 except: sys.exit(strings.PILError()) 28 except: sys.exit(strings.PILError()) 28 29 29 30 ## Process the command line stuff
