Skip to content

Commit e20aba1

Browse files
committed
Reduce complexity of PoFile.read function
1 parent 97b2a1a commit e20aba1

1 file changed

Lines changed: 75 additions & 48 deletions

File tree

msgcheck/po.py

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,74 @@ def check_spelling(self, spelling, checkers):
294294
return errors
295295

296296

297+
class Checker(object):
298+
"""Messages checker."""
299+
300+
def __init__(self):
301+
self.numline = 0
302+
self.numline_msgid = 0
303+
self.fuzzy = False,
304+
self.msgfuzzy = False
305+
self.noqa = False
306+
self.msgnoqa = False
307+
self.fmt = None
308+
self.msgfmt = None
309+
self.msg = {}
310+
self.msgcurrent = ''
311+
312+
def check_line(self, line):
313+
message = None
314+
self.numline += 1
315+
if not line:
316+
return None
317+
if line.startswith('#,'):
318+
self.fuzzy = 'fuzzy' in line
319+
match = re.search(r'([a-z-]+)-format', line, re.IGNORECASE)
320+
self.fmt = match.group(1) if match else None
321+
if line.startswith('#'):
322+
self.noqa = self.noqa or 'noqa' in line
323+
return None
324+
if line.startswith('msg'):
325+
match = re.match(
326+
r'([a-zA-Z0-9-_]+(\[\d+\])?)[ \t](.*)',
327+
line)
328+
if match:
329+
self.oldmsgcurrent = self.msgcurrent
330+
self.msgcurrent = match.group(1)
331+
line = match.group(3)
332+
if self.msgcurrent == 'msgid':
333+
if self.oldmsgcurrent.startswith('msgstr'):
334+
message = (
335+
self.numline_msgid,
336+
self.msgfuzzy,
337+
self.msgfmt,
338+
self.msgnoqa,
339+
self.msg,
340+
)
341+
self.msgfuzzy = self.fuzzy
342+
self.msgnoqa = self.noqa
343+
self.fuzzy = False
344+
self.noqa = False
345+
self.msgfmt = self.fmt
346+
self.fmt = None
347+
self.msg = {}
348+
self.numline_msgid = self.numline
349+
if self.msgcurrent and line.startswith('"'):
350+
self.msg[self.msgcurrent] = (self.msg.get(self.msgcurrent, '') +
351+
line[1:-1])
352+
return message
353+
354+
def last_check(self):
355+
if self.msgcurrent.startswith('msgstr'):
356+
return (
357+
self.numline_msgid,
358+
self.msgfuzzy,
359+
self.msgfmt,
360+
self.msgnoqa,
361+
self.msg,
362+
)
363+
364+
297365
class PoFile(object):
298366
"""
299367
A gettext file. It includes methods to read the file, and perform
@@ -333,56 +401,15 @@ def read(self): # pylint: disable=too-many-locals
333401
Read messages in PO file.
334402
"""
335403
self.msgs = []
336-
numline, numline_msgid = (0, 0)
337-
fuzzy, msgfuzzy = (False, False)
338-
noqa, msgnoqa = (False, False)
339-
fmt, msgfmt = (None, None)
340-
msg = {}
341-
msgcurrent = ''
404+
checker = Checker()
342405
with open(self.filename, 'r') as po_file:
343406
for line in po_file:
344-
numline += 1
345-
line = line.strip()
346-
if not line:
347-
continue
348-
if line.startswith('#,'):
349-
fuzzy = 'fuzzy' in line
350-
match = re.search(r'([a-z-]+)-format', line, re.IGNORECASE)
351-
fmt = match.group(1) if match else None
352-
if line.startswith('#'):
353-
noqa = noqa or 'noqa' in line
354-
continue
355-
if line.startswith('msg'):
356-
match = re.match(
357-
r'([a-zA-Z0-9-_]+(\[\d+\])?)[ \t](.*)',
358-
line)
359-
if match:
360-
oldmsgcurrent = msgcurrent
361-
msgcurrent = match.group(1)
362-
line = match.group(3)
363-
if msgcurrent == 'msgid':
364-
if oldmsgcurrent.startswith('msgstr'):
365-
self._add_message(numline_msgid,
366-
msgfuzzy,
367-
msgfmt,
368-
msgnoqa,
369-
msg)
370-
msgfuzzy = fuzzy
371-
msgnoqa = noqa
372-
fuzzy = False
373-
noqa = False
374-
msgfmt = fmt
375-
fmt = None
376-
msg = {}
377-
numline_msgid = numline
378-
if msgcurrent and line.startswith('"'):
379-
msg[msgcurrent] = msg.get(msgcurrent, '') + line[1:-1]
380-
if msgcurrent.startswith('msgstr'):
381-
self._add_message(numline_msgid,
382-
msgfuzzy,
383-
msgfmt,
384-
msgnoqa,
385-
msg)
407+
message = checker.check_line(line.strip())
408+
if message:
409+
self._add_message(*message)
410+
message = checker.last_check()
411+
if message:
412+
self._add_message(*message)
386413

387414
def compile(self):
388415
"""

0 commit comments

Comments
 (0)