<s> import re <EOL> from feaTools import parser <EOL> from feaTools . writers . fdkSyntaxWriter import FDKSyntaxFeatureWriter <EOL> class FilterFeatureWriter ( FDKSyntaxFeatureWriter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , refs = set ( ) , name = None , isFeature = False ) : <EOL> """<STR_LIT>""" <EOL> self . refs = refs <EOL> self . featureNames = set ( ) <EOL> self . lookupNames = set ( ) <EOL> self . tableNames = set ( ) <EOL> self . languageSystems = set ( ) <EOL> super ( FilterFeatureWriter , self ) . __init__ ( <EOL> name = name , isFeature = isFeature ) <EOL> self . classErr = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> subErr = [ '<STR_LIT>' ] <EOL> if self . _name : <EOL> subErr . append ( "<STR_LIT>" ) <EOL> subErr . append ( "<STR_LIT>" if self . _isFeature else "<STR_LIT>" ) <EOL> subErr . append ( '<STR_LIT>' % self . _name ) <EOL> subErr . append ( "<STR_LIT:.>" ) <EOL> self . subErr = "<STR_LIT>" . join ( subErr ) <EOL> self . posErr = self . subErr . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def _subwriter ( self , name , isFeature ) : <EOL> """<STR_LIT>""" <EOL> return FilterFeatureWriter ( self . refs , name , isFeature ) <EOL> def _flattenRefs ( self , refs , flatRefs ) : <EOL> """<STR_LIT>""" <EOL> for ref in refs : <EOL> if type ( ref ) == list : <EOL> self . _flattenRefs ( ref , flatRefs ) <EOL> elif ref != "<STR_LIT:'>" : <EOL> flatRefs . append ( ref ) <EOL> def _checkRefs ( self , refs , errorMsg ) : <EOL> """<STR_LIT>""" <EOL> flatRefs = [ ] <EOL> self . _flattenRefs ( refs , flatRefs ) <EOL> for ref in flatRefs : <EOL> if ref [ - <NUM_LIT:1> ] == "<STR_LIT:'>" : <EOL> ref = ref [ : - <NUM_LIT:1> ] <EOL> if ref not in self . refs : <EOL> print errorMsg % ref <EOL> super ( FilterFeatureWriter , self ) . rawText ( "<STR_LIT:;>" ) <EOL> return False <EOL> return True <EOL> def classDefinition ( self , name , contents ) : <EOL> """<STR_LIT>""" <EOL> if name in self . refs : <EOL> return <EOL> newContents = [ ] <EOL> for ref in contents : <EOL> if ref not in self . refs and ref != "<STR_LIT:->" : <EOL> print self . classErr % ( ref , name ) <EOL> else : <EOL> newContents . append ( ref ) <EOL> self . refs . add ( name ) <EOL> super ( FilterFeatureWriter , self ) . classDefinition ( name , newContents ) <EOL> def gsubType1 ( self , target , replacement ) : <EOL> """<STR_LIT>""" <EOL> if self . _checkRefs ( [ target , replacement ] , self . subErr ) : <EOL> super ( FilterFeatureWriter , self ) . gsubType1 ( target , replacement ) <EOL> def gsubType4 ( self , target , replacement ) : <EOL> """<STR_LIT>""" <EOL> if self . _checkRefs ( [ target , replacement ] , self . subErr ) : <EOL> super ( FilterFeatureWriter , self ) . gsubType4 ( target , replacement ) <EOL> def gsubType6 ( self , precedingContext , target , trailingContext , replacement ) : <EOL> """<STR_LIT>""" <EOL> refs = [ precedingContext , target , trailingContext , replacement ] <EOL> if self . _checkRefs ( refs , self . subErr ) : <EOL> super ( FilterFeatureWriter , self ) . gsubType6 ( <EOL> precedingContext , target , trailingContext , replacement ) <EOL> def gposType1 ( self , target , value ) : <EOL> """<STR_LIT>""" <EOL> if self . _checkRefs ( [ target ] , self . posErr ) : <EOL> super ( FilterFeatureWriter , self ) . gposType1 ( target , value ) <EOL> def gposType2 ( self , target , value , needEnum = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _checkRefs ( target , self . posErr ) : <EOL> super ( FilterFeatureWriter , self ) . gposType2 ( target , value , needEnum ) <EOL> def gsubType3 ( self , target , replacement ) : <EOL> raise NotImplementedError <EOL> def feature ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name not in self . featureNames : <EOL> self . featureNames . add ( name ) <EOL> return super ( FilterFeatureWriter , self ) . feature ( name ) <EOL> return FDKSyntaxFeatureWriter ( name , True ) <EOL> def lookup ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name not in self . lookupNames : <EOL> self . lookupNames . add ( name ) <EOL> return super ( FilterFeatureWriter , self ) . lookup ( name ) <EOL> return FDKSyntaxFeatureWriter ( name , False ) <EOL> def languageSystem ( self , langTag , scriptTag ) : <EOL> """<STR_LIT>""" <EOL> system = ( langTag , scriptTag ) <EOL> if system not in self . languageSystems : <EOL> self . languageSystems . add ( system ) <EOL> super ( FilterFeatureWriter , self ) . languageSystem ( langTag , scriptTag ) <EOL> def table ( self , name , data ) : <EOL> """<STR_LIT>""" <EOL> if name in self . tableNames : <EOL> return <EOL> self . tableNames . add ( name ) <EOL> self . _instructions . append ( "<STR_LIT>" % name ) <EOL> self . _instructions . extend ( [ "<STR_LIT>" % line for line in data ] ) <EOL> self . _instructions . append ( "<STR_LIT>" % name ) <EOL> def compileFeatureRE ( name ) : <EOL> """<STR_LIT>""" <EOL> featureRE = list ( parser . featureContentRE ) <EOL> featureRE . insert ( <NUM_LIT:2> , name ) <EOL> featureRE . insert ( <NUM_LIT:6> , name ) <EOL> return re . compile ( "<STR_LIT>" . join ( featureRE ) ) <EOL> def updateFeature ( font , name , value ) : <EOL> """<STR_LIT>""" <EOL> featureRE = compileFeatureRE ( name ) <EOL> if featureRE . search ( font . features . text ) : <EOL> font . features . text = featureRE . sub ( value , font . features . text ) <EOL> else : <EOL> font . features . text += "<STR_LIT:\n>" + value <EOL> def readFeatureFile ( font , text , prepend = True ) : <EOL> """<STR_LIT>""" <EOL> writer = FilterFeatureWriter ( set ( font . keys ( ) ) ) <EOL> if prepend : <EOL> text += font . features . text <EOL> else : <EOL> text = font . features . text + text <EOL> parser . parseFeatures ( writer , text ) <EOL> font . features . text = writer . write ( ) <EOL> def writeFeatureFile ( font , path ) : <EOL> """<STR_LIT>""" <EOL> fout = open ( path , "<STR_LIT:w>" ) <EOL> fout . write ( font . features . text ) <EOL> fout . close ( ) </s>
