7 def __init__( self, theFileName, theStartAttr, theIndexAttr ):
10 if not theFileName.endswith( DICO_EXT ):
11 theFileName += DICO_EXT
12 aFile = open( theFileName )
13 self.data = self.parse( aFile.read(), theStartAttr, theIndexAttr )
16 def parse( self, theText, theStartAttr, theIndexAttr ):
17 aTokenList = self.parse_tokens( theText )
18 self.expand_values( aTokenList )
19 aList = self.convert_to_tuples( aTokenList )
20 return self.convert_to_blocks( aList, theStartAttr, theIndexAttr )
22 def parse_tokens( self, theText ):
23 theText = theText.replace( "''", "`" )
24 aLexer = shlex.shlex( theText )
25 aLexer.commenters = '/'
27 aLexer.wordchars = aLexer.wordchars + '-.'
31 if aToken[0]=="'" and aToken[-1]=="'":
33 elif aToken[0]=='"' and aToken[-1]=='"':
37 aToken = aToken.replace( "`", "'" )
38 aToken = aToken.replace( "\n", "" )
39 aToken = self.simplify_spaces( aToken )
41 aTokenList.append( aToken )
44 def simplify_spaces( self, theToken ):
45 return ' '.join( theToken.split() )
47 def expand_values( self, theList ):
48 for i in xrange( 0, len( theList ) ):
49 if '=' in theList[i] and theList[i]!='=':
50 aTokenList = self.parse_tokens( theList[i] )
51 aSubItemsList = self.convert_to_tuples( aTokenList )
52 if( len( aSubItemsList ) == 0 ):
54 theList[i] = aSubItemsList[0] # we assume that only one '=' in the subitem
56 def convert_to_tuples( self, theList ):
62 if theList[i]=='=' and i<n-1:
63 aPairsList.append( (aKey, theList[i+1]) )
65 elif theList[i]==';' and len( aPairsList ) > 0 and i<n-1:
66 aKey, aValue = aPairsList[-1]
67 if isinstance( aValue, list ):
68 aValue.append( theList[i+1] )
70 aValue = [aValue, theList[i+1]]
71 aPairsList[-1] = ( aKey, aValue )
78 def convert_to_blocks( self, theTuples, theStartAttr, theIndexAttr ):
82 for aKey, aValue in theTuples:
84 if aKey==theStartAttr:
87 aData[aBlockKey] = aBlock
90 elif aKey==theIndexAttr:
97 aData[aBlockKey] = aBlock
100 def are_equal( self, theStrDico, theStrCata ):
101 return theStrDico == self.to_dico_str( theStrCata )
103 def to_dico_str( self, theStrCata ):
104 aCata = theStrCata.replace( '_', ' ' )
105 aCata = aCata.upper()
108 def to_cata_str( self, theStrDico ):
109 aWordsList = theStrDico.split()
111 for aWord in aWordsList:
112 aWord = aWord.lower()
113 aWord = aWord[0].upper() + aWord[1:]
114 aCata.append( aWord )
115 return ' '.join( aCata )
117 def search_in_block( self, theBlock, theAttrTr ):
118 #print 'search_in_block:', theAttrTr
119 return theBlock[theAttrTr]
121 def search( self, theIndexText, theAttrTr, theChoiceText, theAttrCh, theAttrChTr ):
122 anIndexText = self.to_dico_str( theIndexText )
123 if not anIndexText in self.data:
127 return self.search_in_block( self.data[anIndexText], theAttrTr )
129 aDataCh = self.search_in_block( self.data[anIndexText], theAttrCh )
130 aDataChTr = self.search_in_block( self.data[anIndexText], theAttrChTr )
131 if isinstance( theChoiceText, basestring ):
132 aChoiceText = self.to_dico_str( theChoiceText )
134 aChoiceText = theChoiceText
135 #print 'Choice text:', aChoiceText
136 #print 'Choice data:', aDataCh
137 #print 'Choice tr data:', aDataChTr
138 if isinstance( aDataCh, basestring ) and aDataCh==aChoiceText:
141 for i in xrange( 0, len(aDataCh) ):
142 if isinstance( aDataCh[i], tuple ):
143 aKey, aValue = aDataCh[i]
144 elif isinstance( aDataCh[i], basestring ):
148 if aValue==aChoiceText:
149 if isinstance( aDataChTr[i], tuple ):
150 aKeyTr, aValueTr = aDataChTr[i]
151 elif isinstance( aDataChTr[i], basestring ):
153 aValueTr = aDataChTr[i]
157 def translate( self, theIndexText, theAttrTr, theChoiceText='', theAttrCh='', theAttrChTr='' ):
158 aTrText = self.search( theIndexText, theAttrTr, theChoiceText, theAttrCh, theAttrChTr )
160 return self.to_cata_str( aTrText )