'Produces a file object from source.
source can be either a file object, local filename or a string.'

def openStream(self, source):

    if hasattr(source, 'read'):
        stream = source
    else:
        if isinstance(source, unicode):
            source = source.encode('utf-8')
            self.charEncoding = ('utf-8', 'certain')
        try:
            from io import BytesIO
        except:
            from cStringIO import StringIO as BytesIO
        stream = BytesIO(source)
    if ((not (hasattr(stream, 'tell') and hasattr(stream, 'seek'))) or (stream is sys.stdin)):
        stream = BufferedStream(stream)
    return stream
