# Path: quantrocket/exceptions.py
# class ImproperlyConfigured(Exception):
#     pass
# 
# Path: quantrocket/houston.py
# CANNOT_CONNECT_TO_HOUSTON_ERROR_LOCAL = """{error}
# 
# Could not connect to houston at {scheme}://{netloc}
# 
# Please verify that houston is running and bound to port {port} by checking the output of:
# 
#     docker ps
# 
# The output should resemble:
# 
#     quantrocket/houston ... 0.0.0.0:{port}->80/tcp ... quantrocket_houston_1
#     ...
# 
# If not running you can deploy it with:
# 
#     docker-compose -p quantrocket up -d
# """
# CANNOT_CONNECT_TO_HOUSTON_ERROR_CLOUD = """{error}
# 
# Could not connect to houston at {scheme}://{netloc}
# 
# Please verify that the URL is correct and houston is running.
# """
#     DEFAULT_TIMEOUT = 120
# def _get_force_timeout():
#     def __init__(self):
#     def base_url(self):
#     def _set_base_url(self):
#     def request(self, method, url, *args, **kwargs):
#     def raise_for_status_with_json(response):
# def ping():
# def _cli_ping():
# class Houston(requests.Session):
# 
# Path: quantrocket/cli/utils/output.py
# def json_to_cli(func, *args, **kwargs):
#     """
#     Converts a JSON response to a more appropriate CLI response.
# 
#     If JSON is preferred, the response will be returned as-is. Otherwise:
# 
#     - If the JSON is a list of scalars, the output will be simplified to a
#     string of newline-separated values suitable for the command line (unless
#     simplify_lists is False).
# 
#     - If the JSON is empty, nothing will be returned.
# 
#     - YAML will be returned.
#     """
#     exit_code = 0
#     simplify_list = kwargs.pop("simplify_list", True)
#     try:
#         json_response = func(*args, **kwargs)
#     except requests.exceptions.HTTPError as e:
#         # use json response from service, if available
#         json_response = getattr(e, "json_response", {}) or {"status": "error", "msg": repr(e)}
#         exit_code = 1
#     if not json_response:
#         return None, exit_code
#     if os.environ.get("QUANTROCKET_CLI_OUTPUT_FORMAT", "").lower() == "json":
#         return json.dumps(json_response), exit_code
#     if simplify_list and isinstance(json_response, list) and not any([
#         isinstance(item, (dict, list, tuple, set)) for item in json_response]):
#         return "\n".join([str(item) for item in json_response]), exit_code
#     return yaml.safe_dump(json_response, default_flow_style=False).strip(), exit_code
# 
# Path: quantrocket/cli/utils/files.py
# def write_response_to_filepath_or_buffer(filepath_or_buffer, response):
#     """
#     Writes the response content to the filepath or buffer.
#     """
#     if hasattr(filepath_or_buffer, "write"):
#         if six.PY3 and filepath_or_buffer is sys.stdout:
#             # Write bytes to stdout (https://stackoverflow.com/a/23932488)
#             filepath_or_buffer = filepath_or_buffer.buffer
#         mode = getattr(filepath_or_buffer, "mode", "w")
#         for chunk in response.iter_content(chunk_size=1024):
#             if chunk:
#                 if "b" not in mode and six.PY3:
#                     chunk = chunk.decode("utf-8")
#                 filepath_or_buffer.write(chunk)
#         if filepath_or_buffer.seekable():
#             filepath_or_buffer.seek(0)
#     else:
#         with open(filepath_or_buffer, "wb") as f:
#             for chunk in response.iter_content(chunk_size=1024):
#                 if chunk:
#                     f.write(chunk)


import logging, logging.handlers
import socket
import time
import re
import six
import sys
import os
            import warnings
from six.moves import queue, urllib
from .exceptions import ImproperlyConfigured
from .houston import Houston, houston
from quantrocket.cli.utils.output import json_to_cli
from quantrocket.cli.utils.files import write_response_to_filepath_or_buffer
def download_logfile(outfile, detail=False, match=None):
    """
    Download the logfile.

    Parameters
    ----------
    outfile: str or file-like object, required
        filename or file object to write the logfile to

    detail : bool
        download detailed logs from the logspout service, otherwise download
        the standard logs from the flightlog service

    match : str, optional
        filter the logfile to lines containing this string

    Returns
    -------
    None
    """
    if detail:
        logtype = "system"
    else:
        logtype = "app"

    params = {}
    if match:
        params["match"] = match

    response = houston.get("/flightlog/logfile/{0}".format(logtype), params=params, stream=True)

