# 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/data_utils.py
# def pandas_to_orange(df):
#     domain, attributes, metas = construct_domain(df)
#     orange_table = Orange.data.Table.from_numpy(domain = domain, X = df[attributes].values, Y = None, metas = df[metas].values, W = None)
#     return orange_table
# 
# def format_sql(str_sql):
#     return str(sqlparse.format(str_sql, reindent = True, keyword_case = 'upper'))


import pyspark
from Orange.widgets import widget, gui
from Orange.widgets.settings import Setting
from Orange.widgets.widget import OWWidget
from PyQt4 import QtCore
from PyQt4.QtGui import (
    QSizePolicy, QSplitter, QPlainTextEdit
)
from orangecontrib.spark.base.shared_spark_context import SharedSparkContext
from orangecontrib.spark.utils.data_utils import pandas_to_orange, format_sql



def convert_dataframe_to_orange(df):
    return pandas_to_orange(df)


class OWSparkDataFrame(SharedSparkContext, OWWidget):
    priority = 2
    allSQLSelectWidgets = []
    lastQuery = Setting('')
    name = "Data Frame"
    description = "Create a Spark Dataframe from an SparkSQL source"
    icon = "../icons/sql.png"

    outputs = [("DataFrame", pyspark.sql.DataFrame, widget.Dynamic)]
    out_df = None

    def __init__(self):
        super().__init__()
        gui.label(self.controlArea, self, "Spark DataFrame")
        self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)

# 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/data_utils.py
# def pandas_to_orange(df):
#     domain, attributes, metas = construct_domain(df)
#     orange_table = Orange.data.Table.from_numpy(domain = domain, X = df[attributes].values, Y = None, metas = df[metas].values, W = None)
#     return orange_table
# 
# def format_sql(str_sql):
#     return str(sqlparse.format(str_sql, reindent = True, keyword_case = 'upper'))


# Path: orangecontrib/spark/widgets/data/spark_sql_dataframe.py
import pyspark
from Orange.widgets import widget, gui
from Orange.widgets.settings import Setting
from Orange.widgets.widget import OWWidget
from PyQt4 import QtCore
from PyQt4.QtGui import (
    QSizePolicy, QSplitter, QPlainTextEdit
)
from orangecontrib.spark.base.shared_spark_context import SharedSparkContext
from orangecontrib.spark.utils.data_utils import pandas_to_orange, format_sql




def convert_dataframe_to_orange(df):
    return pandas_to_orange(df)


class OWSparkDataFrame(SharedSparkContext, OWWidget):
    priority = 2
    allSQLSelectWidgets = []
    lastQuery = Setting('')
    name = "Data Frame"
    description = "Create a Spark Dataframe from an SparkSQL source"
    icon = "../icons/sql.png"

    outputs = [("DataFrame", pyspark.sql.DataFrame, widget.Dynamic)]
    out_df = None

    def __init__(self):
        super().__init__()
        gui.label(self.controlArea, self, "Spark DataFrame")
        self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)

        self.allSQLSelectWidgets.append(self)  # set default settings