# Path: orangecontrib/spark/base/shared_spark_context.py
# class SharedSparkContext:
#     _sc = None
#     _hc = None
# 
#     @property
#     def sc(self):
#         return SharedSparkContext._sc
# 
#     @sc.setter
#     def sc(self, val):
#         SharedSparkContext._sc = val
# 
#     @property
#     def hc(self):
#         return SharedSparkContext._hc
# 
#     @hc.setter
#     def hc(self, val):
#         SharedSparkContext._hc = val
# 
# Path: orangecontrib/spark/utils/gui_utils.py
# class GuiParam:
#     widget = None
# 
#     def __init__(self, parent_widget, label = None, default_value = None, place_holder_text = None, list_values = None, callback_func = None, doc_text = None, **kwargs):
#         """
# 
#         :param label: a text label for the GUI control
#         :param default_value: a default value to asing to the value of the control in case of empty.
#         :param place_holder_text: a text to show when the value of the control is empty, it can be used to avoid use of label.
#         :param list_values: a closed list of the values that accepts this parameter, this should create a combobox like control.
#         :param callback_func: a call back function when the value of the GUI control changes, e.g. combobox selection.
#         :param doc_text: if want to add extra documentation for the parameter, it can be used for a tool tip for instance.
#         :param kwargs: used to add extra parameters for future improvements and compatibility.
#         :return: an instance of gui_param
#         """
#         dummy_func = lambda x: True
# 
#         self.default_value = default_value
#         list_values = ['True', 'False'] if default_value in ('True', 'False') else list_values
# 
#         self.callback_func = callback_func
#         self.parent_widget = parent_widget
#         self.hbox = self.parent_widget
#         layout = self.hbox.layout()
#         if doc_text:
#             self.doc_text = doc_text
# 
#         if list_values:
#             self.list_values = list_values
#             self.gui_type = 'multiple'
#             callback_func = dummy_func if not callback_func else callback_func
#             self.widget = create_auto_combobox(parent_widget, self.list_values, callback_func)
#             self.widget.setStyleSheet("background-color: rgb(255, 255, 255);")
#             if self.default_value:
#                 index = self.widget.findText(self.default_value, QtCore.Qt.MatchFixedString)
#                 if index >= 0:
#                     self.widget.setCurrentIndex(index)
# 
#         else:
#             self.gui_type = 'single'
#             self.widget = QtGui.QLineEdit(parent_widget)
#             self.widget.setStyleSheet("background-color: rgb(255, 255, 255);")
#             self.widget.setPlaceholderText(str(place_holder_text))
#             if self.default_value:
#                 self.widget.setText(str(self.default_value))
# 
#         if label:
#             self.label = label + ":"
#             self.widget_label = QtGui.QLabel(self.label, self.parent_widget)
#             self.widget_label.setBuddy(self.widget)
# 
#             self.hbox = gui.widgetBox(self.hbox, orientation = 'horizontal', addSpace = True)
#             layout = self.hbox.layout()
#             layout.addWidget(self.widget_label)
# 
#         layout.addWidget(self.widget)
# 
#     def get_value(self):
#         if self.gui_type == 'multiple':
#             return self.widget.currentText()
#         elif self.gui_type == 'single':
#             return self.widget.text()
#         else:
#             return None
# 
#     def update(self, values):
#         self.widget.clear()
#         if self.gui_type == 'multiple':
#             for val in values:
#                 self.widget.addItem(val)
#         else:
#             self.widget.setText(values)
# 
#     def get_usable_value(self):
#         val = self.get_value().strip()
#         if val == 'None' or val == '' or val is None:
#             return None
#         if val in ('True', 'False'):
#             return True if val == 'True' else False
#         try:
#             try:
#                 if float(val) == int(val):
#                     if "." in val:
#                         return float(val)
#                     return int(val)
#             except ValueError:
#                 return float(val)
# 
#         except ValueError:
#             return val


from collections import OrderedDict
from Orange.widgets import widget, gui
from Orange.widgets.settings import Setting
from pyspark import SparkConf, SparkContext
from pyspark.sql import HiveContext
from orangecontrib.spark.base.shared_spark_context import SharedSparkContext
from orangecontrib.spark.utils.gui_utils import GuiParam
        # The main label of the Control's GUI.
        # gui.label(self.controlArea, self, "Spark Context")

        self.conf = SparkConf()
        all_prefedined = dict(self.conf.getAll())
        # Create parameters Box.
        box = gui.widgetBox(self.controlArea, "Spark Application", addSpace = True)

        self.gui_parameters = OrderedDict()

        main_parameters = OrderedDict()
        main_parameters['spark.app.name'] = 'OrangeSpark'
        main_parameters['spark.master'] = 'yarn-client'
        main_parameters["spark.executor.instances"] = "8"
        main_parameters["spark.executor.cores"] = "4"
        main_parameters["spark.executor.memory"] = "8g"
        main_parameters["spark.driver.cores"] = "4"
        main_parameters["spark.driver.memory"] = "2g"
        main_parameters["spark.logConf"] = "false"
        main_parameters["spark.app.id"] = "dummy"

        for k, v in self.saved_gui_params.items():
            main_parameters[k] = v

        for k, v in main_parameters.items():
            default_value = all_prefedined.setdefault(k, v)
            self.gui_parameters[k] = GuiParam(parent_widget = box, label = k, default_value = v)
            all_prefedined.pop(k)

        for k, v in all_prefedined.items():