# Path: quantrocket/exceptions.py
# class ImproperlyConfigured(Exception):
#     pass
# 
# Path: quantrocket/houston.py
# CANNOT_CONNECT_TO_HOUSTON_ERROR_LOCAL = """{error}
# 
# Could not connect to houston at {scheme}://{netloc}
# 
# Please verify that houston is running and bound to port {port} by checking the output of:
# 
#     docker ps
# 
# The output should resemble:
# 
#     quantrocket/houston ... 0.0.0.0:{port}->80/tcp ... quantrocket_houston_1
#     ...
# 
# If not running you can deploy it with:
# 
#     docker-compose -p quantrocket up -d
# """
# CANNOT_CONNECT_TO_HOUSTON_ERROR_CLOUD = """{error}
# 
# Could not connect to houston at {scheme}://{netloc}
# 
# Please verify that the URL is correct and houston is running.
# """
#     DEFAULT_TIMEOUT = 120
# def _get_force_timeout():
#     def __init__(self):
#     def base_url(self):
#     def _set_base_url(self):
#     def request(self, method, url, *args, **kwargs):
#     def raise_for_status_with_json(response):
# def ping():
# def _cli_ping():
# class Houston(requests.Session):
# 
# Path: quantrocket/cli/utils/output.py
# def json_to_cli(func, *args, **kwargs):
#     """
#     Converts a JSON response to a more appropriate CLI response.
# 
#     If JSON is preferred, the response will be returned as-is. Otherwise:
# 
#     - If the JSON is a list of scalars, the output will be simplified to a
#     string of newline-separated values suitable for the command line (unless
#     simplify_lists is False).
# 
#     - If the JSON is empty, nothing will be returned.
# 
#     - YAML will be returned.
#     """
#     exit_code = 0
#     simplify_list = kwargs.pop("simplify_list", True)
#     try:
#         json_response = func(*args, **kwargs)
#     except requests.exceptions.HTTPError as e:
#         # use json response from service, if available
#         json_response = getattr(e, "json_response", {}) or {"status": "error", "msg": repr(e)}
#         exit_code = 1
#     if not json_response:
#         return None, exit_code
#     if os.environ.get("QUANTROCKET_CLI_OUTPUT_FORMAT", "").lower() == "json":
#         return json.dumps(json_response), exit_code
#     if simplify_list and isinstance(json_response, list) and not any([
#         isinstance(item, (dict, list, tuple, set)) for item in json_response]):
#         return "\n".join([str(item) for item in json_response]), exit_code
#     return yaml.safe_dump(json_response, default_flow_style=False).strip(), exit_code
# 
# Path: quantrocket/cli/utils/files.py
# def write_response_to_filepath_or_buffer(filepath_or_buffer, response):
#     """
#     Writes the response content to the filepath or buffer.
#     """
#     if hasattr(filepath_or_buffer, "write"):
#         if six.PY3 and filepath_or_buffer is sys.stdout:
#             # Write bytes to stdout (https://stackoverflow.com/a/23932488)
#             filepath_or_buffer = filepath_or_buffer.buffer
#         mode = getattr(filepath_or_buffer, "mode", "w")
#         for chunk in response.iter_content(chunk_size=1024):
#             if chunk:
#                 if "b" not in mode and six.PY3:
#                     chunk = chunk.decode("utf-8")
#                 filepath_or_buffer.write(chunk)
#         if filepath_or_buffer.seekable():
#             filepath_or_buffer.seek(0)
#     else:
#         with open(filepath_or_buffer, "wb") as f:
#             for chunk in response.iter_content(chunk_size=1024):
#                 if chunk:
#                     f.write(chunk)


# Path: quantrocket/flightlog.py
import logging, logging.handlers
import socket
import time
import re
import six
import sys
import os
            import warnings
from six.moves import queue, urllib
from .exceptions import ImproperlyConfigured
from .houston import Houston, houston
from quantrocket.cli.utils.output import json_to_cli
from quantrocket.cli.utils.files import write_response_to_filepath_or_buffer

def download_logfile(outfile, detail=False, match=None):
    """
    Download the logfile.

    Parameters
    ----------
    outfile: str or file-like object, required
        filename or file object to write the logfile to

    detail : bool
        download detailed logs from the logspout service, otherwise download
        the standard logs from the flightlog service

    match : str, optional
        filter the logfile to lines containing this string

    Returns
    -------
    None
    """
    if detail:
        logtype = "system"
    else:
        logtype = "app"

    params = {}
    if match:
        params["match"] = match

    response = houston.get("/flightlog/logfile/{0}".format(logtype), params=params, stream=True)

    houston.raise_for_status_with_json(response)