Update check_po to support msgctxt strings

This commit is contained in:
Nick Hall 2023-07-31 17:43:54 +01:00
parent 41720c5a7e
commit ab5d12bf34

View File

@ -276,9 +276,11 @@ class Msgid:
fuzzy_pat = re.compile( 'fuzzy' )
tips_xml_pat = re.compile( r'tips\.xml' )
def __init__( self, msgnr, lineno ):
self._msgctxt = [] # For debugging purpose the original text
self._msgid = [] # For debugging purpose the original text
self._msgidp = [] # For debugging purpose the original text
self._msgstr = [] # For debugging purpose the original text
self.msgctxt = ''
self.msgid = ''
self.msgidp = ''
self.msgstr = [] # This is a list to support plural
@ -299,6 +301,15 @@ class Msgid:
else:
sys.stdout.write( ''.join( self._msgstr ) )
def add_msgctxt( self, line, lineno ):
self._msgctxt.append( line )
line = re.sub( r'msgctxt\s+', '', line )
line = line.strip()
if line[0] != '"' or line[-1:] != '"':
print("ERROR at line %d: Missing quote." % lineno)
line = strip_quotes( line )
self.msgctxt += line
def add_msgid( self, line, lineno ):
self._msgid.append( line )
line = re.sub( r'msgid\s+', '', line )
@ -345,6 +356,7 @@ def create_new_Msgid( msgs, lineno ):
def read_msgs( fname ):
empty_pat = re.compile( r'^ \s* $', re.VERBOSE )
comment_pat = re.compile( r'\#', re.VERBOSE )
msgctxt_pat = re.compile( r'msgctxt \s+ "', re.VERBOSE )
msgid_pat = re.compile( r'msgid \s+ "', re.VERBOSE )
msgid_plural_pat = re.compile( r'msgid_plural \s+ "', re.VERBOSE )
msgstr_pat = re.compile( r'msgstr (\[\d\])? \s+ "', re.VERBOSE )
@ -357,6 +369,7 @@ def read_msgs( fname ):
# parse it like a statemachine
NONE = 'NONE' # Nothing detected, yet
CMNT = 'CMNT' # Inside comment part
MSGCTXT = 'msgctxt' # Inside msgctxt part
MSGID = 'msgid' # Inside msgid part
MSGIDP = 'msgid_plural' # Inside msgid_plural part
MSGSTR = 'msgstr' # Inside msgstr part
@ -380,6 +393,8 @@ def read_msgs( fname ):
next_state = OLD
elif comment_pat.match( line ):
next_state = CMNT
elif msgctxt_pat.match( line ):
next_state = MSGCTXT
elif msgid_pat.match( line ):
next_state = MSGID
elif msgid_plural_pat.match( line ):
@ -394,13 +409,19 @@ def read_msgs( fname ):
#print("%(state)d->%(next_state)d\t%(line)s" % vars())
if state == NONE:
# expect msgid or comment or old stuff
# expect msgctxt, msgid, comment or old stuff
if next_state == CMNT:
state = CMNT
# Start with an empty new item
msg = create_new_Msgid( msgs, lineno )
msg.add_cmnt( line )
elif next_state == MSGCTXT:
state = MSGCTXT
# Start with an empty new item
msg = create_new_Msgid( msgs, lineno )
msg.add_msgctxt( line, lineno )
elif next_state == MSGID:
state = MSGID
# Start with an empty new item
@ -429,7 +450,7 @@ def read_msgs( fname ):
'(state = %(state)s)' % vars() )
elif state == CMNT:
# Expect more comment, or msgid.
# Expect more comment, msgctxt, or msgid.
# If msgstr or string it is flagged as error.
if next_state == CMNT:
if msg:
@ -439,6 +460,13 @@ def read_msgs( fname ):
# Skip for now
pass
elif next_state == MSGCTXT:
state = MSGCTXT
if not msg:
# Start with an empty new item
msg = create_new_Msgid( msgs, lineno )
msg.add_msgctxt( line, lineno )
elif next_state == MSGID:
state = MSGID
if not msg:
@ -467,6 +495,43 @@ def read_msgs( fname ):
else:
raise CheckException( 'Unexpected state in po parsing '
'(state = %(state)s)' % vars() )
elif state == MSGCTXT:
# Expect more msgctxt or msgid.
# If msgstr or string it is flagged as error.
if next_state == CMNT:
# Hmmm. A comment here?
print('WARNING: Unexpted comment '
'at %(fname)s:%(lineno)d' % vars())
elif next_state == MSGCTXT:
raise CheckException( 'Unexpected %(next_state)s '
'at %(fname)s:%(lineno)d' % vars() )
elif next_state == MSGID:
state = MSGID
msg.add_msgid( line, lineno )
elif next_state == MSGIDP:
raise CheckException( 'Unexpected %(next_state)s '
'at %(fname)s:%(lineno)d' % vars() )
elif next_state == MSGSTR:
print('WARNING: Wild msgstr at %(fname)s:%(lineno)d' % vars())
state = MSGSTR
# Start with an empty new item
msg = create_new_Msgid( msgs, lineno )
msg.add_new_msgstr( line, lineno )
elif next_state == STR:
print('WARNING: Wild string at %(fname)s:%(lineno)d' % vars())
elif next_state == STR:
# Continuation of msgctxt, stay in state MSGCTXT
msg.add_msgctxt( line, lineno )
else:
raise CheckException( 'Unexpected state in po parsing '
'(state = %(state)s)' % vars() )
elif state == MSGID:
# Expect msgstr or msgid_plural or string
@ -475,6 +540,10 @@ def read_msgs( fname ):
print('WARNING: Unexpted comment '
'at %(fname)s:%(lineno)d' % vars())
elif next_state == MSGCTXT:
raise CheckException( 'Unexpected %(next_state)s '
'at %(fname)s:%(lineno)d' % vars() )
elif next_state == MSGID:
raise CheckException( 'Unexpected %(next_state)s '
'at %(fname)s:%(lineno)d' % vars() )
@ -506,6 +575,10 @@ def read_msgs( fname ):
print('WARNING: Unexpected comment '
'at %(fname)s:%(lineno)d' % vars())
elif next_state == MSGCTXT:
raise CheckException( 'Unexpected %(next_state)s '
'at %(fname)s:%(lineno)d' % vars() )
elif next_state == MSGID:
raise CheckException( 'Unexpected %(next_state)s '
'at %(fname)s:%(lineno)d' % vars() )
@ -538,6 +611,11 @@ def read_msgs( fname ):
msg = create_new_Msgid( msgs, lineno )
msg.add_cmnt( line )
elif next_state == MSGCTXT:
state = MSGCTXT
msg = create_new_Msgid( msgs, lineno )
msg.add_msgctxt( line, lineno )
elif next_state == MSGID:
state = MSGID
msg = create_new_Msgid( msgs, lineno )