#NEWLINE# def read_mo( fileobj ) : #NEWLINE# #INDENT# 'Read a binary MO file from the given file-like object and return a\n    corresponding `Catalog` object.\n\n    :param fileobj: the file-like object to read the MO file from\n\n    :note: The implementation of this function is heavily based on the\n           ``GNUTranslations._parse`` method of the ``gettext`` module in the\n           standard library.\n    ' #NEWLINE# catalog = Catalog ( ) #NEWLINE# headers = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# filename = getattr ( fileobj , 'name' , '' ) #NEWLINE# buf = fileobj . read ( ) #NEWLINE# buflen = len ( buf ) #NEWLINE# unpack = struct . unpack #NEWLINE# magic = unpack ( '<I' , buf [ : 4 ] ) [ 0 ] #NEWLINE# if ( magic == LE_MAGIC ) : #NEWLINE# #INDENT# ( version , msgcount , origidx , transidx ) = unpack ( '<4I' , buf [ 4 : 20 ] ) #NEWLINE# ii = '<II' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( magic == BE_MAGIC ) : #NEWLINE# #INDENT# ( version , msgcount , origidx , transidx ) = unpack ( '>4I' , buf [ 4 : 20 ] ) #NEWLINE# ii = '>II' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise IOError ( 0 , 'Bad magic number' , filename ) #NEWLINE# #UNINDENT# #UNINDENT# for i in range_type ( 0 , ii ) : #NEWLINE# #INDENT# ( mlen , moff ) = unpack ( ii , buf [ origidx : ( origidx + 8 ) ] ) #NEWLINE# mend = ( moff + mlen ) #NEWLINE# ( tlen , toff ) = unpack ( ii , buf [ transidx : ( transidx + 8 ) ] ) #NEWLINE# tend = ( toff + tlen ) #NEWLINE# if ( ( mend < buflen ) and ( tend < buflen ) ) : #NEWLINE# #INDENT# msg = buf [ moff : mend ] #NEWLINE# tmsg = buf [ toff : tend ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise IOError ( 0 , 'File is corrupt' , filename ) #NEWLINE# #UNINDENT# if ( mlen == 0 ) : #NEWLINE# #INDENT# lastkey = key = None #NEWLINE# for item in tmsg . splitlines ( ) : #NEWLINE# #INDENT# item = item . strip ( ) #NEWLINE# if ( not item ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( b':' in item ) : #NEWLINE# #INDENT# ( key , value ) = item . split ( b':' , 1 ) #NEWLINE# lastkey = key = key . strip ( ) . lower ( ) #NEWLINE# headers [ key ] = value . strip ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if lastkey : #NEWLINE# #INDENT# headers [ lastkey ] += ( b'\n' + item ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( b'\x04' in msg ) : #NEWLINE# #INDENT# ( ctxt , msg ) = msg . split ( b'\x04' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ctxt = None #NEWLINE# #UNINDENT# if ( b'\x00' in msg ) : #NEWLINE# #INDENT# msg = msg . split ( b'\x00' ) #NEWLINE# tmsg = tmsg . split ( b'\x00' ) #NEWLINE# if catalog . charset : #NEWLINE# #INDENT# msg = [ x . decode ( catalog . charset ) for x in msg ] #NEWLINE# tmsg = [ x . decode ( catalog . charset ) for x in tmsg ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if catalog . charset : #NEWLINE# #INDENT# msg = msg . decode ( catalog . charset ) #NEWLINE# tmsg = tmsg . decode ( catalog . charset ) #NEWLINE# #UNINDENT# #UNINDENT# catalog [ msg ] = Message ( msg , tmsg , context = ctxt ) #NEWLINE# origidx += 8 #NEWLINE# transidx += 8 #NEWLINE# #UNINDENT# catalog . mime_headers = headers . items ( ) #NEWLINE# return catalog