# 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


KNOWN_SETTINGS = {
                    'ecoinvent':['username', 'password', 'version', 'system_model'],
                    'model_storage': ['location', 'project', 'single_project_name']
                 }

KNOWN_SETTINGS_META = {
                        ('ecoinvent', 'username'): {'type':'text', 'label':'username'},
                        ('ecoinvent', 'password'): {'type': 'password', 'label':'password'},
                        ('ecoinvent', 'version'): {'type':'select', 'label':'version', 'options':[('3.2', '3.2'),('3.3', '3.3'),('3.4', '3.4'),('3.5', '3.5')]},
                        ('ecoinvent', 'system_model'): {'type':'select', 'label':'system model', 'options':[('cutoff', 'Cut-off'), ('apos', 'Allocation at the Point of Substitution (APOS)'), ('consequential', 'Consequential')]},
                        ('model_storage', 'location'): {'type':'select', 'label':'save location', 'options':[('appdir', 'Application directory'), ('curdir', 'Current directory')]},
                        ('model_storage', 'project'): {'type':'select', 'label':'project setup', 'options':[('single', 'One project containing all lcopt models'), ('unique', 'Each lcopt model has a separate project')]},
                        ('model_storage', 'single_project_name'): {'type':'text', 'label':'single project name'},

# 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



KNOWN_SETTINGS = {
                    'ecoinvent':['username', 'password', 'version', 'system_model'],
                    'model_storage': ['location', 'project', 'single_project_name']
                 }

KNOWN_SETTINGS_META = {
                        ('ecoinvent', 'username'): {'type':'text', 'label':'username'},
                        ('ecoinvent', 'password'): {'type': 'password', 'label':'password'},
                        ('ecoinvent', 'version'): {'type':'select', 'label':'version', 'options':[('3.2', '3.2'),('3.3', '3.3'),('3.4', '3.4'),('3.5', '3.5')]},
                        ('ecoinvent', 'system_model'): {'type':'select', 'label':'system model', 'options':[('cutoff', 'Cut-off'), ('apos', 'Allocation at the Point of Substitution (APOS)'), ('consequential', 'Consequential')]},
                        ('model_storage', 'location'): {'type':'select', 'label':'save location', 'options':[('appdir', 'Application directory'), ('curdir', 'Current directory')]},
                        ('model_storage', 'project'): {'type':'select', 'label':'project setup', 'options':[('single', 'One project containing all lcopt models'), ('unique', 'Each lcopt model has a separate project')]},
                        ('model_storage', 'single_project_name'): {'type':'text', 'label':'single project name'},

                      }                