# Path: sacredboard/app/sacredboard.py
# class Sacredboard():
#     """Class related to sacredboard itself. A better comment will follow later."""
# 
#     @staticmethod
#     def get_version():
#         """Return the current version of sacredboard."""
#         version = pkg_resources.require("sacredboard")[0].version
#         return version
# 
# Path: sacredboard/app/process/process.py
# class Process:
# class WindowsProcess(Process):
# class ProcessError(Exception):
# class UnexpectedOutputError(ProcessError):
#     def __init__(self, command):
#     def run(self):
#     def is_running(self):
#     def read_line(self, time_limit=None):
#     def read_line_stderr(self, time_limit=None):
#     def _read_line(self, source, time_limit=None):
#     def _get_read_object(self, source):
#     def terminate(self, wait=False):
#     def pid(self):
#     def terminate_all(wait=False):
#     def create_process(command):
#     def __init__(self, command):
#     def read_line(self, time_limit=None):
#     def read_line_stderr(self, time_limit=None):
#     def __init__(self, output, expected=None):
# 
# Path: sacredboard/app/process/tensorboard.py
# TENSORBOARD_BINARY = "tensorboard"
# def stop_all_tensorboards():
# def run_tensorboard(logdir, listen_on="0.0.0.0", port=0, tensorboard_args=None, timeout=10):
# def parse_port_from_tensorboard_output(tensorboard_output: str) -> int:
# class TensorboardNotFoundError(ProcessError):
# 
# Path: sacredboard/app/process/tensorboard.py
# class TensorboardNotFoundError(ProcessError):
#     """TensorBoard binary not found."""
# 
#     pass
# 
# def stop_all_tensorboards():
#     """Terminate all TensorBoard instances."""
#     for process in Process.instances:
#         print("Process '%s', running %d" % (process.command[0],
#                                             process.is_running()))
#         if process.is_running() and process.command[0] == "tensorboard":
#             process.terminate()


import re
from pathlib import Path
from flask import Blueprint, Flask
from flask import current_app
from flask import render_template
from flask import request, redirect, url_for
from sacredboard.app.sacredboard import Sacredboard
from ..process import process, tensorboard
from ..process.tensorboard import TensorboardNotFoundError, \
    stop_all_tensorboards
        path_to_log_dir = base_dir.joinpath(log_dir)

    port = int(tensorboard.run_tensorboard(str(path_to_log_dir)))
    url_root = request.url_root
    url_parts = re.search("://([^:/]+)", url_root)
    redirect_to_address = url_parts.group(1)
    return redirect("http://%s:%d" % (redirect_to_address, port))


@routes.route("/tensorboard/stop", methods=['GET', 'POST'])
def close_tensorboards():
    """Stop all TensorBoard instances launched by Sacredboard."""
    stop_all_tensorboards()
    return "Stopping tensorboard"


@routes.errorhandler(TensorboardNotFoundError)
def handle_tensorboard_not_found(e):
    """Handle exception: tensorboard script not found."""
    return "Tensorboard not found on your system." \
           " Please install tensorflow first. Sorry.", 503


@routes.errorhandler(TimeoutError)
def handle_tensorboard_timeout(e):
    """Handle exception: TensorBoard does not respond."""
    return "Tensorboard does not respond. Sorry.", 503


@routes.errorhandler(process.UnexpectedOutputError)

# Path: sacredboard/app/sacredboard.py
# class Sacredboard():
#     """Class related to sacredboard itself. A better comment will follow later."""
# 
#     @staticmethod
#     def get_version():
#         """Return the current version of sacredboard."""
#         version = pkg_resources.require("sacredboard")[0].version
#         return version
# 
# Path: sacredboard/app/process/process.py
# class Process:
# class WindowsProcess(Process):
# class ProcessError(Exception):
# class UnexpectedOutputError(ProcessError):
#     def __init__(self, command):
#     def run(self):
#     def is_running(self):
#     def read_line(self, time_limit=None):
#     def read_line_stderr(self, time_limit=None):
#     def _read_line(self, source, time_limit=None):
#     def _get_read_object(self, source):
#     def terminate(self, wait=False):
#     def pid(self):
#     def terminate_all(wait=False):
#     def create_process(command):
#     def __init__(self, command):
#     def read_line(self, time_limit=None):
#     def read_line_stderr(self, time_limit=None):
#     def __init__(self, output, expected=None):
# 
# Path: sacredboard/app/process/tensorboard.py
# TENSORBOARD_BINARY = "tensorboard"
# def stop_all_tensorboards():
# def run_tensorboard(logdir, listen_on="0.0.0.0", port=0, tensorboard_args=None, timeout=10):
# def parse_port_from_tensorboard_output(tensorboard_output: str) -> int:
# class TensorboardNotFoundError(ProcessError):
# 
# Path: sacredboard/app/process/tensorboard.py
# class TensorboardNotFoundError(ProcessError):
#     """TensorBoard binary not found."""
# 
#     pass
# 
# def stop_all_tensorboards():
#     """Terminate all TensorBoard instances."""
#     for process in Process.instances:
#         print("Process '%s', running %d" % (process.command[0],
#                                             process.is_running()))
#         if process.is_running() and process.command[0] == "tensorboard":
#             process.terminate()


# Path: sacredboard/app/webapi/routes.py
import re
from pathlib import Path
from flask import Blueprint, Flask
from flask import current_app
from flask import render_template
from flask import request, redirect, url_for
from sacredboard.app.sacredboard import Sacredboard
from ..process import process, tensorboard
from ..process.tensorboard import TensorboardNotFoundError, \
    stop_all_tensorboards

        path_to_log_dir = base_dir.joinpath(log_dir)

    port = int(tensorboard.run_tensorboard(str(path_to_log_dir)))
    url_root = request.url_root
    url_parts = re.search("://([^:/]+)", url_root)
    redirect_to_address = url_parts.group(1)
    return redirect("http://%s:%d" % (redirect_to_address, port))


@routes.route("/tensorboard/stop", methods=['GET', 'POST'])
def close_tensorboards():
    """Stop all TensorBoard instances launched by Sacredboard."""
    stop_all_tensorboards()
    return "Stopping tensorboard"


@routes.errorhandler(TensorboardNotFoundError)
def handle_tensorboard_not_found(e):
    """Handle exception: tensorboard script not found."""
    return "Tensorboard not found on your system." \
           " Please install tensorflow first. Sorry.", 503


@routes.errorhandler(TimeoutError)
def handle_tensorboard_timeout(e):
    """Handle exception: TensorBoard does not respond."""
    return "Tensorboard does not respond. Sorry.", 503


@routes.errorhandler(process.UnexpectedOutputError)

def handle_tensorboard_unexpected_output(e: process.UnexpectedOutputError):