def findunit(self, source): # find using hash instead of index source_hash = md5_f(source.encode("utf-8")).hexdigest() try: return self.units.get(source_hash=source_hash) except Unit.DoesNotExist: return None
def convertunit(self, unit, prefix): if self.ignorefunc: if self.ignorefunc(unit): return unit if prefix.find("@hash_placeholder@") != -1: if unit.getlocations(): hashable = unit.getlocations()[0] else: hashable = unit.source prefix = prefix.replace( "@hash_placeholder@", hash.md5_f(hashable).hexdigest()[:self.hash_len]) rich_source = unit.rich_source if not isinstance(rich_source, StringElem): rich_source = [ rich_parse(string, podebug_parsers) for string in rich_source ] if self.rewritefunc: rewritten = [self.rewritefunc(string) for string in rich_source] if rewritten: unit.rich_target = rewritten elif not unit.istranslated(): unit.rich_target = unit.rich_source unit.rich_target = add_prefix(prefix, unit.rich_target) return unit
def phprep(self, text): """Replaces all instances of PHP with placeholder tags, and returns the new text and a dictionary of tags. The current implementation replaces <?foo?> with <?md5(foo)?>. The hash => code conversions are stored in self.phpdict for later use in restoring the real PHP. The purpose of this is to remove all potential "tag-like" code from inside PHP. The hash looks nothing like an HTML tag, but the following PHP:: $a < $b ? $c : ($d > $e ? $f : $g) looks like it contains an HTML tag:: < $b ? $c : ($d > to nearly any regex. Hence, we replace all contents of PHP with simple strings to help our regexes out. """ from translate.misc import hash self.phpdict = {} result = re.findall('(?s)<\?(.*?)\?>', text) for cmd in result: h = hash.md5_f(cmd).hexdigest() self.phpdict[h] = cmd text = text.replace(cmd, h) return text
def findid(self, id): if hasattr(self, "id_index"): return self.id_index.get(id, None) unitid_hash = md5_f(id.encode("utf-8")).hexdigest() try: return self.units.get(unitid_hash=unitid_hash) except Unit.DoesNotExist: return None
def findunits(self, source): if hasattr(self, "sourceindex"): return super(Store, self).findunit(source) # find using hash instead of index source_hash = md5_f(source.encode("utf-8")).hexdigest() units = self.units.filter(source_hash=source_hash) if units.count(): return units
def update(self, unit): """update indb translation from file""" changed = False if ( self.source != unit.source or len(self.source.strings) != stringcount(unit.source) or self.hasplural() != unit.hasplural() ): if unit.hasplural() and len(unit.source.strings) == 1: self.source = [unit.source, PLURAL_PLACEHOLDER] else: self.source = unit.source changed = True if self.target != unit.target or len(self.target.strings) != stringcount(unit.target): notempty = filter(None, self.target_f.strings) self.target = unit.target if filter(None, self.target_f.strings) or notempty: # FIXME: we need to do this cause we discard nplurals # for empty plurals changed = True notes = unit.getnotes(origin="developer") if self.developer_comment != notes and (self.developer_comment or notes): self.developer_comment = notes or None changed = True notes = unit.getnotes(origin="translator") if self.translator_comment != notes and (self.translator_comment or notes): self.translator_comment = notes or None changed = True locations = "\n".join(unit.getlocations()) if self.locations != locations and (self.locations or locations): self.locations = locations or None changed = True context = unit.getcontext() if self.context != unit.getcontext() and (self.context or context): self.context = context or None changed = True if self.isfuzzy() != unit.isfuzzy(): self.markfuzzy(unit.isfuzzy()) changed = True if self.isobsolete() != unit.isobsolete(): if unit.isobsolete(): self.makeobsolete() else: self.resurrect() changed = True if self.unitid != unit.getid(): self.unitid = unicode(unit.getid()) or unicode(unit.source) self.unitid_hash = md5_f(self.unitid.encode("utf-8")).hexdigest() changed = True if hasattr(unit, "getalttrans"): for suggestion in unit.getalttrans(): if suggestion.source == self.source: self.add_suggestion(suggestion.target, touch=False) changed = True return changed
def findunits(self, source, obsolete=False): if not obsolete and hasattr(self, "sourceindex"): return super(Store, self).findunits(source) # find using hash instead of index source_hash = md5_f(source.encode("utf-8")).hexdigest() units = self.unit_set.filter(source_hash=source_hash) if obsolete: units = units.filter(state=OBSOLETE) else: units = units.filter(state__gt=OBSOLETE) if units.count(): return units
def update(self, unit): """update indb translation from file""" changed = False if self.hasplural() != unit.hasplural(): self.source = unit.source self.target = unit.target changed = True else: if self.source != unit.source: self.source = unit.source changed = True if self.target != unit.target: wordcount = self.target_wordcount self.target = unit.target if not (wordcount == count_words(self.target_f.strings) == 0): #FIXME: we need to do this cause we discard nplurals for empty plurals changed = True notes = unit.getnotes(origin="developer") if self.developer_comment != notes: self.developer_comment = notes changed = True notes = unit.getnotes(origin="translator") if self.translator_comment != notes: self.translator_comment = notes changed = True locations = "\n".join(unit.getlocations()) if self.locations != locations: self.locations = locations changed = True if self.context != unit.getcontext(): self.context = unit.getcontext() changed = True if self.isfuzzy() != unit.isfuzzy(): self.markfuzzy(unit.isfuzzy()) changed = True if self.isobsolete() != unit.isobsolete(): if unit.isobsoltete(): self.makeobsolete() else: self.resurrect() changed = True if self.unitid != unit.getid(): self.unitid = unit.getid() self.unitid_hash = md5_f(self.unitid.encode("utf-8")).hexdigest() changed = True if hasattr(unit, 'getalttrans'): for suggestion in unit.getalttrans(): if suggestion.source == self.source: self.add_suggestion(suggestion.target, touch=False) changed = True return changed
def save(self, *args, **kwargs): if self._source_updated: # update source related fields self.source_hash = md5_f(self.source_f.encode("utf-8")).hexdigest() self.source_wordcount = count_words(self.source_f.strings) self.source_length = len(self.source_f) if self._target_updated: # update target related fields self.target_wordcount = count_words(self.target_f.strings) self.target_length = len(self.target_f) if filter(None, self.target_f.strings): if self.state == UNTRANSLATED: self.state = TRANSLATED elif self.state > FUZZY: self.state = UNTRANSLATED super(Unit, self).save(*args, **kwargs) if ( settings.AUTOSYNC and self.store.file and self.store.state >= PARSED and (self._target_updated or self._source_updated) ): # FIXME: last translator information is lost self.sync(self.getorig()) self.store.update_store_header() self.store.file.savestore() if self.store.state >= CHECKED and (self._source_updated or self._target_updated): # FIXME: are we sure only source and target affect quality checks? self.update_qualitychecks() # done processing source/target update remove flag self._source_updated = False self._target_updated = False if self.store.state >= PARSED: # updated caches store = self.store # translation_project = store.translation_project # translation_project.update_index( # translation_project.indexer, store, self.id) deletefromcache(store, ["getquickstats", "getcompletestats", "get_mtime", "has_suggestions"])
def convertunit(self, unit, prefix): if self.ignorefunc: if self.ignorefunc(unit): return unit if prefix.find("@hash_placeholder@") != -1: if unit.getlocations(): hashable = unit.getlocations()[0] else: hashable = unit.source prefix = prefix.replace("@hash_placeholder@", hash.md5_f(hashable).hexdigest()[:self.hash_len]) rich_source = unit.rich_source if not isinstance(rich_source, StringElem): rich_source = [rich_parse(string, podebug_parsers) for string in rich_source] if self.rewritefunc: rewritten = [self.rewritefunc(string) for string in rich_source] if rewritten: unit.rich_target = rewritten elif not unit.istranslated(): unit.rich_target = unit.rich_source unit.rich_target = add_prefix(prefix, unit.rich_target) return unit
def setid(self, value): self.unitid = value self.unitid_hash = md5_f(self.unitid.encode("utf-8")).hexdigest()
def _set_target(self, value): self.target_f = value self.target_hash = md5_f(self.target_f.encode("utf-8")).hexdigest()
def get_email_hash(self): return md5_f(self.user.email).hexdigest()