# 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 os.path
import pyodbc
import pandas as pd
import pandas.io.sql as psql
import Orange
from Orange.widgets.widget import OWWidget
from orangecontrib.spark.utils.data_utils import pandas_to_orange, format_sql
from PyQt4.QtGui import (
    QListView, QSizePolicy, QApplication, QAction, QKeySequence,
    QGraphicsLineItem, QSlider, QPainterPath, QSplitter, QPlainTextEdit, QFileDialog
)
from PyQt4 import QtCore
from Orange.widgets import widget, gui, settings
# from Orange.widgets import widget, gui, settings
# from Orange.widgets.utils import itemmodels, colorpalette
# from Orange.widgets.visualize.owscatterplotgraph import LegendItem, legend_anchor_pos
# from Orange.widgets.io import FileFormats




def convert_dataframe_to_orange(df):
    return pandas_to_orange(df)


class OWodbcTable(OWWidget):
    priority = 3
    allSQLSelectWidgets = []
    settingsList = ["recentConnections", "lastQuery"]
    name = "ODBC"
    description = "Create a Table from an ODBC datasource"
    icon = "../icons/sql.png"
    inputs = []
    outputs = [("Data", Orange.data.Table, widget.Default),
               ("Feature Definitions", Orange.data.Domain, widget.Default),
               ("Pandas", pd.DataFrame, widget.Default)]

    settingsHandler = settings.DomainContextHandler()

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

# 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/odbc_table.py
import os.path
import pyodbc
import pandas as pd
import pandas.io.sql as psql
import Orange
from Orange.widgets.widget import OWWidget
from orangecontrib.spark.utils.data_utils import pandas_to_orange, format_sql
from PyQt4.QtGui import (
    QListView, QSizePolicy, QApplication, QAction, QKeySequence,
    QGraphicsLineItem, QSlider, QPainterPath, QSplitter, QPlainTextEdit, QFileDialog
)
from PyQt4 import QtCore
from Orange.widgets import widget, gui, settings

# from Orange.widgets import widget, gui, settings
# from Orange.widgets.utils import itemmodels, colorpalette
# from Orange.widgets.visualize.owscatterplotgraph import LegendItem, legend_anchor_pos
# from Orange.widgets.io import FileFormats




def convert_dataframe_to_orange(df):
    return pandas_to_orange(df)


class OWodbcTable(OWWidget):
    priority = 3
    allSQLSelectWidgets = []
    settingsList = ["recentConnections", "lastQuery"]
    name = "ODBC"
    description = "Create a Table from an ODBC datasource"
    icon = "../icons/sql.png"
    inputs = []
    outputs = [("Data", Orange.data.Table, widget.Default),
               ("Feature Definitions", Orange.data.Domain, widget.Default),
               ("Pandas", pd.DataFrame, widget.Default)]

    settingsHandler = settings.DomainContextHandler()

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

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