async def log_handler(self, req): import sanic from collections import Sequence if req.files: file, = req.files['file'] print(f"uploading: {file.name} len: {len(file.body)}") self.log(file.name, file.body, "byte", LogOptions(overwrite=True)) return sanic.response.json(dict(name=file.name, length=len(file.body), overwrite=True)) elif not req.json: cprint(f'request json is empty: {req.text}', 'red') return sanic.response.text("Request json is empty") log_entry = LogEntry(**req.json) print(f"writing: {log_entry.key} type: {log_entry.type} options: {log_entry.options}") data = deserialize(log_entry.data) if not log_entry: options = None elif log_entry.options is None: options = log_entry.options elif isinstance(log_entry.options, Sequence): options = LogOptions(*log_entry.options) elif isinstance(log_entry.options, dict): options = LogOptions(**log_entry.options) else: options = None self.log(log_entry.key, data, log_entry.type, options) return sanic.response.text('ok')
def ping(self, exp_key, status, burn=True): status_path = os.path.join(exp_key, '__presence') self.log(status_path, dict(status=status, time=datetime.now()), dtype="yaml", options=LogOptions(overwrite=True, write_mode='key')) signal_path = os.path.join(exp_key, '__signal.pkl') res = self.load(signal_path, 'read_pkl') if burn: self.remove(signal_path) return serialize(res)
async def log_handler(self, req): import sanic if not req.json: print(f'request json is empty: {req.text}') return sanic.response.text("Reuqest json is empty") log_entry = LogEntry(**req.json) print("writing: {} type: {} options: {}".format(log_entry.key, log_entry.type, log_entry.options)) data = deserialize(log_entry.data) options = log_entry.options if log_entry.options is None else LogOptions(*log_entry.options) self.log(log_entry.key, data, log_entry.type, options) return sanic.response.text('ok')
def log_buffer(self, key, buf): # defaults to overwrite for binary data. self._log(key, buf, dtype="byte", options=LogOptions(overwrite=True))
def log(self, key, data, **options): self._log(key, data, dtype="log", options=LogOptions(**options))
def send_signal(self, exp_key, signal=None): options = LogOptions(overwrite=True) channel = os.path.join(exp_key, "__signal.pkl") self._log(channel, signal, dtype="log", options=options)
def send_image(self, key, data): assert data.dtype in ALLOWED_TYPES, "image data must be one of {}".format( ALLOWED_TYPES) self._log(key, data, dtype="image", options=LogOptions(overwrite=True))
def log_text(self, key, text, **options): self._log(key, text, dtype="text", options=LogOptions(**options))