def _load_from_stream(self): """Load content from memory :params stream file_content: the actual file content in memory :returns: a book """ self.__load_from_memory_flag = True self.__line_terminator = self._keywords.get( constants.KEYWORD_LINE_TERMINATOR, self.__line_terminator ) separator = DEFAULT_SHEET_SEPARATOR_FORMATTER % self.__line_terminator if self.__multiple_sheets: # will be slow for large files self._file_stream.seek(0) content = self._file_stream.read() sheets = content.split(separator) named_contents = [] for sheet in sheets: if sheet == "": # skip empty named sheet continue lines = sheet.split(self.__line_terminator) result = re.match(constants.SEPARATOR_MATCHER, lines[0]) new_content = "\n".join(lines[1:]) new_sheet = NamedContent( result.group(1), compact.StringIO(new_content) ) named_contents.append(new_sheet) return named_contents else: if hasattr(self._file_stream, "seek"): self._file_stream.seek(0) return [NamedContent(self._file_type, self._file_stream)]
def get_file_handle(self): if compact.PY2: f = UTF8Recorder(self._native_sheet.payload, self._encoding) else: if isinstance(self._native_sheet.payload, compact.BytesIO): content = self._native_sheet.payload.read() f = compact.StringIO(content.decode(self._encoding)) else: f = self._native_sheet.payload return f
def get_file_handle(self): if isinstance(self._native_sheet.payload, compact.BytesIO): # please note that # if the end developer feed us bytesio in python3 # we will do the conversion to StriongIO but that # comes at a cost. content = self._native_sheet.payload.read() unicode_reader = compact.StringIO(content.decode(self._encoding)) else: unicode_reader = self._native_sheet.payload return unicode_reader
def get_file_handle(self): if compact.PY2: if hasattr(self._native_sheet.payload, 'read'): f = UTF8Recorder(self._native_sheet.payload, self._encoding) else: f = self._native_sheet.payload else: if isinstance(self._native_sheet.payload, compact.BytesIO): # please note that # if the end developer feed us bytesio in python3 # we will do the conversion to StriongIO but that # comes at a cost. content = self._native_sheet.payload.read() f = compact.StringIO(content.decode(self._encoding)) else: f = self._native_sheet.payload return f
def __init__(self, file_stream, file_type, multiple_sheets=False, **keywords): """Load content from memory :params stream file_content: the actual file content in memory :returns: a book """ self.handles = [] self.keywords = keywords if file_type == constants.FILE_FORMAT_TSV: self.keywords["dialect"] = constants.KEYWORD_TSV_DIALECT self.file_type = file_type self.__load_from_memory_flag = True self.__line_terminator = keywords.get( constants.KEYWORD_LINE_TERMINATOR, constants.DEFAULT_CSV_NEWLINE) separator = DEFAULT_SHEET_SEPARATOR_FORMATTER % self.__line_terminator if multiple_sheets: # will be slow for large files file_stream.seek(0) content = file_stream.read() sheets = content.split(separator) named_contents = [] for sheet in sheets: if sheet == "": # skip empty named sheet continue lines = sheet.split(self.__line_terminator) result = re.match(constants.SEPARATOR_MATCHER, lines[0]) new_content = "\n".join(lines[1:]) new_sheet = NamedContent(result.group(1), compact.StringIO(new_content)) named_contents.append(new_sheet) self.content_array = named_contents else: if hasattr(file_stream, "seek"): file_stream.seek(0) self.content_array = [NamedContent(self.file_type, file_stream)]
def __init__(self, f, encoding="utf-8", **kwds): # Redirect output to a queue self.queue = compact.StringIO() self.writer = csv.writer(self.queue, **kwds) self.stream = f self.encoder = codecs.getincrementalencoder(encoding)()