import ast
import sys
from typing import TYPE_CHECKING
import click
if TYPE_CHECKING:
pass
@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx: click.Context):
if not ctx.invoked_subcommand:
run()
@cli.command()
def run():
import vnstation.run
return vnstation.run.main()
improve: 浣跨敤璺ㄨ繘绋嬫寔缁€氫俊鏈哄埗锛屽彲浠ヤ繚璇佺埗杩涚▼閫€鍑烘椂瀛愯繘绋嬩篃閫€鍑猴紙瀛愯繘绋嬩富鍔ㄩ€€鍑猴紝鑰屼笉鏄鐖惰繘绋嬫潃鎺夛級
@cli.command()
@click.argument("setting")
def runtrader(setting: str = None):
s: dict = ast.literal_eval(setting)
run_trader(s)
@cli.command()
@click.option("--all", is_flag=True,
help="Print the version of all packages related to vnpy.",
)
def version(
all: bool = False,
):
if not all:
from . import version
print(version)
else:
from vnstation.update_manager import UpdateManager
pm = UpdateManager(None)
package_names = pm.package_names
for name in package_names:
version = pm.current_version_for_package(name)
print(f"{name}:{version}")
if name == 'main':
exit(cli())
def run_trader(setting: dict):
import importlib
from vnstation.info import APP_INFO, GATEWAY_INFO
from vnpy.trader.ui import create_qapp
from vnpy.event import EventEngine
from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import MainWindow as TraderMainWindow, excepthook
# Record process id into a file for later close purpose
import shelve
import os
import pathlib
pid = os.getpid()
pid_file_path = pathlib.Path.home().joinpath(".vnstation/trader_pid")
f = shelve.open(str(pid_file_path))
f["pid"] = pid
f.close()
qapp = create_qapp()
try:
event_engine = EventEngine()
main_engine = MainEngine(event_engine)
if setting:
gateway_setting = setting["gateway"]
for d in GATEWAY_INFO:
if gateway_setting.get(d["name"], False):
module = importlib.import_module(d["module"])
gateway = getattr(module, d["class"])
main_engine.add_gateway(gateway)
gateway_setting = setting["app"]
for d in APP_INFO:
if gateway_setting.get(d["name"], False):
module = importlib.import_module(d["module"])
app = getattr(module, d["class"])
main_engine.add_app(app)
# Show mainwindow
main_window = TraderMainWindow(main_engine, event_engine)
main_window.showMaximized()
except: # noqa
excepthook(*sys.exc_info())
qapp.exec_()