<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> from urllib import pathname2url , url2pathname <EOL> from urllib2 import urlopen , Request <EOL> from urlparse import urljoin <EOL> from rdflib . py3compat import PY3 <EOL> if PY3 : <EOL> from io import BytesIO <EOL> assert BytesIO <EOL> else : <EOL> from StringIO import StringIO as BytesIO <EOL> from xml . sax import xmlreader <EOL> from rdflib import __version__ <EOL> from rdflib . term import URIRef <EOL> from rdflib . namespace import Namespace <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Parser ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def parse ( self , source , sink ) : <EOL> pass <EOL> class InputSource ( xmlreader . InputSource , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , system_id = None ) : <EOL> xmlreader . InputSource . __init__ ( self , system_id = system_id ) <EOL> self . content_type = None <EOL> self . auto_close = False <EOL> def close ( self ) : <EOL> f = self . getByteStream ( ) <EOL> if f and hasattr ( f , '<STR_LIT>' ) : <EOL> f . close ( ) <EOL> class StringInputSource ( InputSource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , system_id = None ) : <EOL> super ( StringInputSource , self ) . __init__ ( system_id ) <EOL> stream = BytesIO ( value ) <EOL> self . setByteStream ( stream ) <EOL> headers = { <EOL> '<STR_LIT>' : <EOL> '<STR_LIT>' % __version__ <EOL> } <EOL> class URLInputSource ( InputSource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , system_id = None , format = None ) : <EOL> super ( URLInputSource , self ) . __init__ ( system_id ) <EOL> self . url = system_id <EOL> myheaders = dict ( headers ) <EOL> if format == '<STR_LIT>' : <EOL> myheaders [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif format == '<STR_LIT>' : <EOL> myheaders [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif format == '<STR_LIT>' : <EOL> myheaders [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif format == '<STR_LIT>' : <EOL> myheaders [ '<STR_LIT>' ] = ( <EOL> '<STR_LIT>' ) <EOL> else : <EOL> myheaders [ '<STR_LIT>' ] = ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> req = Request ( system_id , None , myheaders ) <EOL> file = urlopen ( req ) <EOL> self . url = file . geturl ( ) <EOL> self . setPublicId ( self . url ) <EOL> self . content_type = file . info ( ) . get ( '<STR_LIT>' ) <EOL> if self . content_type is not None : <EOL> self . content_type = self . content_type . split ( "<STR_LIT:;>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> self . setByteStream ( file ) <EOL> self . response_info = file . info ( ) <EOL> def __repr__ ( self ) : <EOL> return self . url <EOL> class FileInputSource ( InputSource ) : <EOL> def __init__ ( self , file ) : <EOL> base = urljoin ( "<STR_LIT>" , pathname2url ( os . getcwd ( ) ) ) <EOL> system_id = URIRef ( urljoin ( "<STR_LIT>" , pathname2url ( file . name ) ) , base = base ) <EOL> super ( FileInputSource , self ) . __init__ ( system_id ) <EOL> self . file = file <EOL> self . setByteStream ( file ) <EOL> def __repr__ ( self ) : <EOL> return repr ( self . file ) <EOL> def create_input_source ( source = None , publicID = None , <EOL> location = None , file = None , data = None , format = None ) : <EOL> """<STR_LIT>""" <EOL> if sum ( ( <EOL> source is not None , <EOL> location is not None , <EOL> file is not None , <EOL> data is not None , <EOL> ) ) != <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> input_source = None <EOL> if source is not None : <EOL> if isinstance ( source , InputSource ) : <EOL> input_source = source <EOL> else : <EOL> if isinstance ( source , basestring ) : <EOL> location = source <EOL> elif hasattr ( source , "<STR_LIT>" ) and not isinstance ( source , Namespace ) : <EOL> f = source <EOL> input_source = InputSource ( ) <EOL> input_source . setByteStream ( f ) <EOL> if f is sys . stdin : <EOL> input_source . setSystemId ( "<STR_LIT>" ) <EOL> elif hasattr ( f , "<STR_LIT:name>" ) : <EOL> input_source . setSystemId ( f . name ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % <EOL> ( type ( source ) , source ) ) <EOL> absolute_location = None <EOL> auto_close = False <EOL> if location is not None : <EOL> if os . path . exists ( location ) : <EOL> location = pathname2url ( location ) <EOL> base = urljoin ( "<STR_LIT>" , "<STR_LIT>" % pathname2url ( os . getcwd ( ) ) ) <EOL> absolute_location = URIRef ( location , base = base ) . defrag ( ) <EOL> if absolute_location . startswith ( "<STR_LIT>" ) : <EOL> filename = url2pathname ( absolute_location . replace ( "<STR_LIT>" , "<STR_LIT:/>" ) ) <EOL> file = open ( filename , "<STR_LIT:rb>" ) <EOL> else : <EOL> input_source = URLInputSource ( absolute_location , format ) <EOL> auto_close = True <EOL> if file is not None : <EOL> input_source = FileInputSource ( file ) <EOL> if data is not None : <EOL> if isinstance ( data , unicode ) : <EOL> data = data . encode ( '<STR_LIT:utf-8>' ) <EOL> input_source = StringInputSource ( data ) <EOL> auto_close = True <EOL> if input_source is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> input_source . auto_close |= auto_close <EOL> if publicID is not None : <EOL> input_source . setPublicId ( publicID ) <EOL> elif input_source . getPublicId ( ) is None : <EOL> input_source . setPublicId ( absolute_location or "<STR_LIT>" ) <EOL> return input_source </s>
