Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _init(self):
if os.path.isdir(self.job_dir):
logger.error(f'job dir {self.job_dir} already exists')
raise RuntimeError(f'job {self.id} already exists')
try:
os.makedirs(self.job_dir)
self._update_status(MLJobStatus.INITIALIZED)
self._save_meta()
except OSError:
logger.error(f'failed to create job dir {self.job_dir}')
else:
logger.debug(f'successfully created the directory {self.job_dir}')
cookies = {
v.key: v
for _, v in list(
filter(
lambda item: item[0].lower() == "set-cookie",
response.headers.items(),
)
)
}
headers += [
(str(name).encode("latin-1"), str(value).encode("latin-1"))
for name, value in response.headers.items()
if name.lower() not in ["set-cookie"]
]
except AttributeError:
logger.error(
"Invalid response object for url %s, "
"Expected Type: HTTPResponse, Actual Type: %s",
self.request.url,
type(response),
)
exception = ServerError("Invalid response type")
response = self.sanic_app.error_handler.response(
self.request, exception
)
headers = [
(str(name).encode("latin-1"), str(value).encode("latin-1"))
for name, value in response.headers.items()
if name not in (b"Set-Cookie",)
]
if "content-length" not in response.headers and not isinstance(
def response(success: bool = True, data=None, message=None):
data = {'success': success, 'message': message, 'data': data}
data = {k: v for k, v in data.items() if v is not None}
try:
return json(data, ensure_ascii=False)
except Exception as err:
logger.error(err, exc_info=True)
msg = traceback.format_exc()
data = {'success': success, 'message': msg}
return json(data, ensure_ascii=False)
"""
Writes response content synchronously to the transport.
"""
if self._response_timeout_handler:
self._response_timeout_handler.cancel()
self._response_timeout_handler = None
try:
keep_alive = self.keep_alive
self.transport.write(
response.output(
self.request.version, keep_alive, self.keep_alive_timeout
)
)
self.log_response(response)
except AttributeError:
logger.error(
"Invalid response object for url %s, "
"Expected Type: HTTPResponse, Actual Type: %s",
self.url,
type(response),
)
self.write_error(ServerError("Invalid response type"))
except RuntimeError:
if self._debug:
logger.error(
"Connection lost before response written @ %s",
self.request.ip,
)
keep_alive = False
except Exception as e:
self.bail_out(
"Writing response failed, connection closed {}".format(repr(e))
def bail_out(self, message, from_error=False):
if from_error or self.transport.is_closing():
logger.error(
"Transport closed @ %s and exception "
"experienced during error handling",
self.transport.get_extra_info("peername"),
)
logger.debug("Exception:\n%s", traceback.format_exc())
else:
exception = ServerError(message)
self.write_error(exception)
logger.error(message)
:return: None
"""
if from_error or self.transport is None or self.transport.is_closing():
logger.error(
"Transport closed @ %s and exception "
"experienced during error handling",
(
self.transport.get_extra_info("peername")
if self.transport is not None
else "N/A"
),
)
logger.debug("Exception:", exc_info=True)
else:
self.write_error(ServerError(message))
logger.error(message)
def write_error(self, exception):
# An error _is_ a response.
# Don't throw a response timeout, when a response _is_ given.
if self._response_timeout_handler:
self._response_timeout_handler.cancel()
self._response_timeout_handler = None
response = None
try:
response = self.error_handler.response(self.request, exception)
version = self.request.version if self.request else "1.1"
self.transport.write(response.output(version))
except RuntimeError:
if self._debug:
logger.error(
"Connection lost before error written @ %s",
self.request.ip if self.request else "Unknown",
)
except Exception as e:
self.bail_out(
"Writing error failed, connection closed {}".format(repr(e)),
from_error=True,
)
finally:
if self.parser and (
self.keep_alive or getattr(response, "status", 0) == 408
):
self.log_response(response)
try:
self.transport.close()
except AttributeError as e:
hashlib.sha256(attachment.encode('utf-8')).hexdigest()
)
self.sql_data_transactions.append(tx_data)
for proof in tx['proofs']:
proof_id = 'proof-' + str(uuid.uuid4())
self.sql_data_proofs.append((tx['id'], proof, proof_id))
except asyncio.CancelledError:
logger.info('Parser has been stopped')
raise
except Exception as error:
logger.error('Fetching data error: {}'.format(error))
pass
# await self.emergency_stop_loop('Fetch data', error)
async def _handle_request(self, real_handle, request, write_callback,
stream_callback):
cancelled = False
try:
_ = await real_handle(request, write_callback,
stream_callback)
except CancelledError as ce:
# We still want to run cleanup middleware, even if cancelled
cancelled = ce
except BaseException as be:
logger.error("SPF caught an error that should have been caught"
" by Sanic response handler.")
logger.error(str(be))
raise
finally:
# noinspection PyUnusedLocal
_ = await self._run_cleanup_middleware(request) # noqa: F841
if cancelled:
raise cancelled
def bail_out(self, message, from_error=False):
if from_error or self.transport.is_closing():
logger.error(
"Transport closed @ %s and exception "
"experienced during error handling",
self.transport.get_extra_info("peername"),
)
logger.debug("Exception:\n%s", traceback.format_exc())
else:
exception = ServerError(message)
self.write_error(exception)
logger.error(message)