用新的路径寻找方式替换原本的os.getcwd()从而支持任意目录启动vn.trader

This commit is contained in:
chenxy123 2016-10-28 22:55:46 +08:00
parent 5ad2c56845
commit 2e92f5c891
19 changed files with 82 additions and 22 deletions

View File

@ -33,7 +33,8 @@ from vtFunction import todayDate
class CtaEngine(object): class CtaEngine(object):
"""CTA策略引擎""" """CTA策略引擎"""
settingFileName = 'CTA_setting.json' settingFileName = 'CTA_setting.json'
settingFileName = os.getcwd() + '/ctaAlgo/' + settingFileName path = os.path.abspath(os.path.dirname(__file__))
settingFileName = os.path.join(path, settingFileName)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self, mainEngine, eventEngine): def __init__(self, mainEngine, eventEngine):

View File

@ -31,6 +31,8 @@ class DatayesClient(object):
def loadSetting(self): def loadSetting(self):
"""载入配置""" """载入配置"""
try: try:
path = os.path.abspath(os.path.dirname(__file__))
FILENAME = os.path.join(path, FILENAME)
f = file(FILENAME) f = file(FILENAME)
except IOError: except IOError:
print u'%s无法打开配置文件' % self.name print u'%s无法打开配置文件' % self.name

View File

@ -87,7 +87,8 @@ class CtpGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/ctpGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -25,7 +25,8 @@ class DrEngine(object):
"""数据记录引擎""" """数据记录引擎"""
settingFileName = 'DR_setting.json' settingFileName = 'DR_setting.json'
settingFileName = os.getcwd() + '/dataRecorder/' + settingFileName path = os.path.abspath(os.path.dirname(__file__))
settingFileName = os.path.join(path, settingFileName)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self, mainEngine, eventEngine): def __init__(self, mainEngine, eventEngine):

View File

@ -78,7 +78,8 @@ class FemasGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/femasGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -143,7 +143,8 @@ class IbGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/ibGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -51,7 +51,8 @@ class KsgoldGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/ksgoldGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -74,7 +74,8 @@ class KsotpGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/ksotpGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -76,7 +76,8 @@ class LtsGateway(VtGateway):
"""连接""" """连接"""
# 载入json 文件 # 载入json 文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/ltsGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -58,7 +58,8 @@ class OandaGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/oandaGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -107,7 +107,8 @@ class OkcoinGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/okcoinGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -20,7 +20,8 @@ from vtGateway import VtLogData
class RmEngine(object): class RmEngine(object):
"""风控引擎""" """风控引擎"""
settingFileName = 'RM_setting.json' settingFileName = 'RM_setting.json'
settingFileName = os.getcwd() + '/riskManager/' + settingFileName path = os.path.abspath(os.path.dirname(__file__))
settingFileName = os.path.join(path, settingFileName)
name = u'风控模块' name = u'风控模块'

View File

@ -94,7 +94,8 @@ class SgitGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/sgitGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -77,7 +77,8 @@ class ShzdGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/shzdGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)

View File

@ -2,6 +2,7 @@
import json import json
import csv import csv
import os
from collections import OrderedDict from collections import OrderedDict
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
@ -14,8 +15,12 @@ from vtGateway import *
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def loadFont(): def loadFont():
"""载入字体设置""" """载入字体设置"""
fileName = 'VT_setting.json'
path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file("VT_setting.json") f = file(fileName)
setting = json.load(f) setting = json.load(f)
family = setting['fontFamily'] family = setting['fontFamily']
size = setting['fontSize'] size = setting['fontSize']

View File

@ -1,9 +1,10 @@
# encoding: UTF-8 # encoding: UTF-8
""" """
包含一些开中常用的函数 包含一些开中常用的函数
""" """
import os
import decimal import decimal
import json import json
from datetime import datetime from datetime import datetime
@ -30,8 +31,12 @@ def safeUnicode(value):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def loadMongoSetting(): def loadMongoSetting():
"""载入MongoDB数据库的配置""" """载入MongoDB数据库的配置"""
fileName = 'VT_setting.json'
path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file("VT_setting.json") f = file(fileName)
setting = json.load(f) setting = json.load(f)
host = setting['mongoHost'] host = setting['mongoHost']
port = setting['mongoPort'] port = setting['mongoPort']

View File

@ -1,12 +1,22 @@
# encoding: UTF-8 # encoding: UTF-8
import sys import sys
import os
import ctypes import ctypes
import platform import platform
import vtPath
from vtEngine import MainEngine from vtEngine import MainEngine
from uiMainWindow import * from uiMainWindow import *
# 文件路径名
path = os.path.abspath(os.path.dirname(__file__))
ICON_FILENAME = 'vnpy.ico'
ICON_FILENAME = os.path.join(path, ICON_FILENAME)
SETTING_FILENAME = 'VT_setting.json'
SETTING_FILENAME = os.path.join(path, SETTING_FILENAME)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def main(): def main():
"""主程序入口""" """主程序入口"""
@ -20,12 +30,12 @@ def main():
# 初始化Qt应用对象 # 初始化Qt应用对象
app = QtGui.QApplication(sys.argv) app = QtGui.QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('vnpy.ico')) app.setWindowIcon(QtGui.QIcon(ICON_FILENAME))
app.setFont(BASIC_FONT) app.setFont(BASIC_FONT)
# 设置Qt的皮肤 # 设置Qt的皮肤
try: try:
f = file("VT_setting.json") f = file(SETTING_FILENAME)
setting = json.load(f) setting = json.load(f)
if setting['darkStyle']: if setting['darkStyle']:
import qdarkstyle import qdarkstyle

24
vn.trader/vtPath.py Normal file
View File

@ -0,0 +1,24 @@
# encoding: UTF-8
"""
用于vn.trader的运行目录环境设置
"""
import os
import sys
# 将根目录路径添加到环境变量中
ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
sys.path.append(ROOT_PATH)
# 将功能模块的目录路径添加到环境变量中
# 若各目录下存在同名文件可能导致异常,请注意测试
MODULE_PATH = {}
MODULE_PATH['CTA'] = os.path.join(ROOT_PATH, 'ctaAlgo')
MODULE_PATH['RM'] = os.path.join(ROOT_PATH, 'riskManager')
MODULE_PATH['DR'] = os.path.join(ROOT_PATH, 'dataRecorder')
# 添加到环境变量中
for path in MODULE_PATH.values():
if path not in sys.path:
sys.path.append(path)

View File

@ -76,7 +76,8 @@ class XspeedGateway(VtGateway):
"""连接""" """连接"""
# 载入json文件 # 载入json文件
fileName = self.gatewayName + '_connect.json' fileName = self.gatewayName + '_connect.json'
fileName = os.getcwd() + '/xspeedGateway/' + fileName path = os.path.abspath(os.path.dirname(__file__))
fileName = os.path.join(path, fileName)
try: try:
f = file(fileName) f = file(fileName)