# Path: kquant_data/wind/wset.py
# def write_constituent(path, df):
#     df.to_csv(path, encoding='utf-8-sig', date_format='%Y-%m-%d', index=False)
# 
# def read_constituent(path):
#     """
#     读取板块文件
#     :param path:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, encoding='utf-8-sig', parse_dates=True)
#     except Exception as e:
#         return None
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except KeyError:
#         pass
#     return df
# 
# def download_sectorconstituent(w, date, sector, windcode, field='wind_code'):
#     """
#     板块成份
#     中信证券一级行业指数：时间好像没有必要，因为日历日也会查询出来
#     风险警示股票：日期就是查询的日期，股票也是最新名，没有啥用
# 
#     w.wset("sectorconstituent","date=2017-03-03;sectorid=a001010100000000;field=wind_code")
#     w.wset("sectorconstituent","date=2017-03-03;sectorid=a001010100000000")
#     w.wset("sectorconstituent","date=2017-03-03;windcode=000300.SH")
#     :param w:
#     :param sector:
#     :param date:
#     :return:
#     """
#     param = 'date=%s' % date
#     if sector:
#         param += ';sector=%s' % sector
#     if windcode:
#         param += ';windcode=%s' % windcode
#     if field:
#         param += ';field=%s' % field
# 
#     w.asDateTime = asDateTime
#     w_wset_data = w.wset("sectorconstituent", param)
#     df = pd.DataFrame(w_wset_data.Data)
#     df = df.T
#     df.columns = w_wset_data.Fields
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except KeyError:
#         pass
#     return df
# 
# def download_indexconstituent(w, date, windcode, field='wind_code,i_weight'):
#     """
#     指数权重
#     如果指定日期不是交易日，会返回时前一个交易日的信息
#     :param w:
#     :param windcode:
#     :param date:
#     :return:
#     """
#     param = 'date=%s' % date
#     if windcode:
#         param += ';windcode=%s' % windcode
#     if field:
#         param += ';field=%s' % field
# 
#     w.asDateTime = asDateTime
#     w_wset_data = w.wset("indexconstituent", param)
#     df = pd.DataFrame(w_wset_data.Data)
#     df = df.T
#     df.columns = w_wset_data.Fields
#     return df
# 
# def download_futureoir(w, startdate, enddate, windcode):
#     """
#     品种持仓
#     是将多仓与空仓排名合并到一起，然后按多仓进行统一排名，所以对于空仓要使用时，需要自行重新排名
#     查询的跨度不要太长，一个月或三个月
#     :param w:
#     :param startdate:
#     :param enddate:
#     :return:
#     """
#     w.asDateTime = asDateTime
#     w_wset_data = w.wset("futureoir",
#                          "startdate=%s;enddate=%s;varity=%s;order_by=long;ranks=all;"
#                          "field=date,ranks,member_name,"
#                          "long_position,long_position_increase,long_potion_rate,"
#                          "short_position,short_position_increase,short_position_rate,"
#                          "vol,vol_increase,vol_rate,settle" % (startdate, enddate, windcode))
#     df = pd.DataFrame(w_wset_data.Data)
#     df = df.T
#     df.columns = w_wset_data.Fields
# 
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except:
#         pass
# 
#     return df


