Example #1
0
 def loadTests(self, fname):
     #print "TestList::loadTests(" + fname + ")"
     
     # Assumes the file has been added to the UI.
     self.testGroups = []
     self.gcombo.clear()
     for i in range(self.liststack.count()-1, -1, -1) :
         self.liststack.removeWidget(self.liststack.widget(i))
     if not fname or not os.path.exists(fname) :
         # Create a new set of tests - these will be stored in a new file.
         self.initTests(fname)
         return
         
     try :
         e = et.parse(fname)
     except Exception as err:
         reportError("TestsFile %s: %s" % (fname, str(err)))
         return
     if e.getroot().tag == 'tests' :
         print "Can't find tests file " + fname
         self.loadOldTests(e)
         return
         
     styles = {}
     langs = {}
     self.header = e.find('.//head') 
     if self.header is None : self.header = e.find('.//header')
     for s in e.iterfind('.//style') :
         styleName = s.get('name')
         featDescrip = s.get('feats') or ""
         langCode = s.get('lang') or ""
         fset = featDescrip + "\n" + langCode
         if fset not in self.fsets : self.fsets[fset] = styleName
         styles[styleName] = {}
         if langCode : langs[styleName] = langCode
         for ft in featDescrip.split(" ") :
             if '=' in ft :
                 (fname, value) = ft.split('=')
                 if value and value != "None" :  # under buggy circumstances value can be 'None'; test for it just in case
                     styles[styleName][fname] = int(value)
         m = re.match(r'fset([0-9]+)', styleName)
         if m :
             i = int(m.group(1)) ## number of the fset, eg 'fset2' -> 2
             if i > self.fcount : self.fcount = i
                 
     for g in e.iterfind('testgroup') :
         listwidget = self.addGroup(g.get('label'))
         y = g.find('comment')
         self.comments.append(y.text if y else '')
         for t in g.iterfind('test') :
             y = t.find('string')
             if y is None : y = t.find('text')
             txt = y.text if y is not None else ""
             y = t.find('comment')
             c = y.text if y is not None else ""
             y = t.get('class')
             if y and y in styles :
                 feats = styles[y]
                 lng = langs.get(y)
             else :
                 feats = {}
                 lng = None
             te = Test(txt, feats, lang = lng, rtl = asBool(t.get('rtl')), name = t.get('label'), comment = c)
             b = t.get('background')
             if b :
                 res = QtGui.QColor(b)
                 if res.isValid() : te.background = res
             w = t.get('expand')
             if w :
                 te.setWidth(int(w))
             self.appendTest(te, listwidget)
Example #2
0
    def loadTests(self, fname):
        #print "TestList::loadTests(" + fname + ")"

        # Assumes the file has been added to the UI.
        self.testGroups = []
        self.gcombo.clear()
        for i in range(self.liststack.count() - 1, -1, -1):
            self.liststack.removeWidget(self.liststack.widget(i))
        if not fname or not os.path.exists(fname):
            # Create a new set of tests - these will be stored in a new file.
            self.initTests(fname)
            return

        try:
            e = et.parse(fname)
        except Exception as err:
            reportError("TestsFile %s: %s" % (fname, str(err)))
            return
        if e.getroot().tag == 'tests':
            print "Can't find tests file " + fname
            self.loadOldTests(e)
            return

        styles = {}
        langs = {}
        self.header = e.find('.//head')
        if self.header is None: self.header = e.find('.//header')
        for s in e.iterfind('.//style'):
            styleName = s.get('name')
            featDescrip = s.get('feats') or ""
            langCode = s.get('lang') or ""
            fset = featDescrip + "\n" + langCode
            if fset not in self.fsets: self.fsets[fset] = styleName
            styles[styleName] = {}
            if langCode: langs[styleName] = langCode
            for ft in featDescrip.split(" "):
                if '=' in ft:
                    (fname, value) = ft.split('=')
                    if value and value != "None":  # under buggy circumstances value can be 'None'; test for it just in case
                        styles[styleName][fname] = int(value)
            m = re.match(r'fset([0-9]+)', styleName)
            if m:
                i = int(m.group(1))  ## number of the fset, eg 'fset2' -> 2
                if i > self.fcount: self.fcount = i

        for g in e.iterfind('testgroup'):
            listwidget = self.addGroup(g.get('label'))
            y = g.find('comment')
            self.comments.append(y.text if y else '')
            for t in g.iterfind('test'):
                y = t.find('string')
                if y is None: y = t.find('text')
                txt = y.text if y is not None else ""
                y = t.find('comment')
                c = y.text if y is not None else ""
                y = t.get('class')
                if y and y in styles:
                    feats = styles[y]
                    lng = langs.get(y)
                else:
                    feats = {}
                    lng = None
                te = Test(txt,
                          feats,
                          lang=lng,
                          rtl=asBool(t.get('rtl')),
                          name=t.get('label'),
                          comment=c)
                b = t.get('background')
                if b:
                    res = QtGui.QColor(b)
                    if res.isValid(): te.background = res
                w = t.get('expand')
                if w:
                    te.setWidth(int(w))
                self.appendTest(te, listwidget)
