Changeset 13 for branches

Show
Ignore:
Timestamp:
10/18/07 10:08:40 (1 year ago)
Author:
baptiste
Message:

Added a database system that records fonts and folders visited. No doubt that it is bugged, but if you have a problem, please inform me.
It is a bit slow at the first startup, but then it becomes useful since FP doesn't have to find the fonts names at every start. Applications will come later.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/baptiste/fontypython/config.py

    r11 r13  
    8080                sys.exit(_("The fontypython config file is damaged.\nPlease remove it and start again")) 
    8181            self.__write() 
     82 
    8283    def dontSaveNumInPage(self, flag): 
    8384        self.__dontSaveNumInPage = flag 
     85 
    8486    def __setData(self): 
    8587        self.__data = {"size":self.size, 
     
    9395                                "lastdir": self.lastdir 
    9496                                } 
     97 
    9598    def __write(self): 
    9699        #If we are NOT to save the numinpage, then fetch it from what was there before. 
     
    104107        except IOError: 
    105108            print _("Could not write to the config file.") 
     109 
    106110    def save(self): 
    107111        self.__write()  #Go write the file 
  • branches/baptiste/fontypython/folder.py

    r12 r13  
    2323import fontybugs 
    2424import sys 
    25 import os 
     25import os, os.path 
     26import db 
     27import cStringIO 
     28import config 
     29import datetime 
    2630 
    2731## i18n imports 
     
    4145        ## Public var 
    4246        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] 
    4352 
    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) 
    5361                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 
    5491        if len(self) == 0: 
    5592            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.") 
    56101 
    57102    def label(self): 
  • branches/baptiste/fontypython/fontitem.py

    r6 r13  
    2020## 
    2121 
    22 import os 
     22import os, sys 
     23import db 
     24 
     25try: 
     26    from fontTools import ttLib 
     27    import ttlib 
     28    ttlib_active = True 
     29except: 
     30    ttlib_active = False 
    2331 
    2432#### 
     
    2634class FontItem: 
    2735    """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): 
    2937        self.paf = paf 
    3038        if self.paf == "EMPTY": self.name = "EMPTY" 
    31         else: self.name = os.path.basename(paf) 
    3239        self.ticked = ticked 
    3340        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  
    2222import sys 
    2323import fpsys 
     24import db 
    2425 
    2526## PIL : Is it there? 
    2627try: import Image, ImageFont, ImageDraw  
    27 except: sys.exit(strings.PILError())  
     28except: sys.exit(strings.PILError()) 
    2829     
    2930## Process the command line stuff