import os
import shutil
import pandas as pd
from datetime import datetime
from ..wind.wset import write_constituent, read_constituent, download_sectorconstituent, download_indexconstituent, \
    download_futureoir
    :param w:
    :param dates:
    :param path:
    :param file_ext:
    :param start:
    :param end:
    :param sector:None表示没有权块信息，那就是权重信息了
    :param windcode:
    :param field:
    :param two_sides:用在一开始数据下载时补两头
    :return:
    """
    len = end - start
    if len < 1:
        # 两个相邻，也需要进行操作
        return

    date = dates[start]
    fullpath = os.path.join(path, date + file_ext)
    df_start = read_constituent(fullpath)
    if df_start is None:
        print("下载:%s" % date)
        if not is_indexconstituent:
            df_start = download_sectorconstituent(w, date, sector, windcode, field)
        else:
            df_start = download_indexconstituent(w, date, windcode, field)
        write_constituent(fullpath, df_start)

    date = dates[end]
    fullpath = os.path.join(path, date + file_ext)

# Path: kquant_data/wind/wset.py
# def write_constituent(path, df):
#     df.to_csv(path, encoding='utf-8-sig', date_format='%Y-%m-%d', index=False)
# 
# def read_constituent(path):
#     """
#     读取板块文件
#     :param path:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, encoding='utf-8-sig', parse_dates=True)
#     except Exception as e:
#         return None
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except KeyError:
#         pass
#     return df
# 
# def download_sectorconstituent(w, date, sector, windcode, field='wind_code'):
#     """
#     板块成份
#     中信证券一级行业指数：时间好像没有必要，因为日历日也会查询出来
#     风险警示股票：日期就是查询的日期，股票也是最新名，没有啥用
# 
#     w.wset("sectorconstituent","date=2017-03-03;sectorid=a001010100000000;field=wind_code")
#     w.wset("sectorconstituent","date=2017-03-03;sectorid=a001010100000000")
#     w.wset("sectorconstituent","date=2017-03-03;windcode=000300.SH")
#     :param w:
#     :param sector:
#     :param date:
#     :return:
#     """
#     param = 'date=%s' % date
#     if sector:
#         param += ';sector=%s' % sector
#     if windcode:
#         param += ';windcode=%s' % windcode
#     if field:
#         param += ';field=%s' % field
# 
#     w.asDateTime = asDateTime
#     w_wset_data = w.wset("sectorconstituent", param)
#     df = pd.DataFrame(w_wset_data.Data)
#     df = df.T
#     df.columns = w_wset_data.Fields
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except KeyError:
#         pass
#     return df
# 
# def download_indexconstituent(w, date, windcode, field='wind_code,i_weight'):
#     """
#     指数权重
#     如果指定日期不是交易日，会返回时前一个交易日的信息
#     :param w:
#     :param windcode:
#     :param date:
#     :return:
#     """
#     param = 'date=%s' % date
#     if windcode:
#         param += ';windcode=%s' % windcode
#     if field:
#         param += ';field=%s' % field
# 
#     w.asDateTime = asDateTime
#     w_wset_data = w.wset("indexconstituent", param)
#     df = pd.DataFrame(w_wset_data.Data)
#     df = df.T
#     df.columns = w_wset_data.Fields
#     return df
# 
# def download_futureoir(w, startdate, enddate, windcode):
#     """
#     品种持仓
#     是将多仓与空仓排名合并到一起，然后按多仓进行统一排名，所以对于空仓要使用时，需要自行重新排名
#     查询的跨度不要太长，一个月或三个月
#     :param w:
#     :param startdate:
#     :param enddate:
#     :return:
#     """
#     w.asDateTime = asDateTime
#     w_wset_data = w.wset("futureoir",
#                          "startdate=%s;enddate=%s;varity=%s;order_by=long;ranks=all;"
#                          "field=date,ranks,member_name,"
#                          "long_position,long_position_increase,long_potion_rate,"
#                          "short_position,short_position_increase,short_position_rate,"
#                          "vol,vol_increase,vol_rate,settle" % (startdate, enddate, windcode))
#     df = pd.DataFrame(w_wset_data.Data)
#     df = df.T
#     df.columns = w_wset_data.Fields
# 
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except:
#         pass
# 
#     return df


# Path: kquant_data/wind_resume/wset.py
import os
import shutil
import pandas as pd
from datetime import datetime
from ..wind.wset import write_constituent, read_constituent, download_sectorconstituent, download_indexconstituent, \
    download_futureoir

    :param w:
    :param dates:
    :param path:
    :param file_ext:
    :param start:
    :param end:
    :param sector:None表示没有权块信息，那就是权重信息了
    :param windcode:
    :param field:
    :param two_sides:用在一开始数据下载时补两头
    :return:
    """
    len = end - start
    if len < 1:
        # 两个相邻，也需要进行操作
        return

    date = dates[start]
    fullpath = os.path.join(path, date + file_ext)
    df_start = read_constituent(fullpath)
    if df_start is None:
        print("下载:%s" % date)
        if not is_indexconstituent:
            df_start = download_sectorconstituent(w, date, sector, windcode, field)
        else:
            df_start = download_indexconstituent(w, date, windcode, field)
        write_constituent(fullpath, df_start)

    date = dates[end]
    fullpath = os.path.join(path, date + file_ext)

    df_end = read_constituent(fullpath)