Salome HOME
pb python3
[tools/eficas.git] / ts / tsparser.py
1
2 import os.path
3 from xml.etree import ElementTree as ET
4 from xml.etree.ElementTree import *
5 from xml.dom import minidom
6 import re
7
8 def get_file_name( theScript, theFileName, theExtension ):
9         anExt = '.'+theExtension
10         if theFileName=='':
11                 return theScript+anExt
12         if theFileName.lower().endswith( anExt ):
13                 return theFileName
14         else:
15                 return theFileName + anExt
16
17 class TSParser:
18         def __init__( self ):
19                 self.data = {}
20                 self.check_object = None # the function to check if the object is suitable for translation
21                 self.translation  = None # the function to translate the object's name
22                 self.sub_objects  = None # the function to get sub-objects of the given object
23                 self.not_translated = {}
24                 
25         def add( self, theContext, theSource, theTranslation ):
26                 if len(theSource)==0 or len(theTranslation)==0:
27                         return
28                 if not theContext in self.data:
29                         self.data[theContext] = {}
30                 self.data[theContext][theSource] = theTranslation
31
32         def saveXML( self, theFileName, theEncoding, theDocType, theRootElem ):
33                 aRoughRepr = ET.tostring( theRootElem, theEncoding )
34                 aDoc = minidom.parseString( aRoughRepr )
35                 aDocType = minidom.getDOMImplementation( '' ).createDocumentType( theDocType, '', '' )
36                 aDoc.insertBefore( aDocType, aDoc.documentElement )
37                 anXmlRepr = aDoc.toprettyxml( indent='  ' )
38                 anXmlLines = anXmlRepr.split( '\n' )[1:]
39                 anXmlRepr = '\n'.join( anXmlLines )
40                 anExpr = re.compile( '>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL )    
41                 anXmlRepr = anExpr.sub( '>\g<1></', anXmlRepr )
42                 
43                 #print anXmlRepr
44                 aFile = open( theFileName, 'w' )
45                 aFile.write( anXmlRepr )
46                 aFile.close()
47
48         def load( self, theFileName ):
49                 if not os.path.isfile( theFileName ):
50                         return
51                         
52                 aTree = ET.parse( theFileName )
53                 aRoot = aTree.getroot()
54                 for aContextElem in aRoot:
55                         aContext = aContextElem[0].text;
56                         if not aContext:
57                                 aContext = ''
58                         #print aContext
59                         for aMessageElem in aContextElem[1:]:
60                                 aSource = aMessageElem[0].text.strip()
61                                 aTranslation = aMessageElem[1].text.strip()
62                                 #print aSource
63                                 #print aTranslation
64                                 self.add( aContext, aSource, aTranslation )
65                 
66         def save( self, theFileName ):
67                 aRoot = Element( 'TS' )
68                 for aContext, aData in self.data.iteritems():
69                         aContextElem = SubElement( aRoot, 'context' )
70                         aName = SubElement( aContextElem, 'name' )
71                         aName.text = aContext
72                         for aSource, aTranslation in aData.iteritems():
73                                 aMessage = SubElement( aContextElem, 'message' )
74                                 aSourceElem = SubElement( aMessage, 'source' )
75                                 aSourceElem.text = aSource
76                                 aTranslationElem = SubElement( aMessage, 'translation' )
77                                 aTranslationElem.text = aTranslation
78                 
79                 self.saveXML( theFileName, 'utf-8', 'TS', aRoot )
80
81         def isOK( self, theName, theObject ):
82                 if theName.startswith( '__' ):
83                         return False
84                 if self.check_object:
85                         return self.check_object( theName, theObject )
86                 return True
87                 
88         def parse( self, theScript, theFileName = '' ):
89                 self.data = {}
90                 aFileName = get_file_name( theScript, theFileName, 'ts' )
91                 self.load( aFileName )
92                 aModule = __import__( theScript )
93                 self.parseObjects( aModule.__dict__, None, '' )
94                 self.save( aFileName )
95                 
96         def parseObjects( self, theDict, theParentObj, theParentName ):
97                 
98                 for aName, anObject in theDict.iteritems():
99                         
100                         if not self.isOK( aName, anObject ):
101                                 continue
102                                 
103                         if self.translation:
104                                 aContext, aTranslation = self.translation( anObject, aName, theParentObj, theParentName )
105                         else:
106                                 aContext = ''
107                                 aTranslation = aName
108                                 
109                         #print "Found:", aName
110                         if len( aTranslation ) > 0:
111                                 self.add( aContext, aName, aTranslation )
112                         elif self.isOK( '', anObject ) and len( aName ) > 0:
113                                 #print anObject, aName
114                                 self.not_translated[aName] = ''
115                                 
116                         aSubObjects = {}
117                         if self.sub_objects:
118                                 aSubObjects = self.sub_objects( anObject )
119                         self.parseObjects( aSubObjects, anObject, aName )
120
121         def saveNotTranslated( self, theFileName ):
122                 aFile = open( theFileName, 'w' )
123                 aNotTranslated = self.not_translated.keys()
124                 aNotTranslated.sort()
125                 for aKey in aNotTranslated:
126                         aFile.write( aKey + '\n' )
127                 aFile.close()