mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-11-30 10:59:43 +08:00
stock house (continue)
This commit is contained in:
parent
ab45778e4f
commit
a5df0098f9
@ -53,5 +53,6 @@ from .deprecated import *
|
||||
from .indicator import *
|
||||
from .trade_manage import *
|
||||
from .trade_sys import *
|
||||
from .stockhouse import *
|
||||
|
||||
__version__ = get_version()
|
||||
|
@ -8,23 +8,41 @@
|
||||
#===============================================================================
|
||||
|
||||
import os
|
||||
import sys
|
||||
import git
|
||||
from configparser import ConfigParser
|
||||
|
||||
from sqlalchemy import create_engine, Sequence, Column, Integer, String, and_
|
||||
from sqlalchemy import (create_engine, Sequence, Column, Integer, String, and_, UniqueConstraint)
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class RemoteHouse(Base):
|
||||
__tablename__ = 'remote_house'
|
||||
class ConfigModel(Base):
|
||||
__tablename__ = 'house_config'
|
||||
id = Column(Integer, Sequence('config_id_seq'), primary_key=True)
|
||||
key = Column(String, index=True) # 参数名
|
||||
value = Column(String) # 参数值
|
||||
|
||||
__table_args__ = (UniqueConstraint('key'), )
|
||||
|
||||
def __str__(self):
|
||||
return "ConfigModel(id={}, key={}, value={})".format(self.id, self.key, self.value)
|
||||
|
||||
def __repr__(self):
|
||||
return "<{}>".format(self.__str__())
|
||||
|
||||
|
||||
class HouseModel(Base):
|
||||
__tablename__ = 'house_repo'
|
||||
id = Column(Integer, Sequence('remote_id_seq'), primary_key=True)
|
||||
name = Column(String, index=True) # 本地仓库名
|
||||
house_type = Column(String)
|
||||
local = Column(String) # 本地地址
|
||||
url = Column(String) # git 仓库地址
|
||||
branch = Column(String) # 远程仓库分支
|
||||
local = Column(String) # 本地缓存地址
|
||||
|
||||
__table_args__ = (UniqueConstraint('name'), )
|
||||
|
||||
def __str__(self):
|
||||
return "RemoteHouse(id={}, url={}, local={})".format(self.id, self.url, self.local)
|
||||
@ -33,6 +51,15 @@ class RemoteHouse(Base):
|
||||
return "<{}>".format(self.__str__())
|
||||
|
||||
|
||||
class PartModel(Base):
|
||||
__tablename__ = 'house_part'
|
||||
id = Column(Integer, Sequence('part_id_seq'), primary_key=True)
|
||||
house_id = Column(Integer) #所属仓库标识
|
||||
part = Column(String) # 部件类型
|
||||
name = Column(String) # 策略名称
|
||||
path = Column(String) # 策略路径
|
||||
|
||||
|
||||
class StockHouse(object):
|
||||
"""策略库管理"""
|
||||
def __init__(self):
|
||||
@ -44,46 +71,70 @@ class StockHouse(object):
|
||||
Session = sessionmaker(bind=engine)
|
||||
self._session = Session()
|
||||
|
||||
# 获取策略仓库配置信息
|
||||
self.config_file = "{}/.hikyuu/stockhouse.ini".format(usr_dir)
|
||||
self.config = ConfigParser()
|
||||
|
||||
if os.path.exists(self.config_file):
|
||||
self.config.read(self.config_file, encoding='utf-8')
|
||||
# 检查并建立远端仓库的本地缓存目录
|
||||
self.remote_cache_dir = self._session.query(ConfigModel.value
|
||||
).filter(ConfigModel.key == 'remote_cache_dir'
|
||||
).first()
|
||||
if self.remote_cache_dir is None:
|
||||
self.remote_cache_dir = "{}/.hikyuu/stockhouse".format(usr_dir)
|
||||
record = ConfigModel(key='remote_cache_dir', value=self.remote_cache_dir)
|
||||
self._session.add(record)
|
||||
self._session.commit()
|
||||
else:
|
||||
# 如果配置文件不存在,则创建默认配置信息
|
||||
# 创建本地默认策略仓库缓存目录
|
||||
house_dir = "{}/.hikyuu/stockhouse".format(usr_dir)
|
||||
if not os.path.lexists(house_dir):
|
||||
os.makedirs(house_dir)
|
||||
self.config["remote"] = {
|
||||
"cache": house_dir,
|
||||
"hikyuu": "https://gitee.com/fasiondog/stockhouse.git"
|
||||
}
|
||||
self.add_remote_house(self.config['remote']['hikyuu'], 'hikyuu', 'master')
|
||||
self.remote_cache_dir = self.remote_cache_dir[0]
|
||||
|
||||
self._save_config()
|
||||
if not os.path.lexists(self.remote_cache_dir):
|
||||
os.makedirs(self.remote_cache_dir)
|
||||
|
||||
def _save_config(self):
|
||||
"""保存配置信息"""
|
||||
with open(self.config_file, 'w', encoding='utf-8') as f:
|
||||
self.config.write(f)
|
||||
# 检查并下载 hikyuu 策略仓库
|
||||
hikyuu_house_path = self._session.query(HouseModel.local
|
||||
).filter(HouseModel.name == 'hikyuu_house').first()
|
||||
if hikyuu_house_path is None:
|
||||
self.add_remote_house(
|
||||
'hikyuu_house', 'https://gitee.com/fasiondog/stockhouse.git', 'master'
|
||||
)
|
||||
|
||||
def add_remote_house(self, url, name, branch='master'):
|
||||
record = self._session.query(RemoteHouse).filter(RemoteHouse.name == name).first()
|
||||
def add_remote_house(self, name, url, branch='master'):
|
||||
"""增加远程策略仓库
|
||||
|
||||
:param str name: 仓库名称(自行起名)
|
||||
:param str url: git 仓库地址
|
||||
:param str branch: git 仓库分支
|
||||
"""
|
||||
record = self._session.query(HouseModel).filter(HouseModel.name == name).first()
|
||||
assert record is None, '本地仓库名重复'
|
||||
|
||||
record = self._session.query(RemoteHouse).filter(
|
||||
and_(RemoteHouse.url == url, RemoteHouse.branch == branch)
|
||||
record = self._session.query(HouseModel).filter(
|
||||
and_(HouseModel.url == url, HouseModel.branch == branch)
|
||||
).first()
|
||||
assert record is None, '该仓库分支已存在'
|
||||
|
||||
local_cache = "{}/{}".format(self.config["remote"]["cache"], name)
|
||||
clone = git.Repo.clone_from(url, local_cache, branch=branch)
|
||||
record = RemoteHouse(name=name, url=url, branch=branch, local=local_cache)
|
||||
# 如果存在同名缓存目录,则强制删除
|
||||
local_dir = "{}/{}".format(self.remote_cache_dir, name)
|
||||
if os.path.lexists(local_dir):
|
||||
os.removedirs(local_dir)
|
||||
|
||||
print('正在克隆至 "stockhouse/{}"'.format(name))
|
||||
try:
|
||||
clone = git.Repo.clone_from(url, local_dir, branch=branch)
|
||||
except:
|
||||
raise RuntimeError("请检查网络是否正常或链接地址({})是否正确!".format(url))
|
||||
|
||||
# 导入仓库各部件策略信息
|
||||
self.import_part_to_db(local_dir)
|
||||
|
||||
# 更新仓库记录
|
||||
record = HouseModel(name=name, house_type='remote', url=url, branch=branch, local=local_dir)
|
||||
self._session.add(record)
|
||||
self._session.commit()
|
||||
|
||||
def import_part_to_db(self, local_dir):
|
||||
for root, dirs, files in os.walk(local_dir):
|
||||
for dir_name in dirs:
|
||||
print(dir_name)
|
||||
for file_name in files:
|
||||
print(file_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
house = StockHouse()
|
Loading…
Reference in New Issue
Block a user