#!/usr/bin/env python
# -*- coding: utf-8 -*-
# time: 2023/3/17 14:46
# file: config.py
# author: David
# company: shenyang JY

import yaml
import argparse


class myargparse(argparse.ArgumentParser):
    def __init__(self, discription, add_help):
        super(myargparse, self).__init__(description=discription, add_help=add_help)

        self.add_argument('-c', '--config_yaml',default='config_xiushui.yml', type=str, metavar='FILE')
        self.add_argument('--norm_yaml', default='./data/xiushui/xiushui15/norm.yaml', type=str, metavar='FILE')

        self.add_argument('--input_size', type=int, default=0, help='输入维度')
        self.add_argument('--input_size_lstm', type=int, default=0, help='输入维度')
        self.add_argument('--input_size_cnn', type=int, default=0, help='输入维度')

        self.add_argument('--output_size', type=int, default=16, help='输出维度')  # 16个点

        # model_name 和 model_save_path 这两个参数根据yml中的参数拼接而成

        self.add_argument('--model_prefix', type=str, default=None, help='模型名称')

        self.add_argument('--save_name', type=str, default=None, help='保存名称')

        self.add_argument('--model_save_path', type=str, default=None, help='模型保存地址')

        self.add_argument('--columns_lstm', type=list, default=None, help='lstm列名')

        self.add_argument('--columns_cnn', type=list, default=None, help='cnn列名')


    def _init_dir(self, opt):
        import os, time
        # 在这里给opt赋值
        opt.model_prefix = "model_" + opt.continue_flag
        opt.model_save_path = './checkpoint/' + opt.model_prefix + "/"
        opt.save_name = "model_" + opt.save_frame + opt.model_postfix['keras']
        if not os.path.exists(opt.model_save_path):
            os.makedirs(opt.model_save_path)    # makedirs 递归创建目录
        if not os.path.exists(opt.figure_save_path):
            os.mkdir(opt.figure_save_path)
        if opt.do_train and (opt.do_log_save_to_file or opt.do_train_visualized):
            cur_time = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
            log_save_path = opt.log_save_path + cur_time + '_' + opt.used_frame + "/"
            os.makedirs(log_save_path)


# YAML should override the argparser's content
    def _parse_args_and_yaml(self):
        given_configs, remaining = self.parse_known_args()
        if given_configs.config_yaml:
            with open(given_configs.config_yaml, 'r', encoding='utf-8') as f:
                cfg = yaml.safe_load(f)
                self.set_defaults(**cfg)
        if given_configs.norm_yaml:
            with open(given_configs.norm_yaml, 'r', encoding='utf-8') as f:
                cfg = yaml.safe_load(f)
                print("归一化参数：", cfg)
                self.set_defaults(**cfg)

        # The main arg parser parses the rest of the args, the usual
        # defaults will have been overridden if config file specified.
        opt = self.parse_args(remaining)
        self._init_dir(opt)
        # Cache the args as a text string to save them in the output dir later
        opt_text = yaml.safe_dump(opt.__dict__, default_flow_style=False)
        return opt, opt_text


    def parse_args_and_yaml(self):
        return self._parse_args_and_yaml()[0]


if __name__ == "__main__":
    # opt = _parse_args_and_yaml()
    pass




