Add smack rule
[apps/core/preloaded/ug-camera-efl.git] / po / htm2po.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 import sys, re
4 from BeautifulSoup import BeautifulSoup
5
6 help = """
7 arg 1 : input file
8 arg 2 : the locale field which you wanna extract
9 arg 3 : output po file
10 """
11
12 DESIGN_ID = "Design ID"
13 dString = {}
14
15 def findindexof( word, tds ):
16         for i, td in enumerate(tds):
17                 try:
18                         if td.contents[0]==word:
19                                 return i
20                 except IndexError:
21                         pass
22         return -1
23
24 if __name__=="__main__":
25         if len(sys.argv)>3:
26                 in_file = sys.argv[1]
27                 locale = sys.argv[2]
28                 po_file = sys.argv[3]
29         else:
30                 print help
31                 sys.exit()
32
33         print 'input file :', in_file
34         print 'locale :', locale
35         print 'po file :', po_file
36
37         f = open( in_file, 'rt' )
38         soup = BeautifulSoup( f.read() )
39         f.close()
40
41         for i, tr in enumerate(soup('tr')):
42                 tds = tr('td')
43                 if i==0: 
44                         iID = findindexof( DESIGN_ID, tds )
45                         iLocale = findindexof( locale, tds )
46                         print 'index of design id :',iID, ", index of",locale,":", iLocale
47                         if iID<0 or iLocale<0: 
48                                 print 'index failed'
49                                 sys.exit()
50                         continue
51
52                 bPass = True;
53                 for j,td in enumerate(tds):
54                         try:
55                                 if j==iID: ID = td.contents[0]  
56                                 elif j==iLocale: szLocale = td.contents[0]
57                         except IndexError:
58                                 bPass = False;
59                                 pass
60
61                 if bPass: 
62                         dString[ID] = szLocale
63                         #print i, '\t', ID, '\t' ,szLocale
64
65         #modify po file
66         f = open( po_file, 'rt' )
67         po = f.read().decode('utf-8')
68         f.close()
69
70         for k, v in dString.items():
71                 #find key
72                 msg = 'msgid "'+ k + '"'
73                 sp = po.find( msg )
74
75                 #if yes, replace
76                 if sp>-1:
77                         lines = po.split('\n')
78                         for i, line in enumerate(lines):
79                                 if line.find( msg )>-1:
80                                         #replace next line
81                                         print 'replace :',k
82                                         v = v.replace('\r', '')                 #remove linefeed
83                                         v = v.replace('\n ', '\\n') #remove carriage return
84                                         lines[i+1] = 'msgstr "' + v + '"'
85                         po = "\n".join( lines )
86
87                 #if no, append
88                 #if sp<0:       
89                 #       print 'append', k
90                 #       po += 'msgid "' + k + '"\n'
91                 #       po += 'msgstr "' + v + '"\n\n' 
92
93         newfile = po_file+'.new'
94         f = open( newfile , 'wt' )
95         f.write( po.encode('utf-8') )
96         f. close()
97         print newfile, 'is created'