# Path: kquant_data/wind/tdays.py
# def read_tdays(path):
#     try:
#         df = pd.read_csv(path, parse_dates=True)
#     except:
#         return None
# 
#     df['date'] = pd.to_datetime(df['date'])
#     df.index = df['date']
#     return df
# 
# Path: kquant_data/xio/csv.py
# def read_data_dataframe(path, sep=','):
#     """
#     读取季报的公告日
#     注意：有些股票多个季度一起发，一般是公司出问题了，特别是600878，四个报告同一天发布
#     年报与一季报很有可能一起发
#     :param path:
#     :param sep:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, index_col=0, parse_dates=True, encoding='utf-8-sig', sep=sep)
#     except (FileNotFoundError, OSError):
#         return None
# 
#     return df
# 
# Path: kquant_data/wind_resume/wset.py
# def download_index_weight(w, trading_days, wind_code, root_path):
#     """
#     下载指数成份和权重
#     :param w:
#     :param trading_days:
#     :param wind_code:
#     :param root_path:
#     :return:
#     """
#     df = trading_days
#     df['date_str'] = trading_days['date'].astype(str)
# 
#     foldpath = os.path.join(root_path, wind_code)
#     file_download_constituent(w, df['date_str'], foldpath, '.csv',
#                               sector=None, windcode=wind_code, field='wind_code,i_weight', is_indexconstituent=True)
# 
#     dst_path = os.path.join(root_path, "%s_move" % wind_code)
#     move_constituent(foldpath, dst_path)
# 
# Path: kquant_data/config.py
# __CONFIG_H5_STK_FACTOR_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'factor')
# 
# __CONFIG_H5_STK_WEIGHT_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'indexconstituent')
# 
# __CONFIG_TDAYS_SSE_FILE__ = os.path.join(__CONFIG_H5_STK_DIR__, 'tdays', 'SSE.csv')


import os
from WindPy import w
from datetime import datetime
from kquant_data.wind.tdays import read_tdays
from kquant_data.xio.csv import read_data_dataframe
from kquant_data.wind_resume.wset import download_index_weight
from kquant_data.config import __CONFIG_H5_STK_FACTOR_DIR__, __CONFIG_H5_STK_WEIGHT_DIR__, __CONFIG_TDAYS_SSE_FILE__
可能有部分数据出现nan的情况，如纳入成份股时
"""



def download_000300(w, trading_days):
    # 4月8号是指数的发布日期
    trading_days = trading_days['2005-04-08':date_str]

    # 下载多天数据,以另一数据做为标准来下载
    # 比如交易数据是10月8号，那就得取10月7号，然后再平移到8号，如果7号没有数据那就得9月30号
    path = os.path.join(__CONFIG_H5_STK_FACTOR_DIR__, 'test.csv')
    date_index = read_data_dataframe(path)

    # dates = list(date_index.index)

    download_index_weight(w, trading_days, "000300.SH", __CONFIG_H5_STK_WEIGHT_DIR__)


def download_000016(w, trading_days):
    # 万得数据只到2009年4月1号，之前的数据没有
    trading_days = trading_days['2009-04-01':date_str]

    download_index_weight(w, trading_days, "000016.SH", __CONFIG_H5_STK_WEIGHT_DIR__)


def download_000905(w, trading_days):
    # 万得数据只到2009年4月1号，之前的数据没有
    trading_days = trading_days['2007-01-31':date_str]


# Path: kquant_data/wind/tdays.py
# def read_tdays(path):
#     try:
#         df = pd.read_csv(path, parse_dates=True)
#     except:
#         return None
# 
#     df['date'] = pd.to_datetime(df['date'])
#     df.index = df['date']
#     return df
# 
# Path: kquant_data/xio/csv.py
# def read_data_dataframe(path, sep=','):
#     """
#     读取季报的公告日
#     注意：有些股票多个季度一起发，一般是公司出问题了，特别是600878，四个报告同一天发布
#     年报与一季报很有可能一起发
#     :param path:
#     :param sep:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, index_col=0, parse_dates=True, encoding='utf-8-sig', sep=sep)
#     except (FileNotFoundError, OSError):
#         return None
# 
#     return df
# 
# Path: kquant_data/wind_resume/wset.py
# def download_index_weight(w, trading_days, wind_code, root_path):
#     """
#     下载指数成份和权重
#     :param w:
#     :param trading_days:
#     :param wind_code:
#     :param root_path:
#     :return:
#     """
#     df = trading_days
#     df['date_str'] = trading_days['date'].astype(str)
# 
#     foldpath = os.path.join(root_path, wind_code)
#     file_download_constituent(w, df['date_str'], foldpath, '.csv',
#                               sector=None, windcode=wind_code, field='wind_code,i_weight', is_indexconstituent=True)
# 
#     dst_path = os.path.join(root_path, "%s_move" % wind_code)
#     move_constituent(foldpath, dst_path)
# 
# Path: kquant_data/config.py
# __CONFIG_H5_STK_FACTOR_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'factor')
# 
# __CONFIG_H5_STK_WEIGHT_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'indexconstituent')
# 
# __CONFIG_TDAYS_SSE_FILE__ = os.path.join(__CONFIG_H5_STK_DIR__, 'tdays', 'SSE.csv')


# Path: demo_stock/A_1day/E01_download_indexconstituent.py
import os
from WindPy import w
from datetime import datetime
from kquant_data.wind.tdays import read_tdays
from kquant_data.xio.csv import read_data_dataframe
from kquant_data.wind_resume.wset import download_index_weight
from kquant_data.config import __CONFIG_H5_STK_FACTOR_DIR__, __CONFIG_H5_STK_WEIGHT_DIR__, __CONFIG_TDAYS_SSE_FILE__

可能有部分数据出现nan的情况，如纳入成份股时
"""



def download_000300(w, trading_days):
    # 4月8号是指数的发布日期
    trading_days = trading_days['2005-04-08':date_str]

    # 下载多天数据,以另一数据做为标准来下载
    # 比如交易数据是10月8号，那就得取10月7号，然后再平移到8号，如果7号没有数据那就得9月30号
    path = os.path.join(__CONFIG_H5_STK_FACTOR_DIR__, 'test.csv')
    date_index = read_data_dataframe(path)

    # dates = list(date_index.index)

    download_index_weight(w, trading_days, "000300.SH", __CONFIG_H5_STK_WEIGHT_DIR__)


def download_000016(w, trading_days):
    # 万得数据只到2009年4月1号，之前的数据没有
    trading_days = trading_days['2009-04-01':date_str]

    download_index_weight(w, trading_days, "000016.SH", __CONFIG_H5_STK_WEIGHT_DIR__)


def download_000905(w, trading_days):
    # 万得数据只到2009年4月1号，之前的数据没有
    trading_days = trading_days['2007-01-31':date_str]


    download_index_weight(w, trading_days, "000905.SH", __CONFIG_H5_STK_WEIGHT_DIR__)