# Path: orangecontrib/spark/base/shared_spark_context.py
# class SharedSparkContext:
#     _sc = None
#     _hc = None
# 
#     @property
#     def sc(self):
#         return SharedSparkContext._sc
# 
#     @sc.setter
#     def sc(self, val):
#         SharedSparkContext._sc = val
# 
#     @property
#     def hc(self):
#         return SharedSparkContext._hc
# 
#     @hc.setter
#     def hc(self, val):
#         SharedSparkContext._hc = val
# 
# Path: orangecontrib/spark/utils/gui_utils.py
# class GuiParam:
#     widget = None
# 
#     def __init__(self, parent_widget, label = None, default_value = None, place_holder_text = None, list_values = None, callback_func = None, doc_text = None, **kwargs):
#         """
# 
#         :param label: a text label for the GUI control
#         :param default_value: a default value to asing to the value of the control in case of empty.
#         :param place_holder_text: a text to show when the value of the control is empty, it can be used to avoid use of label.
#         :param list_values: a closed list of the values that accepts this parameter, this should create a combobox like control.
#         :param callback_func: a call back function when the value of the GUI control changes, e.g. combobox selection.
#         :param doc_text: if want to add extra documentation for the parameter, it can be used for a tool tip for instance.
#         :param kwargs: used to add extra parameters for future improvements and compatibility.
#         :return: an instance of gui_param
#         """
#         dummy_func = lambda x: True
# 
#         self.default_value = default_value
#         list_values = ['True', 'False'] if default_value in ('True', 'False') else list_values
# 
#         self.callback_func = callback_func
#         self.parent_widget = parent_widget
#         self.hbox = self.parent_widget
#         layout = self.hbox.layout()
#         if doc_text:
#             self.doc_text = doc_text
# 
#         if list_values:
#             self.list_values = list_values
#             self.gui_type = 'multiple'
#             callback_func = dummy_func if not callback_func else callback_func
#             self.widget = create_auto_combobox(parent_widget, self.list_values, callback_func)
#             self.widget.setStyleSheet("background-color: rgb(255, 255, 255);")
#             if self.default_value:
#                 index = self.widget.findText(self.default_value, QtCore.Qt.MatchFixedString)
#                 if index >= 0:
#                     self.widget.setCurrentIndex(index)
# 
#         else:
#             self.gui_type = 'single'
#             self.widget = QtGui.QLineEdit(parent_widget)
#             self.widget.setStyleSheet("background-color: rgb(255, 255, 255);")
#             self.widget.setPlaceholderText(str(place_holder_text))
#             if self.default_value:
#                 self.widget.setText(str(self.default_value))
# 
#         if label:
#             self.label = label + ":"
#             self.widget_label = QtGui.QLabel(self.label, self.parent_widget)
#             self.widget_label.setBuddy(self.widget)
# 
#             self.hbox = gui.widgetBox(self.hbox, orientation = 'horizontal', addSpace = True)
#             layout = self.hbox.layout()
#             layout.addWidget(self.widget_label)
# 
#         layout.addWidget(self.widget)
# 
#     def get_value(self):
#         if self.gui_type == 'multiple':
#             return self.widget.currentText()
#         elif self.gui_type == 'single':
#             return self.widget.text()
#         else:
#             return None
# 
#     def update(self, values):
#         self.widget.clear()
#         if self.gui_type == 'multiple':
#             for val in values:
#                 self.widget.addItem(val)
#         else:
#             self.widget.setText(values)
# 
#     def get_usable_value(self):
#         val = self.get_value().strip()
#         if val == 'None' or val == '' or val is None:
#             return None
#         if val in ('True', 'False'):
#             return True if val == 'True' else False
#         try:
#             try:
#                 if float(val) == int(val):
#                     if "." in val:
#                         return float(val)
#                     return int(val)
#             except ValueError:
#                 return float(val)
# 
#         except ValueError:
#             return val


# Path: orangecontrib/spark/widgets/data/spark_context.py
from collections import OrderedDict
from Orange.widgets import widget, gui
from Orange.widgets.settings import Setting
from pyspark import SparkConf, SparkContext
from pyspark.sql import HiveContext
from orangecontrib.spark.base.shared_spark_context import SharedSparkContext
from orangecontrib.spark.utils.gui_utils import GuiParam

        # The main label of the Control's GUI.
        # gui.label(self.controlArea, self, "Spark Context")

        self.conf = SparkConf()
        all_prefedined = dict(self.conf.getAll())
        # Create parameters Box.
        box = gui.widgetBox(self.controlArea, "Spark Application", addSpace = True)

        self.gui_parameters = OrderedDict()

        main_parameters = OrderedDict()
        main_parameters['spark.app.name'] = 'OrangeSpark'
        main_parameters['spark.master'] = 'yarn-client'
        main_parameters["spark.executor.instances"] = "8"
        main_parameters["spark.executor.cores"] = "4"
        main_parameters["spark.executor.memory"] = "8g"
        main_parameters["spark.driver.cores"] = "4"
        main_parameters["spark.driver.memory"] = "2g"
        main_parameters["spark.logConf"] = "false"
        main_parameters["spark.app.id"] = "dummy"

        for k, v in self.saved_gui_params.items():
            main_parameters[k] = v

        for k, v in main_parameters.items():
            default_value = all_prefedined.setdefault(k, v)
            self.gui_parameters[k] = GuiParam(parent_widget = box, label = k, default_value = v)
            all_prefedined.pop(k)

        for k, v in all_prefedined.items():

            self.gui_parameters[k] = GuiParam(parent_widget = box, label = k, default_value = str(v))