<commit_before># coding=utf-8
"""
Bootstrap module parses command line arguments and initializes the app.

Configures the database connection and starts the web application.
"""
import locale
import sys

import click
from flask import Flask
from gevent.pywsgi import WSGIServer

from sacredboard.app.config import jinja_filters
from sacredboard.app.data.filestorage import FileStorage
from sacredboard.app.data.mongodb import PyMongoDataAccess
from sacredboard.app.webapi import routes

locale.setlocale(locale.LC_ALL, '')
app = Flask(__name__)


@click.command()
@click.option("-m", default=None, metavar="HOST:PORT:DATABASE",
              help="Connect to MongoDB using the format"
                   " host:port:database_name or just the database_name. "
                   "Default: sacred"
                   " Mutually exclusive with -mu")
@click.option("-mu", default=(None, None),
              metavar="CONNECTION_STRING DATABASE", type=(str, str),
              help="Connect to MongoDB using mongodb://..."
                   " and specify the database name."
                   " Mutually exclusive with -m")
@click.option("-mc", default="runs", metavar="COLLECTION",
              help="The collection containing the Sacred's list of runs. "
                   "You might need it if you use a custom collection name "
                   "or Sacred v0.6 (which used default.runs). "
                   "Default: runs")
@click.option("-F", default="",
              help="Path to directory containing experiment results of the"
                   "File Storage observer.")
@click.option("--no-browser", is_flag=True, default=False,
              help="Do not open web browser automatically.")
@click.option("--debug", is_flag=True, default=False,
              help="Run the application in Flask debug mode "
                   "(for development).")
@click.version_option()
def run(debug, no_browser, m, mu, mc, f):
    """
    Sacredboard.

\b
Sacredboard is a monitoring dashboard for Sacred.
Homepage: http://github.com/chovanecm/sacredboard

Example usage:

\b
sacredboard -m sacred
    Starts Sacredboard on default port (5000) and connects to
    a local MongoDB database called 'sacred'. Opens web browser.
    Note: MongoDB must be listening on localhost.
\b
sacredboard -m 192.168.1.1:27017:sacred
    Starts Sacredboard on default port (5000) and connects to
    a MongoDB database running on 192.168.1.1 on port 27017
    to a database called 'sacred'. Opens web browser.
\b
sacredboard -mu mongodb://user:pwd@host/admin?authMechanism=SCRAM-SHA-1 sacred
    Starts Sacredboard on default port (5000) and connects to
    a MongoDB database running on localhost on port 27017
    to a database called 'sacred'. Opens web browser.<commit_msg>Add note that -F option (file storage) is experimental<commit_after># coding=utf-8
"""
Bootstrap module parses command line arguments and initializes the app.

Configures the database connection and starts the web application.
"""
import locale
import sys

import click
from flask import Flask
from gevent.pywsgi import WSGIServer

from sacredboard.app.config import jinja_filters
from sacredboard.app.data.filestorage import FileStorage
from sacredboard.app.data.mongodb import PyMongoDataAccess
from sacredboard.app.webapi import routes

locale.setlocale(locale.LC_ALL, '')
app = Flask(__name__)


@click.command()
@click.option("-m", default=None, metavar="HOST:PORT:DATABASE",
              help="Connect to MongoDB using the format"
                   " host:port:database_name or just the database_name. "
                   "Default: sacred"
                   " Mutually exclusive with -mu")
@click.option("-mu", default=(None, None),
              metavar="CONNECTION_STRING DATABASE", type=(str, str),
              help="Connect to MongoDB using mongodb://..."
                   " and specify the database name."
                   " Mutually exclusive with -m")
@click.option("-mc", default="runs", metavar="COLLECTION",
              help="The collection containing the Sacred's list of runs. "
                   "You might need it if you use a custom collection name "
                   "or Sacred v0.6 (which used default.runs). "
                   "Default: runs")
@click.option("-F", default="",
              help="Path to directory containing experiment results of the"
                   "File Storage observer. (experimental)")
@click.option("--no-browser", is_flag=True, default=False,
              help="Do not open web browser automatically.")
@click.option("--debug", is_flag=True, default=False,
              help="Run the application in Flask debug mode "
                   "(for development).")
@click.version_option()
def run(debug, no_browser, m, mu, mc, f):
    """
    Sacredboard.

\b
Sacredboard is a monitoring dashboard for Sacred.
Homepage: http://github.com/chovanecm/sacredboard

Example usage:

\b
sacredboard -m sacred
    Starts Sacredboard on default port (5000) and connects to
    a local MongoDB database called 'sacred'. Opens web browser.
    Note: MongoDB must be listening on localhost.
\b
sacredboard -m 192.168.1.1:27017:sacred
    Starts Sacredboard on default port (5000) and connects to
    a MongoDB database running on 192.168.1.1 on port 27017
    to a database called 'sacred'. Opens web browser.
\b
sacredboard -mu mongodb://user:pwd@host/admin?authMechanism=SCRAM-SHA-1 sacred
    Starts Sacredboard on default port (5000) and connects to
    a MongoDB database running on localhost on port 27017
    to a database called 'sacred'. Opens web browser.
{'fraction_non_alphanumeric': 0.09623655913978495, 'fraction_numerical': 0.016845878136200716, 'mean_word_length': 2.921995783555868, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '888995', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1530, 'n_words': 550}