This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pulling out the file streaming as a module
- Loading branch information
1 parent
6495d7f
commit ecf4276
Showing
2 changed files
with
50 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
"""File Streaming for Workbench CLI""" | ||
import os, sys | ||
import lz4 | ||
|
||
class FileStreamer(object): | ||
"""File Streaming for Workbench CLI""" | ||
|
||
def __init__(self, workbench, progress): | ||
''' FileStreamer Initialization ''' | ||
|
||
# Setup workbench and progress handles | ||
self.workbench = workbench | ||
self.progress = progress | ||
|
||
# Setup compression | ||
self.compressor = lz4.dumps | ||
self.decompressor = lz4.loads | ||
self.compress_ident = 'lz4' | ||
|
||
# Some defaults and counters | ||
self.chunk_size = 1024*1024 # 1 MB | ||
|
||
def _file_chunks(self, data, chunk_size): | ||
""" Yield compressed chunks from a data array""" | ||
for i in xrange(0, len(data), chunk_size): | ||
yield self.compressor(data[i:i+chunk_size]) | ||
|
||
def stream_to_workbench(self, raw_bytes, filename, type_tag): | ||
"""Split up a large file into chunks and send to Workbench""" | ||
md5_list = [] | ||
sent_bytes = 0 | ||
total_bytes = len(raw_bytes) | ||
for chunk in self._file_chunks(raw_bytes, self.chunk_size): | ||
md5_list.append(self.workbench.store_sample(chunk, filename, self.compress_ident)) | ||
sent_bytes += self.chunk_size | ||
self.progress(sent_bytes, total_bytes) | ||
|
||
# Now we just ask Workbench to combine these | ||
return self.workbench.combine_samples(md5_list, filename, type_tag) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters