Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def transfile_edit(request, pofile_id):
pofile = get_object_or_404(POFile, pk=pofile_id)
po_entries = pofile.object.trans.get_po_entries(pofile.filename)
filename = pofile.filename
component = pofile.object
lang_code = pofile.language_code
if not webtrans_is_under_max(pofile.total):
return permission_denied(request)
if request.method == "POST":
for fieldname, value in request.POST.items():
if 'msgid_field_' in fieldname:
nkey = fieldname.split('msgid_field_')[1]
if request.POST.get('changed_field_%s' % nkey, None) == 'True':
entry = po_entries.find(unescape(value))
#TODO: Find out why it's needed to remove it first
po_entries.remove(entry)
try:
string = request.POST['msgstr_field_%s' % nkey]
entry.msgstr = unescape(string);
except MultiValueDictKeyError:
has_plurals = True
index=0
while has_plurals:
plural_field = 'msgstr_field_%s_%s' % (nkey, index)
string = request.POST.get(plural_field, None)
if string is not None:
entry.msgstr_plural['%s' % index]=unescape(string)
else:
nkey = fieldname.split('msgid_field_')[1]
msgstr_field = 'msgstr_field_%s' % nkey
fuzzy_field = 'fuzzy_field_%s' % nkey
if msgstr_field in form.changed_data or \
fuzzy_field in form.changed_data:
msgid_value = form.cleaned_data['msgid_field_%s' % nkey]
entry = self.po_entries.find(unescape(msgid_value))
msgstr_value = form.cleaned_data['msgstr_field_%s' % nkey]
try:
entry.msgstr = unescape(msgstr_value)
except AttributeError:
for i, value in enumerate(msgstr_value):
entry.msgstr_plural['%s' % i]=unescape(value)
# Taking care of fuzzies flags
if form.cleaned_data.get('fuzzy_field_%s' % nkey, None):
if 'fuzzy' not in entry.flags:
entry.flags.append('fuzzy')
else:
if 'fuzzy' in entry.flags:
entry.flags.remove('fuzzy')
def update_po_entries(self, form):
"""
Update po_entries, which is a polib.POFile object, with the changed
form data.
"""
for fieldname in form.fields.keys():
if 'msgid_field_' in fieldname:
nkey = fieldname.split('msgid_field_')[1]
msgstr_field = 'msgstr_field_%s' % nkey
fuzzy_field = 'fuzzy_field_%s' % nkey
if msgstr_field in form.changed_data or \
fuzzy_field in form.changed_data:
msgid_value = form.cleaned_data['msgid_field_%s' % nkey]
entry = self.po_entries.find(unescape(msgid_value))
msgstr_value = form.cleaned_data['msgstr_field_%s' % nkey]
try:
entry.msgstr = unescape(msgstr_value)
except AttributeError:
for i, value in enumerate(msgstr_value):
entry.msgstr_plural['%s' % i]=unescape(value)
# Taking care of fuzzies flags
if form.cleaned_data.get('fuzzy_field_%s' % nkey, None):
if 'fuzzy' not in entry.flags:
entry.flags.append('fuzzy')
else:
if 'fuzzy' in entry.flags:
entry.flags.remove('fuzzy')
line = NSIWorkingFile.readline()
lineNo += 1
while line != '':
line = removeEscapedNewLine(line)
end = line.find('"')
if end != -1: #If we found the closing character, append
value += line[:end].lstrip()
break
else: #If not, append and continue
value += line.lstrip()
line=NSIWorkingFile.readline()
lineNo += 1
# Remove whitespace and new lines
value = value.strip("\t\n")
value = polib.unescape ( value )
if not value in LangStringCache:
LangStringCache[value] = []
# Note down our file and line number
LangStringCache[value].append([options.input,lineNo])
if not value in LangStringLabels:
LangStringLabels[value] = []
# Note down our label
LangStringLabels[value].append(label)
line=NSIWorkingFile.readline()
lineNo += 1
# Now, we loop through our cache and build PO entries for each
# We use PO comment field to store our NSIS labels, so we can decode it back later
for msgid,lineOccurances in LangStringCache.iteritems():
def validate(self, old, new):
new = unescape(new)
if len(new.strip()) == 0:
raise ValidationError(
_("Translation string only contains whitespaces.")
)
def validate(self, old, new):
old = unescape(old)
new = unescape(new)
for c in self.bracket_chars:
if new.count(c) != old.count(c):
raise ValidationError(
_("Translation string doesn't contain the same "
"number of '%s' as the source string." % c)
)
if not line:
continue
if line.startswith("#"):
continue
if line.startswith("msgid "):
if word:
yield self._glos.newEntry(word, defi)
wordCount += 1
word = ""
defi = ""
word = po_unescape(line[6:])
msgstr = False
elif line.startswith("msgstr "):
if msgstr:
log.error("msgid omitted!")
defi = po_unescape(line[7:])
msgstr = True
else:
if msgstr:
defi += po_unescape(line)
else:
word += po_unescape(line)
if word:
yield self._glos.newEntry(word, defi)
wordCount += 1
self._wordCount = wordCount
the source translation exists in the target translation, too.
Additionally, if there are positional keys, whether those
found in the source translation exist in the target translation,
as well.
We raise a ``ValidationError``, whenever one of the
conditions is not met.
Args:
source_trans: The source translation.
target_trans: The target translation.
Raises:
ValidationError, in case the translation is not valid.
"""
source_trans = unescape(source_trans)
target_trans = unescape(target_trans)
source_matches = list(self.printf_re.finditer(source_trans))
target_matches = list(self.printf_re.finditer(target_trans))
# We could use just one list comprehension:
#
# target_data = [
# (pattern.group('type'), pattern.group('key'))
# for pattern in target_matches
# ]
# target_specifiers, target_keys = map(
# list, zip(*target_data)
# ) or [[], []]
#
# but that would probably be less efficient, since target_matches
# should ususally have 0 - 5 elements, and much less readable.
# So, we do it in two steps.
if line.startswith("msgid "):
if word:
yield self._glos.newEntry(word, defi)
wordCount += 1
word = ""
defi = ""
word = po_unescape(line[6:])
msgstr = False
elif line.startswith("msgstr "):
if msgstr:
log.error("msgid omitted!")
defi = po_unescape(line[7:])
msgstr = True
else:
if msgstr:
defi += po_unescape(line)
else:
word += po_unescape(line)
if word:
yield self._glos.newEntry(word, defi)
wordCount += 1
self._wordCount = wordCount
So, we check, whether *every* conversion specifier found in
the source translation exists in the target translation, too.
Additionally, if there are positional keys, whether those
found in the source translation exist in the target translation,
as well.
We raise a ``ValidationError``, whenever one of the
conditions is not met.
Args:
source_trans: The source translation.
target_trans: The target translation.
Raises:
ValidationError, in case the translation is not valid.
"""
source_trans = unescape(source_trans)
target_trans = unescape(target_trans)
source_matches = list(self.printf_re.finditer(source_trans))
target_matches = list(self.printf_re.finditer(target_trans))
# We could use just one list comprehension:
#
# target_data = [
# (pattern.group('type'), pattern.group('key'))
# for pattern in target_matches
# ]
# target_specifiers, target_keys = map(
# list, zip(*target_data)
# ) or [[], []]
#
# but that would probably be less efficient, since target_matches
# should ususally have 0 - 5 elements, and much less readable.