Skip to content

Commit

Permalink
修正类加载逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
woldy committed Dec 28, 2023
1 parent c752a78 commit fbca819
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 16 deletions.
2 changes: 1 addition & 1 deletion database/finhack_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ CREATE TABLE `factors_mining` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2023-12-26 18:40:11
-- Dump completed on 2023-12-27 13:30:47
1 change: 1 addition & 0 deletions examples/demo-project/loader/testmodule_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def testaction(self):
Log.logger.debug("testarg1 is:"+str(self.args.testarg1))
Log.logger.debug("testarg2 is:"+str(self.args.testarg2))
obj=self.klass()
obj.args=self.args
obj.run()


Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import runtime.global_var as global_var

import time
class Testmodule():
class DefaultTestmodule():
def __init__(self):
pass
def run(self):
Log.logger.debug("----%s----" % (__file__))
Log.logger.debug("this is Testmodule")
Log.logger.debug("vendor is default")
print(self.args)
while True:
time.sleep(1)
Log.logger.debug(".")


def run2(self):
print(self.args)
print('run2')
1 change: 1 addition & 0 deletions finhack/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.0.1.dev4'
6 changes: 3 additions & 3 deletions finhack/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import finhack.library.log as Log
from finhack.library.utils import Utils
from finhack.library.class_loader import ClassLoader
from finhack.core.version import get_version
import sys


Expand All @@ -28,8 +29,6 @@ def __init__(self,project_path=''):
self.append_args()
self.generate_global()
self.init_logger()



def init_logger(self):
from runtime.constant import LOGS_DIR
Expand Down Expand Up @@ -169,7 +168,8 @@ def do_action(self):
if hasattr(loader_obj, self.action):
method = getattr(loader_obj, self.action)
else:
klass=loader_obj.klass()
klass=loader_obj.klass
klass.args=self.args
method = getattr(klass, self.action)

method()
9 changes: 6 additions & 3 deletions finhack/core/loader/base_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ def __init__(self,args):
self.user_module_path=BASE_DIR+"/"+self.module_name+"/"+self.vendor+"/"+self.vendor+'_'+self.module_name+".py"
self.module = ClassLoader.get_module(module_path=self.module_path,user_module_path=self.user_module_path)
try:
self.klass = getattr(self.module, self.vendor.capitalize()+self.module_name.capitalize())
self.klass = getattr(self.module, self.vendor.capitalize()+self.module_name.capitalize())()
self.klass.args=self.args
except Exception as e:
if "has no attribute" in str(e) and self.vendor.capitalize()+self.module_name.capitalize() in str(e):
if os.path.exists(self.module_path) or os.path.exists(self.user_module_path):
Log.logger.error( self.vendor.capitalize()+self.module_name.capitalize()+"类不存在")
elif os.path.exists(self.module_path) and os.path.exists(self.user_module_path):
Log.logger.error(self.module_path.replace('.','/')+".py,"+self.user_module_path+"均不存在")
else:
class_name=self.vendor.capitalize()+self.module_name.capitalize()
Log.logger.error(str(e))
Log.logger.error("请根据堆栈信息检查是否在包名错误、包未使用pip安装、包中有错误语法或引用等问题")
Log.logger.error(f"请检查是否存在{class_name}相关文件,是否在包名错误、包未使用pip安装、包中有错误语法或引用等问题")
Log.logger.error(f"提示:可重点关注{self.user_module_path},以及对应类名{class_name}")
print("Traceback:", file=sys.stderr)
traceback.print_tb(e.__traceback__)
exit()
Expand All @@ -48,7 +51,7 @@ def background(self,args):


def run(self):
klass=self.klass()
klass=self.klass
klass.run()


Expand Down
Empty file added finhack/core/notify.py
Empty file.
5 changes: 5 additions & 0 deletions finhack/core/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import finhack
def get_version(package_name='finhack'):
return finhack.__version__

finhack_version = get_version()
11 changes: 7 additions & 4 deletions finhack/library/class_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ def get_module(module_path='',user_module_path=''):
module_name = "user_module"
if filename.endswith(".py"):
module_name = filename[:-3]
module_spec = importlib.util.spec_from_file_location(module_name, user_module_path)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
try:
module_spec = importlib.util.spec_from_file_location(module_name, user_module_path)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
except ModuleNotFoundError as e:
traceback.print_tb(e.__traceback__)
elif not os.path.exists(user_module_path):
try:
module=importlib.import_module(module_path)
except ModuleNotFoundError as e:
Log.logger.warning(f"Module '{module_path}' does not exist,use base_loader")
traceback.print_tb(e.__traceback__)
#traceback.print_tb(e.__traceback__)
module=importlib.import_module('finhack.core.loader.base_loader')

