<s> """<STR_LIT>""" <EOL> from functools import wraps <EOL> import warnings <EOL> import weakref <EOL> try : <EOL> from builtins import ResourceWarning as CleanupWarning <EOL> except ImportError : <EOL> from exceptions import RuntimeWarning as CleanupWarning <EOL> from magic import api <EOL> from magic . api import MagicError <EOL> from magic . flags import MAGIC_NONE <EOL> from magic . compatability import byte_args , iter_encode , str_return <EOL> def raise_if_none ( attrname , exception , message ) : <EOL> "<STR_LIT>" <EOL> def decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> if getattr ( self , attrname ) is None : <EOL> raise exception ( message ) <EOL> return func ( self , * args , ** kwargs ) <EOL> return wrapper <EOL> return decorator <EOL> class Magic ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , paths = None , flags = MAGIC_NONE ) : <EOL> """<STR_LIT>""" <EOL> self . _repr = "<STR_LIT>" . format ( paths , flags ) <EOL> cookie = api . magic_open ( flags ) <EOL> def cleanup ( _ ) : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> CleanupWarning ) <EOL> api . magic_close ( cookie ) <EOL> self . weakref = weakref . ref ( self , cleanup ) <EOL> self . cookie = cookie <EOL> pathstr = b'<STR_LIT::>' . join ( iter_encode ( paths ) ) if paths else None <EOL> try : <EOL> api . magic_load ( self . cookie , pathstr ) <EOL> except MagicError : <EOL> self . close ( ) <EOL> raise <EOL> def __enter__ ( self ) : <EOL> "<STR_LIT>" <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> "<STR_LIT>" <EOL> self . close ( ) <EOL> def __repr__ ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _repr <EOL> def close ( self ) : <EOL> "<STR_LIT>" <EOL> if self . cookie : <EOL> api . magic_close ( self . cookie ) <EOL> del self . weakref <EOL> self . cookie = None <EOL> @ property <EOL> @ raise_if_none ( '<STR_LIT>' , MagicError , '<STR_LIT>' ) <EOL> def consistent ( self ) : <EOL> "<STR_LIT>" <EOL> return api . magic_check ( self . cookie , None ) >= <NUM_LIT:0> <EOL> @ raise_if_none ( '<STR_LIT>' , MagicError , '<STR_LIT>' ) <EOL> @ byte_args ( positions = [ <NUM_LIT:1> ] ) <EOL> @ str_return <EOL> def id_buffer ( self , buffer ) : <EOL> "<STR_LIT>" <EOL> return api . magic_buffer ( self . cookie , <EOL> api . ctypes . c_char_p ( buffer ) , <EOL> len ( buffer ) ) <EOL> @ raise_if_none ( '<STR_LIT>' , MagicError , '<STR_LIT>' ) <EOL> @ byte_args ( positions = [ <NUM_LIT:1> ] ) <EOL> @ str_return <EOL> def id_filename ( self , filename ) : <EOL> "<STR_LIT>" <EOL> return api . magic_file ( self . cookie , filename ) <EOL> @ raise_if_none ( '<STR_LIT>' , MagicError , '<STR_LIT>' ) <EOL> def list ( self , paths = None ) : <EOL> "<STR_LIT>" <EOL> pathstr = b'<STR_LIT::>' . join ( iter_encode ( paths ) ) if paths else None <EOL> try : <EOL> api . magic_list ( self . cookie , pathstr ) <EOL> except AttributeError : <EOL> msg = '<STR_LIT>' <EOL> raise MagicError ( msg ) </s>
