#from quant_api import Quant_api import sys import json import importlib as im def is_request_validated(data, ptype): para = { "rebalance" : ["vendor_id", "pts"]} for parameter in para[ptype]: if not parameter in data.keys(): return False return True def rebalance(data): default_return_msg = {} if not is_request_validated(data, "rebalance"): default_return_msg.update({"errorCode":"2002", "errorMsg":"json format is incorrect."}) return json.dumps(default_return_msg) result = quant_api.rebalance(data["pts"]) result.update(default_return_msg) #quant_api.show_debug_info() return json.dumps(result,indent=2, sort_keys=True, ensure_ascii=False) def usage(): print( '\n\tUsage: %s vender mode portfiolio_file [recommand_file [ class_file [recordportfolio [now]] ]]' % sys.argv[0] ) print( ''' vender = sample (for testing) mode = test / pord portfiolio_file = 客戶持股比例檔 recommandile = 推薦檔 ( for testing: default: test_data/portfolio.json) class_file = 大類資產檔 ( for testing: default: test_data/class.json) recordportfolio = 歷史推薦檔 ( for testing: default: "") now = 測試用系統時間 (預設為當日,format: yyyymmdd)) ''' ) sys.exit() _NOW__ = '' if __name__ == '__main__': portfolioDataPath = "" assetClassPath = "" recordportfolio_DataPath = "" now = "" if len(sys.argv) >= 4: vender = sys.argv[1] mode = sys.argv[2] if mode == 'test' : data = json.load(open(sys.argv[3], encoding='utf-8')) if len(sys.argv) == 5 : portfolioDataPath = sys.argv[4] if len(sys.argv) == 6 : portfolioDataPath = sys.argv[4] assetClassPath = sys.argv[5] if len(sys.argv) == 7: portfolioDataPath = sys.argv[4] assetClassPath = sys.argv[5] recordportfolio_DataPath = sys.argv[6] if len(sys.argv) == 8: portfolioDataPath = sys.argv[4] assetClassPath = sys.argv[5] recordportfolio_DataPath = '' if sys.argv[6] == 'None' else sys.argv[6] _NOW__=sys.argv[7] elif mode == 'prod': data = json.load(open(sys.argv[3], encoding='utf-8')) else: usage() else: usage() ''' else: vender = sys.argv[1] mode = 'release' data = json.load(open(sys.argv[2])) ''' mod = im.import_module('vender.'+ vender + "_quant_api") Quant_api = getattr(mod, "Quant_api") if assetClassPath == "": def print2(*args): #print(*args) return from flask import Flask, current_app app = Flask(__name__) app.logger.error = print2 app.logger.warning = print2 app.logger.info = print2 app.logger.debug = print2 with app.app_context(): quant_api = Quant_api(mode, portfolioDataPath) print(rebalance(data)) else: #if recordportfolio_DataPath == "": # quant_api = Quant_api(mode, portfolioDataPath, assetClassPath) #else: quant_api = Quant_api(mode, [portfolioDataPath, recordportfolio_DataPath], assetClassPath, now=_NOW__) #quant_api = Quant_api(mode, [portfolioDataPath, recordportfolio_DataPath], assetClassPath) print(rebalance(data))