# Path: lcopt/settings.py
# class LcoptSettings(object):
# 
#     def __init__(self, **kwargs):
# 
#         self.refresh(**kwargs)
#         self.write() # if floats get conveted to strings during setup, it might auto-overwite with a partial config - this makes sure it doesn't
# 
#     def as_dict(self):
#         d = {}
#         for k in self._sections:
#             d[k] = getattr(self, k).as_dict()
#         return d
# 
#     def __repr__(self):
# 
#         string = ""
# 
#         for section, content in self.as_dict().items():
#             string += "{}:\n".format(section)
#             for k, v in content.items():
#                 string += "\t{}: {}\n".format(k,v)
# 
#         return "Lcopt settings: \n\n{}".format(string)
# 
#     def refresh(self, **kwargs):
# 
#         self.config = storage.load_config()
# 
#         self._sections = []
# 
#         for section, content in self.config.items():
#             setattr(self, section, SettingsDict(content, self.write, **kwargs))
#             self._sections.append(section)
# 
# 
#     def write(self):
#         """write the current settings to the config file"""
#         with open(storage.config_file, 'w') as cfg:
#             yaml.dump(self.as_dict(), cfg, default_flow_style=False)
# 
#         storage.refresh()
# 
#     def launch_interact(self):
#         from .settings_gui import FlaskSettingsGUI
#         s = FlaskSettingsGUI()
#         s.run()
#         self.refresh()
#         self.write()
# 
# Path: lcopt/constants.py
# DEFAULT_SINGLE_PROJECT = "LCOPT_Models"
# 
# Path: lcopt/utils.py
# def find_port():
#     for port in range(5000, 5100):
#         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#         result = sock.connect_ex(('127.0.0.1', port))
# 
#         if result != 0:
#             return port
#         else:
#             print('port {} is in use, checking {}'.format(port, port + 1))


from flask import Flask, request, render_template, redirect, send_file
from .settings import LcoptSettings
from .constants import DEFAULT_SINGLE_PROJECT
from .utils import find_port
import webbrowser
        def shutdown():                             # pragma: no cover
            self.shutdown_server()
            return render_template('shutdown.html')

        @app.route('/save_settings', methods=['POST'])
        def save_settings():

            f = request.form

            for k, v in f.items():
                section, item = k.split("|")
                setattr(getattr(self.settings, section), item, v)

            self.settings.write()

            self.shutdown_server()
            
            return render_template('settings_saved.html')

        @app.route('/settings_cancel')
        def cancel_settings():                             # pragma: no cover
            self.shutdown_server()
            return render_template('settings_cancel.html')

        return app

    def run(self, port=None, open_browser=True):                      # pragma: no cover
        app = self.create_app()

        if port is None:

# Path: lcopt/settings.py
# class LcoptSettings(object):
# 
#     def __init__(self, **kwargs):
# 
#         self.refresh(**kwargs)
#         self.write() # if floats get conveted to strings during setup, it might auto-overwite with a partial config - this makes sure it doesn't
# 
#     def as_dict(self):
#         d = {}
#         for k in self._sections:
#             d[k] = getattr(self, k).as_dict()
#         return d
# 
#     def __repr__(self):
# 
#         string = ""
# 
#         for section, content in self.as_dict().items():
#             string += "{}:\n".format(section)
#             for k, v in content.items():
#                 string += "\t{}: {}\n".format(k,v)
# 
#         return "Lcopt settings: \n\n{}".format(string)
# 
#     def refresh(self, **kwargs):
# 
#         self.config = storage.load_config()
# 
#         self._sections = []
# 
#         for section, content in self.config.items():
#             setattr(self, section, SettingsDict(content, self.write, **kwargs))
#             self._sections.append(section)
# 
# 
#     def write(self):
#         """write the current settings to the config file"""
#         with open(storage.config_file, 'w') as cfg:
#             yaml.dump(self.as_dict(), cfg, default_flow_style=False)
# 
#         storage.refresh()
# 
#     def launch_interact(self):
#         from .settings_gui import FlaskSettingsGUI
#         s = FlaskSettingsGUI()
#         s.run()
#         self.refresh()
#         self.write()
# 
# Path: lcopt/constants.py
# DEFAULT_SINGLE_PROJECT = "LCOPT_Models"
# 
# Path: lcopt/utils.py
# def find_port():
#     for port in range(5000, 5100):
#         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#         result = sock.connect_ex(('127.0.0.1', port))
# 
#         if result != 0:
#             return port
#         else:
#             print('port {} is in use, checking {}'.format(port, port + 1))


# Path: lcopt/settings_gui.py
from flask import Flask, request, render_template, redirect, send_file
from .settings import LcoptSettings
from .constants import DEFAULT_SINGLE_PROJECT
from .utils import find_port
import webbrowser

        def shutdown():                             # pragma: no cover
            self.shutdown_server()
            return render_template('shutdown.html')

        @app.route('/save_settings', methods=['POST'])
        def save_settings():

            f = request.form

            for k, v in f.items():
                section, item = k.split("|")
                setattr(getattr(self.settings, section), item, v)

            self.settings.write()

            self.shutdown_server()
            
            return render_template('settings_saved.html')

        @app.route('/settings_cancel')
        def cancel_settings():                             # pragma: no cover
            self.shutdown_server()
            return render_template('settings_cancel.html')

        return app

    def run(self, port=None, open_browser=True):                      # pragma: no cover
        app = self.create_app()

        if port is None:

            port = find_port()