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