Example #3
0
    def parseTweaks(self, fname) :
        try :
            e = XmlTree.parse(fname)
        except Exception as err:
            reportError("TweaksFile %s: %s" % (fname, str(err)))
            return

        result = {}
            
        styles = {}
        langs = {} 
        self.header = e.find('.//head') 
        if self.header is None : self.header = e.find('.//header')
        for s in e.iterfind('.//style') :
            styleName = s.get('name')
            featDescrip = s.get('feats') or ""
            langCode = s.get('lang') or ""
            fset = featDescrip + "\n" + langCode
            if fset not in self.fsets :
                self.fsets[fset] = styleName
            styles[styleName] = {}
            if langCode :
                langs[styleName] = langCode
            for ft in featDescrip.split(" ") :
                if '=' in ft :
                    (fname, value) = ft.split('=')
                    styles[styleName][fname] = int(value)
            m = re.match(r'fset([0-9]+)', styleName)
            if m :
                i = int(m.group(1)) ## number of the fset, eg 'fset2' -> 2
                if i > self.fcount : self.fcount = i
                    
        for g in e.iterfind('tweakgroup') :
            groupLabel = g.get('label')
            groupList = []

            tmp = g.find('comment')
            self.comments.append(tmp.text if tmp else '')
            for t in g.iterfind('tweak') :
                tmp = t.find('string')
                if tmp is None : tmp = t.find('text')
                txt = tmp.text if tmp is not None else ""
                
                tmp = t.find('comment')
                c = tmp.text if tmp is not None else ""
                tmp = t.get('class')  # named style, group of features
                if tmp and tmp in styles :
                    feats = styles[tmp]
                    lng = langs.get(tmp)
                else :
                    feats = {}
                    lng = None
                    
                label = t.get('label')
                tweak = Tweak(txt, feats, lang = lng, rtl = asBool(t.get('rtl')), name = t.get('label'), comment = c)
                b = t.get('background')
                if b :
                    res = QtGui.QColor(b)
                    if res.isValid() : tweak.background = res
                w = t.get('expand')  # ???
                if w :
                    tweak.setWidth(int(w))
                twglyphs = []                    
                for gl in t.iterfind('glyphs') :
                    for gf in gl.iterfind('glyph') :
                        if gf.find('gid') :
                            gid = int(gf.get('gid'))
                        else :
                            gid = 0
                        gname = gf.get('name')
                        gclass = gf.get('class')
                        req = gf.get('status')
                        shiftx = int(gf.get('shiftx'))
                        shifty = int(gf.get('shifty'))
                        shiftx_pending = int(gf.get('shiftx-pending'))
                        shifty_pending = int(gf.get('shifty-pending'))
                        twglyph = TweakGlyph(gid, gname, gclass, req, shiftx, shifty, shiftx_pending, shifty_pending)
                        twglyphs.append(twglyph)
                tweak.setGlyphs(twglyphs)
                
                groupList.append(tweak)
            result[groupLabel] = groupList
            
        return result
