📄 trans.py
字号:
#!/neo/opt/bin/pythonimport sys, string, os, getopt, pwd, signal, time, reimport fcntlimport tstartimport db_transfrom log import *import neo_cgi, neo_utilimport odbeTransError = "eTransError"DONE = 0DEBUG = 0TIER2_DIV = 11TIER1_DIV = 11 * TIER2_DIVif not DEBUG: LOGGING_STATUS[DEV_UPDATE] = 0def handleSignal(*arg): global DONE DONE = 1def usage(): print "usage info!!"def exceptionString(): import StringIO, traceback ## get the traceback message sfp = StringIO.StringIO() traceback.print_exc(file=sfp) exception = sfp.getvalue() sfp.close() return exceptionclass TransLoc: def __init__ (self, string_id, filename, location): self.string_id = string_id self.filename = filename self.location = locationclass Translator: _HTML_TAG_RE = None _HTML_TAG_REGEX = '<[^!][^>]*?>' _HTML_CMT_RE = None _HTML_CMT_REGEX = '<!--.*?-->' _CS_TAG_RE = None _CS_TAG_REGEX = '<\\?.+?\\?>' def __init__ (self): self.tdb = db_trans.trans_connect() # configuration data ...... # - we should stop hardcoding this... - jeske self.root = "testroot" self.languages = ['es', 'en'] self.ignore_paths = ['tmpl/m'] # common place for mockups self.ignore_files = ['blah_ignore.cs'] # ignore clearsilver file # ignore clearsilver javascript files self.ignore_patterns = ['tmpl/[^ ]*_js.cs'] # ............................ if self.root is None: raise "Unable to determine installation root" if Translator._HTML_TAG_RE is None: Translator._HTML_TAG_RE = re.compile(Translator._HTML_TAG_REGEX, re.MULTILINE | re.DOTALL) if Translator._HTML_CMT_RE is None: Translator._HTML_CMT_RE = re.compile(Translator._HTML_CMT_REGEX, re.MULTILINE | re.DOTALL) if Translator._CS_TAG_RE is None: Translator._CS_TAG_RE = re.compile(Translator._CS_TAG_REGEX, re.MULTILINE | re.DOTALL) self._html_state = 0 def parseHTMLTag(self, data): # this is only called if we see a full tag in one parse... i = 0 if len(data) == 0: return [] if data[0] in '/?': return [] while i < len(data) and data[i] not in ' \n\r\t>': i = i + 1 if i == len(data): return [] tag = data[:i].lower() #print "Searching tag: %s" % data #print "Found tag: %s" % tag results = [] attrfind = re.compile( r'\s*([a-zA-Z_][-.a-zA-Z_0-9]*)(\s*=\s*' r'(\'[^\']*\'|"[^"]*"|[^ \t\n<>]*))?') k = i attrs = {} attrs_beg = {} while k < len(data): match = attrfind.match(data, k) if not match: break attrname, rest, attrvalue = match.group(1, 2, 3) if not rest: attrvalue = attrname elif attrvalue[:1] == '\'' == attrvalue[-1:] or \ attrvalue[:1] == '"' == attrvalue[-1:]: attrvalue = attrvalue[1:-1] attrname = attrname.lower() if attrs.has_key(attrname): log("Can't handle duplicate attrs: %s" % attrname) attrs[attrname] = attrvalue attrs_beg[attrname] = match.start(3) k = match.end(0) find_l = [] if tag == "input": if attrs.get('type', "").lower() in ["submit", "button"]: find_l.append((attrs.get('value', ''), attrs_beg.get('value', 0))) for s,k in find_l: if s: x = data[k:].find(s) if x != -1: results.append((s, x+k, 1)) return results def parseHTML(self, data, reset=1): if reset: self._html_state = 0 if DEBUG: print "- %d ---------\n%s\n- E ---------" % (self._html_state, data) results = [] i = 0 n = len(data) # if we had state from the last parse... find it if self._html_state: if self._html_state == 2: x = string.find(data[i:], '-->') l = 3 else: x = string.find(data[i:], '>') l = 1 if x == -1: return results i = i + x + l self._html_state = 0 while i < n: if DEBUG: print "MATCHING>%s<MATCHING" % data[i:] cmt_b = string.find(data[i:], '<!--') cmt_e = string.find(data[i:], '-->') tag_b = string.find(data[i:], '<') tag_e = string.find(data[i:], '>') if DEBUG: print "B> %d %d %d %d <B" % (cmt_b, cmt_e, tag_b, tag_e) if cmt_b != -1 and cmt_b <= tag_b: x = i y = i+cmt_b-1 while x < y and data[x] in string.whitespace: x+=1 while y > x and data[y] in string.whitespace: y-=1 results.append((data[x:y+1], x, 1)) if cmt_e == -1: # partial comment: self._html_state = 2 break i = i + cmt_e + 3 elif tag_b != -1: x = i y = i+tag_b-1 while x < y and data[x] in string.whitespace: x+=1 while y > x and data[y] in string.whitespace: y-=1 results.append((data[x:y+1], x, 1)) if tag_e == -1: # partial tag self._html_state = 1 break h_results = self.parseHTMLTag(data[i+tag_b+1:i+tag_e]) h_results = map(lambda x: (x[0], x[1] + i+tag_b+1, x[2]), h_results) results = results + h_results i = i + tag_e + 1 else: x = i y = n-1 while x < y and data[x] in string.whitespace: x+=1 while y > x and data[y] in string.whitespace: y-=1 results.append((data[x:y+1], x, 1)) break return results def parseCS(self, data): results = [] i = 0 n = len(data) while i < n: m = Translator._CS_TAG_RE.search(data, i) if not m: # search for a partial... x = string.find(data[i:], '<?') if x == -1: results.append((data[i:], i)) else: results.append((data[i:x], i)) break (b, e) = m.span() if i != b: results.append((data[i:b], i)) i = e t_results = [] self._html_in = 0 for (s, ofs) in results: r = self.parseHTML(s, reset=0) r = map(lambda x: (x[0], x[1] + ofs, x[2]), r) t_results = t_results + r return t_results def descendHDF(self, obj, prefix): results = [] while obj is not None: if obj.value(): attrs = obj.attrs() attrs = map(lambda x: x[0], attrs) if "Lang" in attrs: if prefix: results.append((obj.value(), "%s.%s" % (prefix, obj.name()), 0)) else: results.append((obj.value(), "%s" % (obj.name()), 0)) if obj.child(): if prefix: results = results + self.descendHDF(obj.child(), "%s.%s" % (prefix, obj.name())) else: results = results + self.descendHDF(obj.child(), (obj.name())) obj = obj.next() return results def parseHDF(self, data): # Ok, we handle HDF files specially.. the theory is, we only # extract entire HDF elements which have the attribute Lang hdf = neo_util.HDF() hdf.readString(data, 1) return self.descendHDF(hdf, "") def handleFile(self, file): if file in self.ignore_files: return [] for a_re in self.ignore_patterns: if re.match(a_re,file): return [] fpath = self.root + '/' + file x = string.rfind(file, '.') if x == -1: return [] data = open(fpath, 'r').read() ext = file[x:] strings = [] if ext in ['.cst', '.cs']: strings = self.parseCS(data) elif ext in ['.html', '.htm']: strings = self.parseHTML(data) elif ext in ['.hdf']: strings = self.parseHDF(data) if len(strings): print "Found %d strings in %s" % (len(strings), file) return strings return [] def walkDirectory(self, path): if path in self.ignore_paths: return [] fpath = self.root + '/' + path files = os.listdir(fpath) dirs = [] results = [] for file in files: if file[0] == '.': continue fname = fpath + '/' + file if os.path.isdir(fname): dirs.append(file) else: strings = self.handleFile(path + '/' + file) if len(strings): results.append((path + '/' + file, strings)) for dir in dirs: if dir not in ["release"]: results = results + self.walkDirectory(path + '/' + dir) return results def cleanHtmlString(self, s): s = re.sub("\s+", " ", s) return string.strip(s) def containsWords(self, s, ishtml): if ishtml: s = string.replace(s, ' ', ' ') s = string.replace(s, '"', '"') s = string.replace(s, '©', '') s = string.replace(s, '<', '<') s = string.replace(s, '>', '>') s = string.replace(s, '&', '&') for x in range (len (s)): n = ord(s[x]) if (n>47 and n<58) or (n>64 and n<91) or (n>96 and n<123): return 1 return 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -