<s> import zipfile <EOL> from xml . sax import make_parser , handler <EOL> from xml . sax . xmlreader import InputSource <EOL> import xml . sax . saxutils <EOL> from io import StringIO <EOL> MANIFESTNS = "<STR_LIT>" <EOL> class ODFManifestHandler ( handler . ContentHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . manifest = { } <EOL> self . elements = { <EOL> ( MANIFESTNS , '<STR_LIT>' ) : ( self . s_file_entry , self . donothing ) , <EOL> } <EOL> def handle_starttag ( self , tag , method , attrs ) : <EOL> method ( tag , attrs ) <EOL> def handle_endtag ( self , tag , method ) : <EOL> method ( tag ) <EOL> def startElementNS ( self , tag , qname , attrs ) : <EOL> method = self . elements . get ( tag , ( None , None ) ) [ <NUM_LIT:0> ] <EOL> if method : <EOL> self . handle_starttag ( tag , method , attrs ) <EOL> else : <EOL> self . unknown_starttag ( tag , attrs ) <EOL> def endElementNS ( self , tag , qname ) : <EOL> method = self . elements . get ( tag , ( None , None ) ) [ <NUM_LIT:1> ] <EOL> if method : <EOL> self . handle_endtag ( tag , method ) <EOL> else : <EOL> self . unknown_endtag ( tag ) <EOL> def unknown_starttag ( self , tag , attrs ) : <EOL> pass <EOL> def unknown_endtag ( self , tag ) : <EOL> pass <EOL> def donothing ( self , tag , attrs = None ) : <EOL> pass <EOL> def s_file_entry ( self , tag , attrs ) : <EOL> m = attrs . get ( ( MANIFESTNS , '<STR_LIT>' ) , "<STR_LIT>" ) <EOL> p = attrs . get ( ( MANIFESTNS , '<STR_LIT>' ) ) <EOL> self . manifest [ p ] = { '<STR_LIT>' : m , '<STR_LIT>' : p } <EOL> def manifestlist ( manifestxml ) : <EOL> odhandler = ODFManifestHandler ( ) <EOL> parser = make_parser ( ) <EOL> parser . setFeature ( handler . feature_namespaces , <NUM_LIT:1> ) <EOL> parser . setContentHandler ( odhandler ) <EOL> parser . setErrorHandler ( handler . ErrorHandler ( ) ) <EOL> inpsrc = InputSource ( ) <EOL> inpsrc . setByteStream ( StringIO ( manifestxml ) ) <EOL> parser . parse ( inpsrc ) <EOL> return odhandler . manifest <EOL> def odfmanifest ( odtfile ) : <EOL> z = zipfile . ZipFile ( odtfile ) <EOL> manifest = z . read ( '<STR_LIT>' ) <EOL> z . close ( ) <EOL> return manifestlist ( manifest ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> result = odfmanifest ( sys . argv [ <NUM_LIT:1> ] ) <EOL> for file in list ( result . values ( ) ) : <EOL> print ( "<STR_LIT>" % ( file [ '<STR_LIT>' ] , file [ '<STR_LIT>' ] ) ) </s>