Example #4
0
    def search(self, fontFileName, targetFile, matchList = None) :
        
        matchResults = []
        
        if self.pattern == "" :
            print "No valid search pattern"
            return matchResults
        
        print "Searching " + targetFile + " for '" + self.pattern + "'..."
        
        cpat = re.compile(self.pattern)  # compiled pattern
        
        totalTests = self._countTests(targetFile)
        showProgress = (totalTests > 25)
        
        # Read and parse the target file.
        try :
            e = et.parse(targetFile)
        except Exception as err :
            reportError("Could not search %s: %s" % (targetFile, str(err)))
            print "could not search " + targetFile ####
            return matchResults
            
        faceAndFont = makeFontAndFace(fontFileName, 12)
        
        if showProgress :
            print "Total tests=",totalTests
            progressDialog = QtGui.QProgressDialog("Searching...0 matches", "Cancel", 0, totalTests, self.matcher)
            progressDialog.setWindowModality(QtCore.Qt.WindowModal)

        cntTested = 0;
        cntMatched = 0;
        canceled = False
        for g in e.iterfind('testgroup') :
            groupLabel = g.get('label')
            for t in g.iterfind('test') :
                testLabel = t.get('label')
                r = t.get('rtl')
                testRtl = True if r == 'True' else False
                d = t.find('string')
                if d is None : d = t.find('text')
                testString = d.text if d is not None else ""
                testComment = t.find('comment') or ""
                featDescrip = t.get('feats') or ""
                langCode = t.get('lang') or ""
                
                jsonOutput = self.app.runGraphiteOverString(fontFileName, faceAndFont, testString, 12, testRtl, {}, {}, 100)
                if jsonOutput != False :
                    glyphOutput = self._dataFromGlyphs(jsonOutput)
                    match = cpat.search(glyphOutput)
                else :
                    match = False
                    
                if match :
                    key = "[" + groupLabel + "] " + testLabel
                    
                    if matchList == None :
                        # Return a list of the results.
                        matchResults.append((key, testString, testRtl, testComment, featDescrip, langCode))
                    else :
                        # Put a match right into the control.
                        te = Test(text = testString, feats = featDescrip, lang = langCode, rtl = testRtl, name = key, comment = testComment)
                        matchList.appendTest(te)
                        matchResults = True
                        cntMatched = cntMatched + 1
                        if showProgress :
                            progressDialog.setLabelText("Searching..." + str(cntMatched) + " matches")
                ###else :
                ###    print testLabel + " did not match"
                
                cntTested = cntTested + 1
                #if cntTested >= 1 : canceled = True
                
                if showProgress :
                    if progressDialog.wasCanceled() :
                        canceled = True;
                    progressDialog.setValue(cntTested)
                    progressDialog.forceShow()
                
                if canceled : break
            # end of for t loop
            
            if canceled : break;
        # end of for g loop
        
        if showProgress :
            progressDialog.hide() # to counteract forceShow()
        
        if canceled :
            return False
        else :
            return matchResults
Example #5
0
    def search(self, fontFileName, targetFile, matchList=None):

        matchResults = []

        if self.pattern == "":
            print "No valid search pattern"
            return matchResults

        print "Searching " + targetFile + " for '" + self.pattern + "'..."

        cpat = re.compile(self.pattern)  # compiled pattern

        totalTests = self._countTests(targetFile)
        showProgress = (totalTests > 25)

        # Read and parse the target file.
        try:
            e = et.parse(targetFile)
        except Exception as err:
            reportError("Could not search %s: %s" % (targetFile, str(err)))
            print "could not search " + targetFile  ####
            return matchResults

        faceAndFont = makeFontAndFace(fontFileName, 12)

        if showProgress:
            print "Total tests=", totalTests
            progressDialog = QtGui.QProgressDialog("Searching...0 matches",
                                                   "Cancel", 0, totalTests,
                                                   self.matcher)
            progressDialog.setWindowModality(QtCore.Qt.WindowModal)

        cntTested = 0
        cntMatched = 0
        canceled = False
        for g in e.iterfind('testgroup'):
            groupLabel = g.get('label')
            for t in g.iterfind('test'):
                testLabel = t.get('label')
                r = t.get('rtl')
                testRtl = True if r == 'True' else False
                d = t.find('string')
                if d is None: d = t.find('text')
                testString = d.text if d is not None else ""
                testComment = t.find('comment') or ""
                featDescrip = t.get('feats') or ""
                langCode = t.get('lang') or ""

                jsonOutput = self.app.runGraphiteOverString(
                    fontFileName, faceAndFont, testString, 12, testRtl, {}, {},
                    100)
                if jsonOutput != False:
                    glyphOutput = self._dataFromGlyphs(jsonOutput)
                    match = cpat.search(glyphOutput)
                else:
                    match = False

                if match:
                    key = "[" + groupLabel + "] " + testLabel

                    if matchList == None:
                        # Return a list of the results.
                        matchResults.append(
                            (key, testString, testRtl, testComment,
                             featDescrip, langCode))
                    else:
                        # Put a match right into the control.
                        te = Test(text=testString,
                                  feats=featDescrip,
                                  lang=langCode,
                                  rtl=testRtl,
                                  name=key,
                                  comment=testComment)
                        matchList.appendTest(te)
                        matchResults = True
                        cntMatched = cntMatched + 1
                        if showProgress:
                            progressDialog.setLabelText("Searching..." +
                                                        str(cntMatched) +
                                                        " matches")
                ###else :
                ###    print testLabel + " did not match"

                cntTested = cntTested + 1
                #if cntTested >= 1 : canceled = True

                if showProgress:
                    if progressDialog.wasCanceled():
                        canceled = True
                    progressDialog.setValue(cntTested)
                    progressDialog.forceShow()

                if canceled: break
            # end of for t loop

            if canceled: break
        # end of for g loop

        if showProgress:
            progressDialog.hide()  # to counteract forceShow()

        if canceled:
            return False
        else:
            return matchResults