def find_in_fileo(ctx, _files, hsize, word, options=None, print_prog=True, include_header=False): """Find word in file object and return result temp file.""" _files = remove_empty_file(_files) if len(_files) == 0: return None def _write_header(_file, result_file): with open(_file.abspath, 'r') as ff: with open(result_file, 'w') as rf: header = ff.readline() rf.write(header) result_file, _ = unique_tmp_path(TMP_PREFIX) tmp_file, _ = unique_tmp_path(TMP_PREFIX) fileno = len(_files) ctx.pg = ProgressBar('finding in %s' % hsize, fileno) # for each target file if isinstance(word, types.ListType) or isinstance(word, types.TupleType): word = '\|'.join(word) linfos = LineInfo() for idx, _file in enumerate(_files): if include_header: _write_header(_file, result_file) include_header = False linfo = _find_in_fileo_grep(ctx, result_file, tmp_file, word, idx, _file, fileno, options, print_prog) if linfo is not None: linfos += linfo ctx.pg.done() os.unlink(tmp_file) tf = TempFile(ctx, result_file, linfos) return tf
def zlink(self): """Return html link to zipped file for IPython notebook to download.""" tmp_file, _ = unique_tmp_path(TMP_PREFIX, '.zip') with zipfile.ZipFile(tmp_file, 'w', zipfile.ZIP_DEFLATED) as zf: _add_zip_flat(zf, self.abspath) filename = tmp_file.split(os.path.sep)[-1] _, TMP_URL = get_urls() return HTML(TMP_URL % (filename, filename))
def _slice(self, slc): idx1, idx2 = get_slice_idx(slc, self.count) tmp_file, _ = unique_tmp_path(TMP_PREFIX) with open(tmp_file, 'w') as out: cmd = ['sed', '-n', '%d,%dp' % (idx1 + 1, idx2), self.abspath] check_call(cmd, stdout=out) # TODO: check with simple test file linfos = self._linfos[slice(idx1, idx2)] return TempFile(self._ctx, tmp_file, linfos, True)
def _file_head_or_tail(flike, head, count=10): tmp_file, _ = unique_tmp_path(TMP_PREFIX) with open(tmp_file, 'w') as out: c = 'head' if head else 'tail' abspath = get_realpath(flike.abspath) cmd = [c, '-n', str(count), abspath] check_call(cmd, stdout=out) cnt = get_line_count(tmp_file) if head: linfos = flike._linfos[0:cnt] else: linfos = flike._linfos[-cnt:] return TempFile(flike._ctx, tmp_file, linfos, True)
def zip_files(ftype, data): """Asynchronously zip files.""" print 'zip_files' files = data.split('\n') total = float(len(files)) prog = 1 tmp_file, _ = unique_tmp_path(TMP_PREFIX, '.zip') with zipfile.ZipFile(tmp_file, 'w', zipfile.ZIP_DEFLATED) as zf: for _file in files: prog += 1 _add_zip_flat(zf, _file) print prog / total _progress(zip_files, prog/total) filename = tmp_file.split(os.path.sep)[-1] _, TMP_URL = get_urls() return TMP_URL % (filename, filename)
def merge(self): """Merge all files into a temp file and return it.""" linfos = LineInfo() tmp_file, _ = unique_tmp_path(TMP_PREFIX) filecnt = len(self.files) fileno = 0 pg = ProgressBar('merging', filecnt) with open(tmp_file, 'w') as out: for _file in self.files: pg.animate(fileno) cmd = ['cat', _file.abspath] check_call(cmd, stdout=out) linfos += _file._linfos fileno += 1 pg.done() return TempFile(self._ctx, tmp_file, linfos)
def _find_in_temp_grep(ctx, result_file, tempo, word, _options, include_header): word = set_grep_encoding(ctx, word) tmp_file, _ = unique_tmp_path(TMP_PREFIX) # grep to tmp with linenum with open(tmp_file, 'w') as out: if include_header: header = open(tempo.abspath, 'r').readline() out.write(header) try: options = ['-h', '-n'] if _options is not None: options += _options.split() cmd = [get_grep(word)] + options + [word, tempo.abspath] check_call(cmd, stdout=out) except CalledProcessError: pass unset_grep_encoding(ctx, word) # fill infos, and write to result return _find_in_temp_grep_write(result_file, tempo, tmp_file)
def _find_in_temp(ctx, tempo, word, options, include_header): """Find word in a temp file and return result temp file.""" tmp_file, _ = unique_tmp_path(TMP_PREFIX) linfos = _find_in_temp_grep(ctx, tmp_file, tempo, word, options, include_header) return TempFile(ctx, tmp_file, linfos)