⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trans.py

📁 在 linux平台上的网页编程的模板
💻 PY
📖 第 1 页 / 共 2 页
字号:
    def findString(self, s):        rows = self.tdb.strings.fetchRows( ('string', s) )        if len(rows) == 0:            row = self.tdb.strings.newRow()            row.string = s            row.save()            return row.string_id        elif len(rows) > 1:            raise eTransError, "String %s exists multiple times!" % s        else:            return rows[0].string_id    def loadStrings(self, one_file=None, verbose=0):        if one_file is not None:            strings = self.handleFile(one_file)            results = [(one_file, strings)]        else:            results = self.walkDirectory('tmpl')        uniq = {}        cnt = 0        seen_hdf = {}        for fname, strings in results:            for (s, ofs, ishtml) in strings:                if s and string.strip(s):                    l = len(s)                    if ishtml:                        s = self.cleanHtmlString(s)                    if self.containsWords(s, ishtml):                        if type(ofs) == type(""): # HDF                            if seen_hdf.has_key(ofs):                                if seen_hdf[ofs][0] != s:                                    log("Duplicate HDF Name %s:\n\t file %s = %s\n\t file %s = %s" % (ofs, seen_hdf[ofs][1], seen_hdf[ofs][0], fname, s))                            else:                                seen_hdf[ofs] = (s, fname)                        try:                            uniq[s].append((fname, ofs, l))                        except KeyError:                            uniq[s] = [(fname, ofs, l)]                        cnt = cnt + 1        print "%d strings, %d unique" % (cnt, len(uniq.keys()))        fp = open("map", 'w')        for (s, locs) in uniq.items():            locs = map(lambda x: "%s:%s:%d" % x, locs)            fp.write('#: %s\n' % (string.join(locs, ',')))            fp.write('msgid=%s\n\n' % repr(s))        log("Loading strings/locations into database")        locations = []        for (s, locs) in uniq.items():            s_id = self.findString(s)            for (fname, ofs, l) in locs:                if type(ofs) == type(""): # ie, its HDF                    location = "hdf:%s" % ofs                else:                    location = "ofs:%d:%d" % (ofs, l)                loc_r = TransLoc(s_id, fname, location)                locations.append(loc_r)        return locations    def stringsHDF(self, prefix, locations, lang='en', exist=0, tiered=0):        hdf = neo_util.HDF()        if exist and lang == 'en': return hdf        done = {}        locations.sort()        maps = self.tdb.maps.fetchRows( ('lang', lang) )        maps_d = {}        for map in maps:            maps_d[int(map.string_id)] = map        strings = self.tdb.strings.fetchRows()        strings_d = {}        for string in strings:            strings_d[int(string.string_id)] = string        count = 0        for loc in locations:            s_id = int(loc.string_id)            if done.has_key(s_id): continue            try:                s_row = maps_d[s_id]                if exist: continue            except KeyError:                try:                    s_row = strings_d[s_id]                except KeyError:                    log("Missing string_id %d, skipping" % s_id)                    continue            count = count + 1            if tiered:                hdf.setValue("%s.%d.%d.%s" % (prefix, int(s_id) / TIER1_DIV, int(s_id) / TIER2_DIV, s_id), s_row.string)            else:                hdf.setValue("%s.%s" % (prefix, s_id), s_row.string)            done[s_id] = 1        if exist == 1: log("Missing %d strings for lang %s" % (count, lang))        return hdf    def dumpStrings(self, locations, lang=None):        log("Dumping strings to HDF")        if lang is None:            langs = ['en']            sql = "select lang from nt_trans_maps group by lang"            cursor = self.tdb.defaultCursor()            cursor.execute(sql)            rows = cursor.fetchall()            for row in rows:                langs.append(row[0])        else:            langs = [lang]        for a_lang in langs:            hdf = self.stringsHDF('S', locations, a_lang)            hdf.writeFile("strings_%s.hdf" % a_lang)        for a_lang in langs:            hdf = self.stringsHDF('S', locations, a_lang, exist=1)            if hdf.child():                hdf.writeFile("strings_missing_%s.hdf" % a_lang)    def fetchString(self, s_id, lang):        if lang == "hdf":            return "<?cs var:Lang.Extracted.%d.%d.%s ?>" % (int(s_id) / TIER1_DIV, int(s_id) / TIER2_DIV, s_id)        rows = self.tdb.maps.fetchRows( [('string_id', s_id), ('lang', lang)] )        if len(rows) == 0:            try:                row = self.tdb.strings.fetchRow( ('string_id', s_id) )            except odb.eNoMatchingRows:                log("Unable to find string id %s" % s_id)                raise eNoString            if lang != 'en':                log("Untranslated string for id %s" % s_id)            return row.string        else:            return rows[0].string    def dumpFiles(self, locations, lang):        log("Dumping files for %s" % lang)        files = {}        for row in locations:            try:                files[row.filename].append(row)            except KeyError:                files[row.filename] = [row]        hdf_map = []        os.system("rm -rf %s/gen/tmpl" % (self.root))        for file in files.keys():            fname = "%s/gen/%s" % (self.root, file)            try:                os.makedirs(os.path.dirname(fname))            except OSError, reason:                if reason[0] != 17:                    raise            do_hdf = 0            x = string.rfind(file, '.')            if x != -1 and file[x:] == '.hdf':                do_hdf = 1            ofs = []            for loc in files[file]:                parts = string.split(loc.location, ':')                if len(parts) == 3 and parts[0] == 'ofs' and do_hdf == 0:                     ofs.append((int(parts[1]), int(parts[2]), loc.string_id))                elif len(parts) == 2 and parts[0] == 'hdf' and do_hdf == 1:                    hdf_map.append((parts[1], loc.string_id))                else:                    log("Invalid location for loc_id %s" % loc.loc_id)                    continue            if not do_hdf:                ofs.sort()                data = open(self.root + '/' + file).read()                # ok, now we split up the original data into sections                x = 0                n = len(data)                out = []                #sys.stderr.write("%s\n" % repr(ofs))                while len(ofs):                    if ofs[0][0] > x:                        out.append(data[x:ofs[0][0]])                        x = ofs[0][0]                    elif ofs[0][0] == x:                        out.append(self.fetchString(ofs[0][2], lang))                        x = ofs[0][0] + ofs[0][1]                        ofs = ofs[1:]                    else:                        log("How did we get here? %s x=%d ofs=%d sid=%d" % (file, x, ofs[0][0], ofs[0][2]))                        log("Data[x:20]: %s" % data[x:20])                        log("Data[ofs:20]: %s" % data[ofs[0][0]:20])                        break                if n > x:                    out.append(data[x:])                odata = string.join(out, '')                open(fname, 'w').write(odata)        if lang == "hdf":            langs = self.languages        else:            langs = [lang]        for d_lang in langs:          # dumping the extracted strings          hdf = self.stringsHDF('Lang.Extracted', locations, d_lang, tiered=1)          fname = "%s/gen/tmpl/lang_%s.hdf" % (self.root, d_lang)          hdf.writeFile(fname)          data = open(fname).read()          fp = open(fname, 'w')          fp.write('## AUTOMATICALLY GENERATED -- DO NOT EDIT\n\n')          fp.write(data)          fp.write('\n#include "lang_map.hdf"\n')          # dumping the hdf strings file          if d_lang == "en":            map_file = "%s/gen/tmpl/lang_map.hdf" % (self.root)          else:            map_file = "%s/gen/tmpl/%s/lang_map.hdf" % (self.root, d_lang)          try:              os.makedirs(os.path.dirname(map_file))          except OSError, reason:               if reason[0] != 17: raise          map_hdf = neo_util.HDF()          for (name, s_id) in hdf_map:              str = hdf.getValue('Lang.Extracted.%d.%d.%s' % (int(s_id) / TIER1_DIV, int(s_id) / TIER2_DIV, s_id), '')              map_hdf.setValue(name, str)          map_hdf.writeFile(map_file)    def loadMap(self, file, prefix, lang):        log("Loading map for language %s" % lang)        hdf = neo_util.HDF()        hdf.readFile(file)        obj = hdf.getChild(prefix)        updates = 0        new_r = 0        while obj is not None:            s_id = obj.name()            str = obj.value()            try:                map_r = self.tdb.maps.fetchRow( [('string_id', s_id), ('lang', lang)])            except odb.eNoMatchingRows:                map_r = self.tdb.maps.newRow()                map_r.string_id = s_id                map_r.lang = lang                new_r = new_r + 1            if map_r.string != str:                updates = updates + 1                map_r.string = str                map_r.save()            obj = obj.next()        log("New maps: %d  Updates: %d" % (new_r, updates - new_r))        def main(argv):  alist, args = getopt.getopt(argv[1:], "f:v:", ["help", "load=", "lang="])  one_file = None  verbose = 0  load_file = None  lang = 'en'  for (field, val) in alist:    if field == "--help":      usage(argv[0])      return -1    if field == "-f":      one_file = val    if field == "-v":      verbose = int(val)    if field == "--load":        load_file = val    if field == "--lang":        lang = val          global DONE  #signal.signal(signal.SIGTERM, handleSignal)  #signal.signal(signal.SIGINT, handleSignal)  log("trans: start")  start_time = time.time()  try:    t = Translator()    if load_file:        t.loadMap(load_file, 'S', lang)    else:        locations = t.loadStrings(one_file, verbose=verbose)        t.dumpStrings(locations)        t.dumpFiles(locations, 'hdf')  except KeyboardInterrupt:    pass  except:    import handle_error    handle_error.handleException("Translation Error")if __name__ == "__main__":  main(sys.argv)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -