# Path: fluxghost/api/misc.py
# class BinaryUploadHelper(object):
#     def __init__(self, length, callback, *args, **kwargs):
#         self.length = length
#         self.callback = callback
#         self.buf = BytesIO()
#         self.buffered = 0
# 
#         self.args = args
#         self.kwargs = kwargs
# 
#         self.last_update = time()
# 
#     def feed(self, buf):
#         l = self.buf.write(buf)
#         self.buffered += l
#         self.last_update = time()
# 
#         if self.buffered < self.length:
#             return False
#         elif self.buffered == self.length:
#             self.callback(self.buf.getvalue(), *self.args, **self.kwargs)
#             return True
#         else:
#             raise RuntimeError("BAD_LENGTH" + " recive too many binary data ("
#                                "should be %i but get %i" %
#                                (self.length, self.buffered),
#                                "recive too many binary data ("
#                                "should be %i but get %i" %
#                                (self.length, self.buffered))
# 
# class BinaryHelperMixin(object):
#     _binary_helper = None
# 
#     def has_binary_helper(self):
#         return self._binary_helper is not None
# 
#     def set_binary_helper(self, helper):
#         self._binary_helper = helper
# 
#     def on_binary_message(self, buf):
#         try:
#             if self._binary_helper:
#                 if self._binary_helper.feed(buf) is True:
#                     self._binary_helper = None
#             else:
#                 raise RuntimeError("BAD_PROTOCOL", "no binary accept")
#         except RuntimeError as e:
#             logger.error(e)
#             self.send_fatal(e.args[0])
# 
# class OnTextMessageMixin(object):
#     def on_text_message(self, message):
#         try:
#             if not self.has_binary_helper():
#                 message = message.rstrip().split(maxsplit=1)
#                 if len(message) == 1:
#                     cmd = message[0]
#                     params = ''
#                 else:
#                     cmd = message[0]
#                     params = message[1]
# 
#                 if cmd in self.cmd_mapping:
#                     self.cmd_mapping[cmd][0](params,
#                                              *self.cmd_mapping[cmd][1:])
#                 else:
#                     logger.exception("Received message: %s" % (message))
#                     raise ValueError('Undefined Command %s' % (cmd))
#             else:
#                 logger.exception("Received Message: %s" % (message))
#                 raise RuntimeError("PROTOCOL_ERROR", "under uploading mode")
# 
#         except ValueError:
#             logger.exception("Received Message: %s" % (message))
#             self.send_json(status='Error', message='BAD_PARAM_TYPE')
# 
#         except RuntimeError as e:
#             logger.exception("Received Message: %s" % (message))
#             self.send_fatal(e.args[0])


from time import time
from fluxclient import SUPPORT_PCL
from fluxclient.scanner.scan_settings import ScanSetting
from fluxclient.scanner.pc_process import PcProcess
from .misc import BinaryUploadHelper, BinaryHelperMixin, OnTextMessageMixin
import logging
            buf = self.m_pc_process.export(name, file_foramt)
            self.send_text('{{"status": "continue", "length": {}}}'.format(len(buf)))
            self.send_binary(buf)
            self.send_ok()
            logger.debug('  export {} as .{} file done'.format(name, file_foramt))

        def export_threading(self, params):
            logger.info('thread export: {}'.format(params))
            name, file_foramt = params.split()
            collect_name = self.m_pc_process.export_threading(name, file_foramt)
            self.send_text('{{"status": "ok", "collect_name": "{}"}}'.format(collect_name))
            logger.debug('  export {} as .{} file in thread'.format(name, file_foramt))

        def export_collect(self, params):
            logger.info('collect export: {}'.format(params))
            collect_name = params

            buf = self.m_pc_process.export_collect(collect_name)
            if buf:
                self.send_text('{{"status": "continue", "length": {}}}'.format(len(buf)))
                self.send_binary(buf)
                self.send_ok()
            elif buf == 'key error':
                self.send_error('{} key not exist'.format(key))
            else:
                self.send_text('{"status": "computing"}')

        def import_file(self, params):
            logger.info('import file: {}'.format(params))
            name, filetype, file_length = params.split()

# Path: fluxghost/api/misc.py
# class BinaryUploadHelper(object):
#     def __init__(self, length, callback, *args, **kwargs):
#         self.length = length
#         self.callback = callback
#         self.buf = BytesIO()
#         self.buffered = 0
# 
#         self.args = args
#         self.kwargs = kwargs
# 
#         self.last_update = time()
# 
#     def feed(self, buf):
#         l = self.buf.write(buf)
#         self.buffered += l
#         self.last_update = time()
# 
#         if self.buffered < self.length:
#             return False
#         elif self.buffered == self.length:
#             self.callback(self.buf.getvalue(), *self.args, **self.kwargs)
#             return True
#         else:
#             raise RuntimeError("BAD_LENGTH" + " recive too many binary data ("
#                                "should be %i but get %i" %
#                                (self.length, self.buffered),
#                                "recive too many binary data ("
#                                "should be %i but get %i" %
#                                (self.length, self.buffered))
# 
# class BinaryHelperMixin(object):
#     _binary_helper = None
# 
#     def has_binary_helper(self):
#         return self._binary_helper is not None
# 
#     def set_binary_helper(self, helper):
#         self._binary_helper = helper
# 
#     def on_binary_message(self, buf):
#         try:
#             if self._binary_helper:
#                 if self._binary_helper.feed(buf) is True:
#                     self._binary_helper = None
#             else:
#                 raise RuntimeError("BAD_PROTOCOL", "no binary accept")
#         except RuntimeError as e:
#             logger.error(e)
#             self.send_fatal(e.args[0])
# 
# class OnTextMessageMixin(object):
#     def on_text_message(self, message):
#         try:
#             if not self.has_binary_helper():
#                 message = message.rstrip().split(maxsplit=1)
#                 if len(message) == 1:
#                     cmd = message[0]
#                     params = ''
#                 else:
#                     cmd = message[0]
#                     params = message[1]
# 
#                 if cmd in self.cmd_mapping:
#                     self.cmd_mapping[cmd][0](params,
#                                              *self.cmd_mapping[cmd][1:])
#                 else:
#                     logger.exception("Received message: %s" % (message))
#                     raise ValueError('Undefined Command %s' % (cmd))
#             else:
#                 logger.exception("Received Message: %s" % (message))
#                 raise RuntimeError("PROTOCOL_ERROR", "under uploading mode")
# 
#         except ValueError:
#             logger.exception("Received Message: %s" % (message))
#             self.send_json(status='Error', message='BAD_PARAM_TYPE')
# 
#         except RuntimeError as e:
#             logger.exception("Received Message: %s" % (message))
#             self.send_fatal(e.args[0])


# Path: fluxghost/api/scan_modeling.py
from time import time
from fluxclient import SUPPORT_PCL
from fluxclient.scanner.scan_settings import ScanSetting
from fluxclient.scanner.pc_process import PcProcess
from .misc import BinaryUploadHelper, BinaryHelperMixin, OnTextMessageMixin
import logging

            buf = self.m_pc_process.export(name, file_foramt)
            self.send_text('{{"status": "continue", "length": {}}}'.format(len(buf)))
            self.send_binary(buf)
            self.send_ok()
            logger.debug('  export {} as .{} file done'.format(name, file_foramt))

        def export_threading(self, params):
            logger.info('thread export: {}'.format(params))
            name, file_foramt = params.split()
            collect_name = self.m_pc_process.export_threading(name, file_foramt)
            self.send_text('{{"status": "ok", "collect_name": "{}"}}'.format(collect_name))
            logger.debug('  export {} as .{} file in thread'.format(name, file_foramt))

        def export_collect(self, params):
            logger.info('collect export: {}'.format(params))
            collect_name = params

            buf = self.m_pc_process.export_collect(collect_name)
            if buf:
                self.send_text('{{"status": "continue", "length": {}}}'.format(len(buf)))
                self.send_binary(buf)
                self.send_ok()
            elif buf == 'key error':
                self.send_error('{} key not exist'.format(key))
            else:
                self.send_text('{"status": "computing"}')

        def import_file(self, params):
            logger.info('import file: {}'.format(params))
            name, filetype, file_length = params.split()

            helepr = BinaryUploadHelper(int(file_length), self._end_upload, 2, name, filetype)