else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FRAMEWORK_DIR="/root/anaconda3/envs/finhack/lib/python3.9/site-packages/finhack"
FRAMEWORK_DIR="/root/anaconda3/lib/python3.9/site-packages/finhack"
BASE_DIR="/data/code/finhack/examples/demo-project"
DATA_DIR=BASE_DIR+"/data/"
CACHE_DIR=DATA_DIR+"cache/"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import finhack.library.log as Log
from finhack.core.loader.base_loader import BaseLoader
class TestmoduleLoader(BaseLoader):
def testaction(self):
Log.logger.debug("----%s----" % (__file__))
Log.logger.debug("this is TestmoduleLoader")
Log.logger.debug("loading "+self.module_name)
Log.logger.debug("testarg1 is:"+str(self.args.testarg1))
Log.logger.debug("testarg2 is:"+str(self.args.testarg2))
obj=self.klass()
obj.args=self.args
obj.run()


Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def testaction(self):
Log.logger.debug("testarg1 is:"+str(self.args.testarg1))
Log.logger.debug("testarg2 is:"+str(self.args.testarg2))
obj=self.klass()
obj.args=self.args
obj.run()


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import finhack.library.log as Log
from runtime.constant import *
import runtime.global_var as global_var

import time
class DefaultTestmodule():
def __init__(self):
pass
def run(self):
Log.logger.debug("----%s----" % (__file__))
Log.logger.debug("this is Testmodule")
Log.logger.debug("vendor is default")
print(self.args)
while True:
time.sleep(1)
Log.logger.debug(".")


def run2(self):
print(self.args)
print('run2')
42 changes: 40 additions & 2 deletions script/package.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
mysqldump -uroot -p --no-data finhack > ./databases/finhack_structure.sql
#!/bin/bash

# 尝试执行mysqldump,但不立即重定向输出
DUMP_OUTPUT=$(mysqldump -uroot -p --no-data finhack 2>&1)

# 检查命令的退出状态
if [ $? -eq 0 ]; then
# mysqldump成功,将输出写入文件
echo "$DUMP_OUTPUT" > ./database/finhack_structure.sql
echo "Database structure dumped successfully."
else
# mysqldump失败,可能是由于密码错误
echo "Failed to dump database structure. Error was:"
echo "$DUMP_OUTPUT"
fi

rm -rf dist/*
rm -rf build/*
Expand All @@ -17,6 +31,30 @@ sed -i '/dingtalk_webhook_webhook=/s/=.*/=/' ./finhack/widgets/templates/empty_p

python setup.py sdist
pip install .
twine upload dist/*


# 设置dist目录的路径
DIST_PATH="./dist"

# 遍历dist目录中的所有文件
for FILE in "$DIST_PATH"/*.tar.gz; do
if [[ "$FILE" =~ ([^/]+)-([0-9]+\.[0-9]+\.[0-9]+(\.dev[0-9]+)?)\.tar\.gz$ ]]; then
PACKAGE_NAME="${BASH_REMATCH[1]}"
PACKAGE_VERSION="${BASH_REMATCH[2]}"

# 使用PyPI API检查包是否已经存在
RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" "https://pypi.org/pypi/${PACKAGE_NAME}/${PACKAGE_VERSION}/json")

if [ "$RESPONSE_CODE" -eq 200 ]; then
echo "Package ${PACKAGE_NAME} version ${PACKAGE_VERSION} already exists on PyPI. Skipping upload for this version."
else
echo "Package ${PACKAGE_NAME} version ${PACKAGE_VERSION} does not exist on PyPI. Uploading..."
twine upload "$FILE"
fi
else
echo "Skipping $FILE, does not appear to be a valid package file."
fi
done


ls /root/anaconda3/envs/finhack/lib/python3.9/site-packages
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
import os

root_dir = 'finhack'
version='0.0.1.dev4'

for subdir, dirs, files in os.walk(root_dir):
if not '__init__.py' in files:
init_file_path = os.path.join(subdir, '__init__.py')
open(init_file_path, 'a').close()
print(f'Created __init__.py in {subdir}')

with open('./finhack/__init__.py', 'w') as file:
file.write(f"__version__ = '{version}'\n")


with open('requirements.txt') as f:
requirements = f.read().splitlines()

setup(
name='finhack',
version='0.0.1.dev3',
version=version,
author='woldy',
description='A scalable quantitative financial analysis framework.',
packages=find_packages(),
Expand Down

0 comments on commit fbca819

Please sign in to comment.