root/trunk/tools/extractLoc.py

Revision 3062, 3.6 kB (checked in by paul, 2 years ago)

Removed trunk/dabo/tools directory; moved those scripts to the exising trunk/tools directory.

  • Property svn:eol-style set to native
Line 
1 """ This script is designed to scan source directories and update the MySQL database
2 containing translation data on dabodev.com.
3 """
4 import os
5 import popen2
6 import MySQLdb
7
8 ### NOTE: you must get these values from a Dabo administrator before
9 ###   getting access to the database
10 db=MySQLdb.connect(host="XXX", user="XXX", passwd="XXX",
11         db="XXX")
12 crs = db.cursor(MySQLdb.cursors.DictCursor)
13
14
15 def processText(txt, proj, pth, fname, xtraPth):
16     print "** PROCESSING:", fname
17     updated = inserted = 0
18     # Each extracted string starts with '#: ', followed by the file and line
19     # number. The first element is the generated header, so discard that.
20     sections = txt.split("#: ")[1:]
21     for section in sections:
22         # Each section looks like this:
23         #--------------------------
24         # dTextBox.py:442
25         # msgid ""
26         # "Position of the beginning of the selected text. If no text is\n"
27         # "\t\t\tselected, returns the Position of the insertion cursor.  (int)"
28         # msgstr ""
29         #--------------------------
30        
31         # First, split off the 'msgstr' line
32         sect = section.split("msgstr \"\"")[0]
33        
34         try:
35             info, locString = sect.split("\nmsgid ")
36             junk, linenum = info.split(":")
37         except ValueError:
38             # This can happen when a string appears multiple times in a file
39             continue
40        
41         cleanLocString = cleanup(locString)
42         xtraSplit = os.path.split(xtraPth)[0]
43         # See if that string already exists
44         sql = """select pkid from originalstrings where cproject = %s
45                 and cdirectory = %s
46                 and cfilename = %s
47                 and nline = %s
48                 and tstring = %s"""
49         res = crs.execute(sql, (proj, xtraSplit, fname, linenum, cleanLocString))
50         if res:
51             # Update! Only set the first such occurrence to not deleted
52             # (there should only be one, anyway!
53             recs = crs.fetchall()
54             for rec in recs:
55                 crs.execute("""update originalstrings set ldeleted=0
56                         where pkid = %s""", rec["pkid"])
57                 updated += 1
58                 break
59         else:
60             sql = """insert into originalstrings (cproject, cdirectory,
61                 cfilename, nline, ldeleted, tstring) VALUES (%s, %s, %s, %s, %s, %s)"""
62             crs.execute(sql, (proj, xtraSplit, fname, linenum, 0, cleanLocString))
63             inserted += 1
64     return (updated, inserted)
65
66
67 def cleanup(val):
68     """Remove the gettext markup from the string."""
69     lns = val.splitlines()
70     newlns = []
71     for ln in lns:
72         if ln.startswith("\"") and ln.endswith("\""):
73             ln = ln[1:-1]
74         if ln.endswith("\\n"):
75             ln = ln[:-2]
76         newlns.append(ln)
77     return "\n".join(newlns)
78
79
80 def processLoc(proj, drct, xtra=None):
81     if xtra is None:
82         pth = drct
83         xtra = ""
84     else:
85         pth = os.path.join(drct, xtra)
86     flist = os.listdir(pth)
87     updated = inserted = 0
88     for fname in flist:
89         if fname.startswith("."):
90             # Hidden file; skip
91             continue
92         fullname = os.path.join(pth, fname)
93         newXtra = os.path.join(xtra, fname)
94         if os.path.isdir(fullname):
95             upd, ins = processLoc(proj, drct, newXtra)
96             updated += upd
97             inserted += ins
98         else:
99             if fname.endswith(".py"):
100                 out, inn, err = popen2.popen3("pygettext.py -a -o - %s" % fullname)
101                 upd, ins = processText(out.read(), proj, pth, fname, newXtra)
102                 updated += upd
103                 inserted += ins
104     return (updated, inserted)
105
106
107 def main():
108     # Mark the strings as deleted, in case they no longer exist
109     crs.execute("update originalstrings set ldeleted = 1")
110    
111     ### NOTE: This must be configured with your local paths
112     projects = {"dabo": "/path/to/dabo/",
113             "ide": "/path/to/ide/",
114             "demo": "/path/to/demo/"}
115     for project, drct in projects.items():
116         upd, ins = processLoc(project, drct)
117         print
118         print """Project %(project)s:
119     %(ins)s entries added
120     %(upd)s entries updated""" % locals()
121         print
122         print
123    
124
125 if __name__ == "__main__":
126     main()
Note: See TracBrowser for help on using the browser.