Salome HOME
merge avec les devloppts de l ete
[tools/eficas.git] / OldCodes / ts / dicoparser.py
1
2 import shlex
3
4 DICO_EXT = '.dico'
5
6 class DicoParser:
7         def __init__( self, theFileName, theStartAttr, theIndexAttr ):
8                 if theFileName=='':
9                         return
10                 if not theFileName.endswith( DICO_EXT ):
11                         theFileName += DICO_EXT
12                 aFile = open( theFileName )
13                 self.data = self.parse( aFile.read(), theStartAttr, theIndexAttr )
14                 aFile.close()
15                 
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 )
21                 
22         def parse_tokens( self, theText ):
23                 theText = theText.replace( "''", "`" )
24                 aLexer = shlex.shlex( theText )
25                 aLexer.commenters = '/'
26                 aLexer.quotes = '\'"'
27                 aLexer.wordchars = aLexer.wordchars + '-.'
28                 aTokenList = []
29                 for aToken in aLexer:
30                         #print aToken
31                         if aToken[0]=="'" and aToken[-1]=="'":
32                                 aToken = aToken[1:-1]
33                         elif aToken[0]=='"' and aToken[-1]=='"':
34                                 aToken = aToken[1:-1]
35                         if aToken=='`':
36                                 aToken = ''
37                         aToken = aToken.replace( "`", "'" )
38                         aToken = aToken.replace( "\n", "" )
39                         aToken = self.simplify_spaces( aToken )
40                         #print aToken
41                         aTokenList.append( aToken )
42                 return aTokenList
43
44         def simplify_spaces( self, theToken ):
45                 return ' '.join( theToken.split() )
46
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 ):
53                                         print theList[i]
54                                 theList[i] = aSubItemsList[0] # we assume that only one '=' in the subitem
55
56         def convert_to_tuples( self, theList ):
57                 aPairsList = []
58                 aKey = ''
59                 i = 0
60                 n = len( theList )
61                 while i<n:
62                         if theList[i]=='=' and i<n-1:
63                                 aPairsList.append( (aKey, theList[i+1]) )
64                                 i = 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] )
69                                 else:
70                                         aValue = [aValue, theList[i+1]]
71                                 aPairsList[-1] = ( aKey, aValue )
72                                 i = i + 1
73                         else:
74                                 aKey = theList[i]
75                         i = i + 1
76                 return aPairsList
77
78         def convert_to_blocks( self, theTuples, theStartAttr, theIndexAttr ):
79                 aData = {}
80                 aBlockKey = ''
81                 aBlock = {}
82                 for aKey, aValue in theTuples:
83                         #print aKey
84                         if aKey==theStartAttr:
85                                 #print "__start__"
86                                 if len(aBlock)>0:
87                                         aData[aBlockKey] = aBlock
88                                 aBlockKey = ''
89                                 aBlock = {}
90                         elif aKey==theIndexAttr:
91                                 #print "__index__"
92                                 aBlockKey = aValue
93                                 #print aBlockKey
94                         aBlock[aKey] = aValue
95                         #print aData
96                 if len(aBlock)>0:
97                         aData[aBlockKey] = aBlock
98                 return aData
99
100         def are_equal( self, theStrDico, theStrCata ):
101                 return theStrDico == self.to_dico_str( theStrCata )
102                 
103         def to_dico_str( self, theStrCata ):
104                 aCata = theStrCata.replace( '_', ' ' )
105                 aCata = aCata.upper()
106                 return aCata
107
108         def to_cata_str( self, theStrDico ):
109                 aWordsList = theStrDico.split()
110                 aCata = []
111                 for aWord in aWordsList:
112                         aWord = aWord.lower()
113                         aWord = aWord[0].upper() + aWord[1:]
114                         aCata.append( aWord )
115                 return ' '.join( aCata )
116                 
117         def search_in_block( self, theBlock, theAttrTr ):
118                 #print 'search_in_block:', theAttrTr
119                 return theBlock[theAttrTr]
120                 
121         def search( self, theIndexText, theAttrTr, theChoiceText, theAttrCh, theAttrChTr ):
122                 anIndexText = self.to_dico_str( theIndexText )
123                 if not anIndexText in self.data:
124                         return ''
125                 
126                 if theAttrCh=='':
127                         return self.search_in_block( self.data[anIndexText], theAttrTr )
128                         
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 )
133                 else:
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:
139                         return aDataChTr
140                 
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 ):
145                                 aKey = ''
146                                 aValue = aDataCh[i]
147                         #print aKey, aValue
148                         if aValue==aChoiceText:
149                                 if isinstance( aDataChTr[i], tuple ):
150                                         aKeyTr, aValueTr = aDataChTr[i]
151                                 elif isinstance( aDataChTr[i], basestring ):
152                                         aKeyTr = ''
153                                         aValueTr = aDataChTr[i]
154                                 return aValueTr
155                 return ''
156
157         def translate( self, theIndexText, theAttrTr, theChoiceText='', theAttrCh='', theAttrChTr='' ):
158                 aTrText = self.search( theIndexText, theAttrTr, theChoiceText, theAttrCh, theAttrChTr )
159                 #print aTrText
160                 return self.to_cata_str( aTrText )