PK ! `& dophon/__init__.py# coding: utf-8
from flask import Blueprint
from dophon.tools.dynamic_import import d_import
name = 'dophon'
def blue_print(name, import_name, inject_config: dict = {}, static_folder=None,
static_url_path=None, template_folder=None,
url_prefix=None, subdomain=None, url_defaults=None,
root_path=None):
"""
获取Flask路由,同时实现自动注入(可细粒度管理)
:param inject_config:注入配置,类型为dict
:param name:同Blueprint.name
:param import_name:同Blueprint.import_name
:param static_folder:同Blueprint.static_floder
:param static_url_path:同Blueprint.static_url_path
:param template_folder:同Blueprint.template_folder
:param url_prefix:同Blueprint.url_prefix
:param subdomain:同Blueprint.subdomain
:param url_defaults:同Blueprint.url_defaults
:param root_path:同Blueprint.root_path
:return:
"""
blue_print_obj = Blueprint(name=name, import_name=import_name, static_folder=static_folder,
static_url_path=static_url_path, template_folder=template_folder,
url_prefix=url_prefix, subdomain=subdomain, url_defaults=url_defaults,
root_path=root_path)
if inject_config:
autowire = __import__('dophon.annotation.AutoWired', fromlist=True)
outerwired = getattr(autowire, 'OuterWired')
outerwired(
obj_obj=inject_config['inj_obj_list'],
g=inject_config['global_obj']
)(inject)()
return blue_print_obj
def inject():
"""
用于构造注入入口,无意义
:return:
"""
pass
def dophon_boot(f):
"""
装饰器形式启动
:return:
"""
def arg(*args, **kwargs):
from . import tools
boot = __import__('dophon.boot', fromlist=True)
kwargs['boot'] = boot
return f(*args, **kwargs)
return arg
__all__ = ['BluePrint', 'blue_print']
BluePrint = blue_print
PK ! Y Y dophon/annotation/__init__.py# coding: utf-8
from dophon.annotation import AutoWired
from dophon.annotation import res, req
from dophon.annotation.AutoWired import *
from dophon.annotation.description import *
"""
注解集合(部分)
author:CallMeE
date:2018-06-01
"""
__all__ = [
'ResponseBody',
'ResponseTemplate',
'AutoParam',
'FullParam',
'RequestMapping',
'GetRoute',
'PostRoute',
'Get',
'Post',
'AutoWired',
'AsResBody',
'AsResTemp',
'AsArgs',
'AsJson',
'AsFile',
'BeanConfig',
'bean',
'Bean',
'Desc',
'DefBean'
]
AutoWired = AutoWired
ResponseBody = AsResBody = res.response_body
ResponseTemplate = AsResTemp = res.response_template
AutoParam = AsArgs = req.auto_param
FullParam = AsJson = req.full_param
FileParam = AsFile = req.file_param
BeanConfig = AutoWired.BeanConfig
bean = AutoWired.bean
Bean = AutoWired.Bean
RequestMapping = req.request_mapping
GetRoute = req.get_route
PostRoute = req.post_route
Get = req.get
Post = req.post
Desc = desc
DefBean = DefBean
PK ! R
,
, dophon/annotation/AutoWired.py# coding: utf-8
# 自动注入修饰器
import re
import inspect
from dophon import properties
from dophon_logger import *
import types
from dophon.tools import sys_utils
logger = get_logger(eval(properties.log_types))
"""
自动注入注解
ps:默认为单例模式,利用模块局内变量域管理实例,减少内存冗余
author:CallMeE
date:2018-06-01
实现自动注入功能需在路由模块(也可以在其他位置)中定义方法(inject_obj)并添加下列两个注解之一
DEMO;
# 注入对象
@AutoWired.InnerWired([ShopGoodsController.ShopGoodsController],a_w_list=['_SGCobj'],g=globals())
或者
@AutoWired.OuterWired(obj_list,g=globals())
def inject_obj():
pass
调用inject_obj()即可实现自动注入
注意:
1.a_w_list中的元素为注入引用名,必须要与注入目标引用名一致,否则注入失效
2.注入位置必须显式定义一个值为None的引用,否则编译不通过
3.注入类型必须为可初始化类型(定义__new__ or __init__)
"""
logger.inject_logger(globals())
obj_manager = {}
class UniqueError(Exception):
"""
唯一错误,存在覆盖已存在的别名实例
"""
# 显式参数注入
def InnerWired(clz, g, a_w_list=[]):
# print(locals())
# 注入实例
def wn(f):
def inner_function(*args, **dic_args):
# 获取数组
if a_w_list and 0 < len(a_w_list):
# 装饰器参数查找赋值
a_name = a_w_list
else:
if dic_args['a_w_list'] and 0 < dic_args['a_w_list']:
# 被装饰函数关键字参数查找赋值
a_name = dic_args['a_w_list']
else:
if not args:
logger.error('动态形参为空')
raise Exception('动态形参为空!!')
# 被装饰函数位置参数查找赋值
a_name = args[0]
for index in range(len(a_name)):
logger.info(str(a_name[index]) + " 注入 " + str(clz[index]))
obj_name = a_name[index]
if obj_name in obj_manager:
g[obj_name] = obj_manager[obj_name]
else:
instance = clz[index]()
obj_manager[obj_name] = instance
g[obj_name] = instance
# return arg
return f()
return inner_function
return wn
# 自定义参数列表注入
def OuterWired(obj_obj, g):
# 前期准备
clz = []
a_w_list = []
for key in obj_obj.keys():
a_w_list.append(key)
clz.append(obj_obj[key])
# 注入实例
def wn(f):
logger.info('开始注入实例')
def inner_function(*args, **dic_args):
# 获取数组
if a_w_list and 0 < len(a_w_list):
# 装饰器参数查找赋值
a_name = a_w_list
else:
if dic_args['a_w_list']:
# 被装饰函数关键字参数查找赋值
a_name = dic_args['a_w_list']
else:
if not args:
logger.error('动态形参为空!!')
raise Exception('动态形参为空!!')
# 被装饰函数位置参数查找赋值
a_name = args[0]
for index in range(len(a_name)):
logger.info(str(a_name[index]) + " 注入 " + str(clz[index]))
try:
obj_name = a_name[index]
if obj_name in obj_manager:
g[obj_name] = obj_manager[obj_name]
else:
instance = clz[index]()
obj_manager[obj_name] = instance
g[obj_name] = instance
except Exception as e:
logger.error('注入' + str(a_name[index]) + '失败,原因:' + str(e))
continue
return f()
return inner_function
return wn
"""
2018-07-22
参照spring实例管理实现实例管理
1.bean装饰器
参照spring中bean注解
需执行被装饰方法才能交由实例管理器管理
默认使用方法名作为实例管理名
可传入自定义别名替换别名
同名别名会抛出二义性错误
暂不支持lambda表达式(强制调用非装饰器装饰lambda表达式会产生注册实例无效)
2.BeanConfig实例管理器
定义管理器子类后定义bean装饰方法后实例化一次后全部交由全局实例管理管理实例
支持with语法
3.Bean实例获取类
实例获取需传入实例别名或实例类型
可直接赋值变量
"""
def bean(name: str = None, value=None):
"""
向实例管理器插入实例
:param by_name: 别名(不传值默认为类型)
:return:
"""
def insert_method(f):
def args(*args, **kwargs):
result = f(*args, **kwargs)
if result is None:
raise TypeError('无法注册实例:' + str(result))
if name:
if name in obj_manager:
raise UniqueError('存在已注册的实例:' + name)
obj_manager[name] = result
else:
alias_name = getattr(f, '__name__') if getattr(f, '__name__') else getattr(type(result), '__name__')
if alias_name in obj_manager:
raise UniqueError('存在已注册的实例')
else:
obj_manager[alias_name] = result
return
return args
def insert_value():
if name:
if name in obj_manager:
raise UniqueError('存在已注册的实例:' + name)
obj_manager[name] = value
else:
alias_name = name
if alias_name in obj_manager:
raise UniqueError('存在已注册的实例')
else:
obj_manager[alias_name] = value
return insert_value if value else insert_method
bean_const = ['file_init', 'sort_bean_config']
class BeanConfig:
"""
实力配置类,类内自定义方法带上bean注解即可(参照springboot中been定义)
"""
def __call__(self, *args, **kwargs):
for name in dir(self):
if re.match('__.+__', name) or name in bean_const:
continue
attr = getattr(self, name)
if callable(attr):
fields = inspect.getfullargspec(attr).args
staticmethod(attr(*fields))
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type and exc_val and exc_tb:
logger.error(f'{str(exc_val)} 实例不存在')
pass
def __init__(self, files: list = [], config_file: str = ''):
if obj_manager:
logger.warning(f'存在已初始化实例管理器,跳过初始化: {self}')
return
logger.info('执行批量实例管理初始化')
if files or config_file:
self.file_init(files if files else [config_file])
else:
# 无参构造,加载内部bean装饰方法,实例统一配置
self()
def file_init(self, files: list = []):
for config_file in files:
if isinstance(config_file, str):
# 字符串形式声明配置文件(文件名)
logger.info('读取配置文件: %s ' % (config_file,))
# 有参构造,分布式实例配置
from dophon import properties
import os
file_path = properties.project_root + (config_file if config_file.startswith(os.sep) else (
os.sep + config_file))
if os.path.exists(file_path):
try:
bean_config = __import__(config_file.replace(os.sep, '.').rstrip('.py'), fromlist=True)
self.sort_bean_config(bean_config)
except Exception as e:
logger.error('实例配置读取失败,信息: %s' % (e,))
else:
logger.error('不存在实例配置文件')
self()
elif isinstance(config_file, types.ModuleType):
# 模块形式配置文件
try:
logger.info('读取配置文件: %s ' % (config_file,))
self.sort_bean_config(config_file)
except Exception as e:
logger.error('实例配置读取失败,信息: %s' % (e,))
else:
# 其他形式声明配置
logger.error('配置文件声明无效(%s)' % (config_file,))
continue
def sort_bean_config(self, bean_config: object):
for k in dir(bean_config):
if not k.startswith('__') and not k.endswith('__'):
# 获取自定义参数
# print(k, '---', getattr(bean_config, k))
bean(k, getattr(bean_config, k))()
# print(obj_manager[k])
class Bean:
def __new__(cls, *args, **kwargs):
if args or len(args) > 1:
bean_key = args[0]
elif kwargs or len(kwargs) > 1:
bean_key = kwargs.keys()[0]
else:
raise KeyError('不存在实例别名或实例类型')
if isinstance(bean_key, str):
if bean_key in obj_manager:
return obj_manager[bean_key]
raise KeyError(f'不存在该别名实例:({bean_key})')
elif isinstance(bean_key, type):
type_list = []
for key in obj_manager.keys():
if re.match('__.+__', key):
continue
bean_obj = obj_manager[key]
if isinstance(bean_obj, bean_key):
if len(type_list) > 0:
raise UniqueError(f'存在定义模糊的实例获取类型:({bean_key})')
type_list.append(bean_obj)
if not type_list:
raise KeyError(f'不存在该类型实例:({bean_key})')
return type_list[0]
else:
logger.error('无法获取的实例: %s' % (bean_key,))
def DefBean(clz):
# print(f'define bean class{clz}')
if type(clz) is type:
__instance = clz()
# for name in dir(clz):
# print(f'{name} => {getattr(clz, name)}')
# print(f'''{clz} => {}''')
alias_fstr = f"_{getattr(clz, '__name__')}__bean_alias"
clz_alias = sys_utils.to_lower_camel(getattr(clz, alias_fstr)) \
if hasattr(clz, alias_fstr) \
else getattr(clz, '__name__')
if clz_alias not in obj_manager:
obj_manager[sys_utils.to_lower_camel(clz_alias)] = __instance
# print(obj_manager)
PK ! e1 1 ) dophon/annotation/description/__init__.pyimport re
from functools import wraps
from ..req import auto_param
from inspect import getfullargspec
DESC_INFO = {}
PATH_TRANSLATE_DICT = {}
def desc(
operation: str = 'None'
):
"""
函数描述装饰器
自动添加请求属性配对
:return:
"""
def inner_method(f):
# 获取方法的参数名集合
# FullArgSpec(args=['test_arg1'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
full_arg_spec = getfullargspec(f)
__own_doc = getattr(f, '__doc__') # 自身定义的文档
__args = full_arg_spec.args # 通过函数定义的属性名集合
__defaults = full_arg_spec.defaults # 通过函数定义参数默认值
# print(__defaults)
__annotations = full_arg_spec.annotations # 通过函数定义的参数修饰
# print(full_arg_spec)
# 处理参数信息,放入参数信息列
__function_arg_info = {}
__func_info = {
'own_doc': re.sub('(\n)', ' ', __own_doc) if __own_doc else operation
}
for __inner_arg_index in range(len(__args)):
__function_arg_info[__args[__inner_arg_index]] = {
'name': __args[__inner_arg_index],
# 获取参数上的默认值
'default_value': __defaults[__inner_arg_index]
if __defaults and __inner_arg_index < len(__defaults)
else 'nothing',
# 获取参数上的类型修饰
'annotation_type': __annotations[__args[__inner_arg_index]].__name__
if __annotations and __args[__inner_arg_index] in __annotations
else 'any',
}
__func_info['args_info'] = __function_arg_info
DESC_INFO[f'{getattr(f, "__module__")}.{getattr(f, "__name__")}'] = __func_info
@wraps(f)
def inner_args(*args, **kwargs):
return auto_param()(f)(*args, **kwargs)
return inner_args
return inner_method
PK ! #ئ,r r ! dophon/annotation/req/__init__.py# coding: utf-8
import functools
import re
from flask import request, abort
from dophon import properties
from dophon_logger import *
logger = get_logger(eval(properties.log_types))
'''
参数体可以为多个,形参名称必须与请求参数名一一对应(只少不多)
装饰器中关键字参数列表可以指定参数名
'''
logger.inject_logger(globals())
# 处理请求参数装饰器(分离参数)
def auto_param(kwarg_list=[]):
def method(f):
@functools.wraps(f)
def args(*args, **kwargs):
try:
if 'GET' == str(request.method):
return is_get(f, request, kwarg_list)
elif 'POST' == str(request.method):
return is_post(f, request, kwarg_list)
else:
logger.error('方法不支持!!')
except TypeError as t_e:
logger.error('参数不匹配!!,msg:' + repr(t_e))
raise t_e
return abort(500)
return args
return method
'''
注意!!!
该注解只需方法体内存在一个形参!!!
同样,需要指定参数名的形式参数列表条目也只能存在一个,多个会默认取第一个
不匹配会打印异常
参数以json形式赋值
'''
# 处理请求参数装饰器(统一参数,参数体内参数指向json串)
def full_param(kwarg_list=[]):
def method(f):
@functools.wraps(f)
def args(*args, **kwargs):
try:
if 'POST' == str(request.method):
r_arg = ()
r_kwarg = {}
if not kwarg_list:
r_arg = (request.json if request.is_json else request.form.to_dict(),)
else:
r_kwarg[kwarg_list[0]] = request.json if request.is_json else request.form.to_dict()
return f(*r_arg, **r_kwarg)
elif 'GET' == str(request.method):
r_arg = ()
r_kwarg = {}
if not kwarg_list:
r_arg = (request.args.to_dict(),)
else:
r_kwarg[kwarg_list[0]] = request.args.to_dict()
return f(*r_arg, **r_kwarg)
else:
logger.error('json统一参数不支持该请求方法!!')
return abort(400)
except TypeError as t_e:
logger.error('参数不匹配!!,msg:' + repr(t_e))
return abort(500)
return args
return method
def file_param(alias_name: str = 'files', extra_param: str = 'args'):
"""
文件参数装在装饰器
ps 文件上传暂时只支持路由方法内单个参数接收(会有校验策略)
参数demo(小程序):
ImmutableMultiDict([('img_upload_test', )])
:return:
"""
def method(f):
@functools.wraps(f)
def args(*args, **kwargs):
# 检测参数
a_nums = len(args) + len(kwargs)
if a_nums > 0:
logger.error('路由绑定参数数量异常')
raise Exception('路由绑定参数数量异常')
try:
extra_param_value = (request.form if request.form else request.json).to_dict()
except:
extra_param_value = {}
k_args = {
alias_name: request.files.to_dict(),
extra_param: extra_param_value
}
return f(**k_args)
return args
return method
def is_get(f, r, kw_list):
r_arg = ()
r_kwarg = {}
if not kw_list:
r_arg = r.args.to_dict().values()
else:
for index in range(len(kw_list)):
r_kwarg[kw_list[index]] = r.args[kw_list[index]]
return f(*r_arg, **r_kwarg)
def is_post(f, r, kw_list):
r_arg = ()
r_kwarg = {}
if r.is_json:
if not kw_list:
r_arg = is_json(r.json)
else:
for index in range(len(kw_list)):
r_kwarg[kw_list[index]] = r.json[kw_list[index]]
else:
if not kw_list:
r_arg = is_form(r.form)
else:
for index in range(len(kw_list)):
r_kwarg[kw_list[index]] = r.form[kw_list[index]]
return f(*r_arg, **r_kwarg)
def is_json(arg_list):
return arg_list.values()
def is_form(arg_list):
return arg_list.to_dict().values()
# 路径绑定装饰器
# 默认服务器从boot获取
def request_mapping(path='', methods=[], app=None):
def method(f):
try:
# 自动获取蓝图实例并进行http协议绑定
current_package = __import__(str(getattr(f, "__module__")), fromlist=True)
try:
package_app = getattr(current_package, '__app') \
if hasattr(current_package, '__app') \
else current_package.app \
if hasattr(current_package, 'app') \
else app \
if hasattr(app, 'routes') \
else __import__('dophon').blue_print(f"_annotation_auto_reg.{getattr(current_package, '__name__')}",
getattr(current_package, '__name__'))
except Exception as e:
logger.warn(f'{e}')
package_app = __import__('dophon.boot', fromlist=True).app
# 回设内部蓝图参数
# print(package_app)
setattr(current_package, '__app', package_app)
result = package_app.route(path, methods=methods)(f)
except Exception as e:
logger.error(f'{getattr(f, "__module__")}参数配置缺失,请检查({path},{methods},{package_app})')
def m_args(*args, **kwargs):
if properties.debug_trace:
logger.info(f'router endpoint:{getattr(f, "__module__")}.{f},router path:{path}')
return result(*args, **kwargs)
return m_args
return method
# 请求方法缩写
def method_route(req_method: str, path: str = '', ):
def method(f):
result = request_mapping(path, [req_method])(f)
def _args(*args, **kwargs):
return result(*args, **kwargs)
return _args
return method
# get方法缩写
def get_route(path=''):
def method(f):
result = method_route('get', path)(f)
def _args(*args, **kwargs):
return result(*args, **kwargs)
return _args
return method
# post方法缩写
def post_route(path=''):
def method(f):
result = method_route('post', path)(f)
def _args(*args, **kwargs):
return result(*args, **kwargs)
return _args
return method
func_to_path = lambda f: f"""{"/" if re.match("^[a-zA-Z0-9]+", getattr(f, "__name__")) else ""}{re.sub("[^a-zA-Z0-9]",
"/",
getattr(f,
"__name__"))}"""
# get方法缩写
def get(f, *args, **kwargs):
path = func_to_path(f)
result = get_route(re.sub('\s+', '', path))(f)
def method():
def _args():
return result(*args, **kwargs)
return _args
return method
# post方法缩写
def post(f, *args, **kwargs):
path = func_to_path(f)
result = post_route(re.sub('\s+', '', path))(f)
def method():
def _args():
return result(*args, **kwargs)
return _args
return method
PK ! ! dophon/annotation/res/__init__.py# 响应返回数据修饰器(跳过视图解析器)
# 响应体形式返回
# (自带AutoParam) <------暂未完成
import functools
from dophon import properties
from dophon_logger import *
from flask import jsonify, render_template
logger = get_logger(eval(properties.log_types))
logger.inject_logger(globals())
def response_body():
def method(f):
@functools.wraps(f)
def arg(*args, **kwargs):
result = jsonify(f(*args, **kwargs))
if result:
return result
else:
return []
return arg
return method
# 返回web模板
def response_template(template: list):
"""
返回模板页面
:param template: 模板页面路径
:return:
"""
def method(f):
@functools.wraps(f)
def args(*args, **kwargs):
page_param = f(*args, **kwargs)
if isinstance(page_param, type({})):
return render_template(template, **page_param)
else:
logger.error('页面参数错误!')
raise KeyError('页面参数错误!')
return args
return methodPK ! :͓ dophon/annotation/side_pkg.py# from dophon_properties import *
#
# get_properties(DOPHON)
from dophon import properties
from dophon_logger import *
"""
框架其余功能装饰器
"""
logger = get_logger(eval(properties.log_types))
logger.inject_logger(globals())
def logger_execute(*args, **kwargs):
logger.info(f'{args} : {kwargs}')
def log_router(func=logger_execute):
# 执行路由日志记录
func()
def r_method(f):
def r_args(*args, **kwargs):
f(*args, **kwargs)
return r_args
return r_method
def logs():
print('logs!!!')
@log_router()
def test():
print('ooo')
test()
PK ! Wuv uv dophon/boot.py# coding: utf-8
import traceback
import functools
import os
import sys
import re
from datetime import datetime
from threading import *
import json
from dophon import pre_boot
pre_boot.check_modules()
from flask import Flask, request, abort, jsonify
from dophon import properties, blue_print
from dophon_logger import *
logger = get_logger(eval(properties.log_types))
logger.inject_logger(globals())
from html import escape
from dophon.tools import gc
from . import tools
from .tools.dynamic_import import d_import
# 旧版本与数据库的耦合,后期版本移除
try:
mysql = __import__('dophon.db.mysql')
except:
mysql = None
def load_banner():
"""
加载header-banner文件
:return:
"""
file_root = properties.project_root
file_path = file_root + os.path.sep + 'header.txt'
if os.path.exists(file_path):
with open(file_path, encoding='utf8') as banner:
for line in banner.readlines():
sys.stdout.write(line)
else:
tools.show_banner()
sys.stdout.flush()
def load_footer():
"""
加载footer-banner文件
:return:
"""
file_root = properties.project_root
file_path = file_root + os.path.sep + 'footer.txt'
if os.path.exists(file_path):
with open(file_path, encoding='utf8') as banner:
for line in banner.readlines():
sys.stdout.write(line)
sys.stdout.flush()
error_info = properties.error_info
def boot_init():
"""
初始化启动
:return:
"""
global app_name, app, ip_count, ipcount_lock, ip_refuse_list
load_banner()
app_name = properties.service_name if hasattr(properties, 'service_name') else __name__
# 定义WEB容器(同时防止json以ascii解码返回)
app = Flask(app_name)
app.config['JSON_AS_ASCII'] = False
# ip计数缓存
ip_count = {}
ipcount_lock = Lock()
if os.path.exists(os.getcwd() + '/ip_count'):
with open('ip_count', 'r') as ip_count_file:
file_text = ip_count_file.read()
if file_text:
ip_count = eval(file_text)
else:
with open('ip_count', 'w') as ip_count_file:
json.dump({}, ip_count_file)
# IP黑名单
ip_refuse_list = {}
boot_init()
def before_request():
"""
定义拦截器
:return:
"""
if properties.ip_count:
ipcount_lock.locked()
ip = request.remote_addr
now_timestemp = datetime.now().timestamp()
# 检测是否为黑名单
if ip in ip_refuse_list:
# 默认禁用一分钟
if (int(now_timestemp) - int(ip_refuse_list[ip])) > 60:
# 可以清除白名单
ip_refuse_list.pop(ip)
# 清除访问记录
ip_count[ip]['req_timestemp'] = [now_timestemp]
else:
# 未到解禁时间
return abort(400)
if ip in ip_count:
ip_item = ip_count[ip]
ip_item['req_count'] += 1
req_timestemp = ip_item['req_timestemp']
"""
判断逻辑:
当前请求时间是最近的,连续的1秒内
"""
if (int(now_timestemp) - int(req_timestemp[0])) > 1 \
and \
(int(now_timestemp) - int(req_timestemp[len(req_timestemp) - 1])) < 1:
# 检测3秒内请求数
if len(req_timestemp) > 50:
# 默认三秒内请求不超过300
# 超出策略则添加到黑名单
ip_refuse_list[ip] = now_timestemp
else:
# 不超出策略则清空请求时间戳缓存
ip_item['req_timestemp'] = [now_timestemp]
else:
ip_item['req_timestemp'].append(now_timestemp)
else:
ip_item = {
'req_count': 1,
'req_timestemp': [now_timestemp]
}
Thread(target=persist_ip_count).start()
ip_count[ip] = ip_item
def persist_ip_count():
"""
持久化ip统计到文件
:return:
"""
# 持久化ip统计
with open('ip_count', 'w') as ip_count_file:
json.dump(ip_count, ip_count_file)
if ipcount_lock.locked():
ipcount_lock.release()
blueprint_init_queue = {} # 蓝图初始化方法缓存(用于初始化后启动)
# 处理各模块中的自动注入以及组装各路由
# dir_path中为路由模块路径,例如需要引入的路由都在routes文件夹中,则传入参数'/routes'
def map_apps(dir_path):
path = os.getcwd() + dir_path
if not os.path.exists(path):
logger.error('路由文件夹不存在,创建路由文件夹')
os.mkdir(path)
f_list = os.listdir(path)
logger.info(f'路由文件夹: {dir_path}')
while f_list:
try:
file = f_list.pop(0)
if re.match('__.*__', file):
continue
i = os.path.join(path, file)
if os.path.isdir(i):
logger.info(f'加载路由模块: {file}')
# 扫描内部文件夹
map_apps(f'{dir_path}{os.sep}{file}')
continue
file_name = re.sub('\.py', '', file)
module_path = re.sub('^\.', '', re.sub('\\\\|/', '.', dir_path))
# print(module_path)
f_model = __import__(f'{module_path}.{file_name}', fromlist=True)
# 自动装配蓝图实例并自动配置部分参数,免除繁琐配置以及精简代码
package_app = getattr(f_model, '__app') \
if hasattr(f_model, '__app') \
else f_model.app \
if hasattr(f_model, 'app') \
else blue_print(f"_boot_auto_reg.{file_name}", getattr(f_model, '__name__'))
filter_method = package_app.before_request(before_request)
# 若需统计请求,装配请求统计方法
if hasattr(properties, 'ip_count') and getattr(properties, 'ip_count'):
setattr(f_model, 'before_request', filter_method)
# 若存在初始化执行方法,执行该方法
if hasattr(f_model, 'blueprint_init'):
init_fun = getattr(f_model, 'blueprint_init')
# 判断是否方法
if callable(init_fun):
blueprint_init_queue[f_model] = before_bp_init_fun(init_fun)
get_app().register_blueprint(package_app)
except Exception as e:
raise e
pass
# for name in dir(item):
# print(f'{name} ==> {getattr(item, name)}')
# print(get_app().blueprints)
def BeanScan(scan_path: list = []):
def method(f):
def method_args(*args, **kwargs):
logger.debug('mapping beans')
for bean_path in scan_path if scan_path else properties.components_path:
is_project_root = os.path.abspath(properties.project_root + bean_path) == os.path.abspath(
properties.project_root)
if is_project_root:
# 若配置项目自身路径则提示警告
logger.warning(f'扫描路径(components_path)存在项目根路径的配置会导致项目异常启动,请注意')
map_bean(bean_path)
return f(*args, **kwargs)
return method_args
return method
def map_bean(bean_path: str, is_project_root: bool = False):
__project_root = properties.project_root.replace("\\", "/")
bean_dir = re.sub('(\\\\|/)', '', bean_path)
# 不存在路由定义
# print(f'bean_path: {bean_path}')
try:
for root, dirs, files in os.walk(f'{__project_root}{bean_path}'):
relative_path = re.sub(f'{__project_root}', '', root)
# 若处于路由定义则跳过
# 相对路径为根路径也跳过
# 强制指定为项目根路径也跳过
if relative_path in properties.blueprint_path \
or relative_path == '/' \
or relative_path.endswith('__') \
or is_project_root:
continue
logger.debug(f'scan path {relative_path}')
# exec(f'from {re.sub("/", ".", __bean_dir)} import {module_path.split(".")[-1]}')
# print(f'{relative_path} => {dirs} => {files}')
for file in files:
file_short_name = os.path.basename(file).split(".")[0]
if not file.endswith('.py') or re.match('__.+__', file_short_name):
# 不是python脚本文件
continue
# 获取内部文件路径
__file_path = f'{relative_path}/{file_short_name}'
# 整理成包形式
__file_package = re.sub('(\\\\|/)', '.', __file_path)
# print(__file_package)
__file_package_part = __file_package.split('.')[1:]
# print(__file_package_part)
__exec_code = f'from {".".join(__file_package_part[:-1])} import {__file_package_part[-1]}'
# print(__exec_code)
logger.debug(f'mapping bean {__file_package_part[-1]} {__exec_code}')
exec(__exec_code)
__import__(bean_dir, fromlist=bean_dir)
except Exception as e:
pass
def before_bp_init_fun(f):
"""
预留蓝图初始化装饰器
:param f:
:return:
"""
# print(f)
def fields(*args, **kwargs):
# print('args: ', args, 'kwargs:', kwargs)
f(*args, **kwargs)
return fields
def get_app() -> Flask:
return app
def free_source():
def method(f):
@functools.wraps(f)
def args(*arg, **kwarg):
try:
logger.info('启动服务器')
logger.info('路由初始化')
logger.debug('mapping routes')
for path in properties.blueprint_path:
map_apps(path)
load_footer()
# 执行蓝图初始化方法
for blueprint_module, blue_print_init_method in blueprint_init_queue.items():
try:
blue_print_init_method()
except Exception as e:
logger.error(f'蓝图"{blueprint_module}"初始化失败,信息: {e}')
# for rule in get_app().url_map.iter_rules():
# print(str(rule))
# print(rule.get_rules())
# for name in dir(rule):
# print(f'{name}---{getattr(rule,name)}')
# print(help(rule))
# break
f(*arg, **kwarg)
"""
释放所有资源
:return:
"""
logger.info('服务器关闭')
logger.debug('释放资源')
if mysql:
mysql.free_pool()
logger.debug('释放连接池')
sys.exit()
# logger.info('再次按下Ctrl+C退出')
except Exception as e:
logger.error(e)
return args
return method
def fix_static(
fix_target: Flask = app,
static_floder: str = 'static',
enhance_power: bool = False
):
"""
修正静态文件路径
:return:
"""
if hasattr(fix_target, 'static_folder'):
if hasattr(properties, 'project_root'):
root_path = properties.project_root
else:
root_path = os.getcwd()
static_floder_path = root_path + '/' + static_floder
fix_target.static_folder = static_floder_path
if os.path.exists(static_floder_path) and enhance_power:
enhance_static_route(static_floder_path)
else:
raise Exception('错误的修复对象')
def enhance_static_route(static_floder_path: str):
"""
增强静态文件路由能力
:param static_floder_path:
:param serlize:
:return:
"""
framework_static_route_path = f'{properties.project_root}{properties.blueprint_path[0]}/FrameworkStaticRoute.py'
# if not os.path.exists(framework_static_route_path):
linux_static_floder_path = static_floder_path.replace('\\', '/')
if True:
# import types
import uuid
# 定义静态资源获取路径
logger.info('增强静态文件路由')
with open(framework_static_route_path, 'wb') as fsroute:
# 写入预设信息
fsroute.write(bytes(
f"# -*- coding: utf-8 -*-\n"
f"from dophon import blue_print\n"
f"from dophon.annotation import *\n"
f"from flask import url_for\n"
f"from flask import render_template\n"
f"from flask import send_from_directory\n"
f"app = blue_print('FrameworkStaticRoute', __name__,static_folder='{linux_static_floder_path}')\n",
encoding='utf-8'))
for root, dir_path, file in os.walk(linux_static_floder_path):
# 消除转义字符(win)
root = re.sub('\\\\', '/', root)
linux_static_floder_path = re.sub('\\\\', '/', linux_static_floder_path)
# 静态目录下的目录名
root_dir_path = re.sub('\\\\', '/', re.sub(linux_static_floder_path, "", root))
# 解析静态资源路径
route_name = re.sub('[^(1-9a-zA-Z_)]', '',
f'{re.sub(linux_static_floder_path, "", root)}_{root_dir_path}_{uuid.uuid1()}')
route_path = f'{root_dir_path}/'
static_url_method_code_body = \
f"@RequestMapping('{route_path}',['get','post'])\ndef {route_name}(file_name):\n\t" \
f"\"\"\"\n\t" \
f"static file route in {route_path}\n\t" \
f":param file_name: static file name\n\t" \
f":return static file blob\n\t" \
f"\"\"\"\n\t" \
f"return render_template(f'{root_dir_path}/" \
"{file_name}" \
f"') if file_name.endswith('.html') " \
f"else send_from_directory('{linux_static_floder_path}{root_dir_path}',f" \
"'{file_name}'" \
",as_attachment=True)\n"
fsroute.write(bytes(
static_url_method_code_body,
encoding='utf-8'))
logger.info("增强静态文件夹完毕")
def fix_template(
fix_target: Flask = app,
template_folder: str = 'templates'
):
"""
修正页面模板文件路径
:return:
"""
if hasattr(fix_target, 'template_folder'):
if hasattr(properties, 'project_root'):
root_path = properties.project_root
else:
root_path = os.getcwd()
fix_target.template_folder = root_path + '/' + template_folder
else:
raise Exception('错误的修复对象')
@free_source()
def run_app(host=properties.host, port=properties.port):
logger.info(f'监听地址: {host} : {port}')
if properties.server_threaded:
# 开启多线程处理
app.run(host=host, port=port, threaded=properties.server_threaded)
elif tools.is_not_windows() and properties.server_processes > 1:
# 开启多进程处理
print('开启多进程', properties.server_processes)
app.run(host=host, port=port, threaded=False, processes=properties.server_processes)
else:
app.run(host=host, port=port)
@free_source()
def tornado(host=properties.host, port=properties.port):
logger.info(f'添加Tornado核心')
d_import('tornado')
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
logger.info(f'开始使用Tornado增强')
http_server = HTTPServer(WSGIContainer(get_app()))
logger.info(f'监听{port}')
if tools.is_not_windows() and properties.server_processes > 1:
http_server.bind(port, host) # flask默认的端口
http_server.start(properties.server_processes)
else:
http_server.listen(port, host) # flask默认的端口
logger.info(f'监听地址: {host}:{port}')
IOLoop.current().start()
@free_source()
def run_app_ssl(host=properties.host, port=properties.port, ssl_context=properties.ssl_context):
logger.info(f'监听地址: {host} : {port}')
if properties.server_threaded:
# 开启多线程处理
app.run(host=host, port=port, ssl_context=ssl_context, threaded=properties.server_threaded)
elif tools.is_not_windows() and properties.server_processes > 1:
# 开启多进程处理
app.run(host=host, port=port, ssl_context=ssl_context, threaded=False,
processes=properties.server_processes)
else:
app.run(host=host, port=port, ssl_context=ssl_context)
@free_source()
def tornado_ssl(host=properties.host, port=properties.port, ssl_context=properties.ssl_context):
logger.info(f'添加Tornado核心')
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
logger.info(f'开始使用Tornado增强')
http_server = HTTPServer(WSGIContainer(get_app()), ssl_options={
'certfile': ssl_context[0],
'keyfile': ssl_context[1],
})
logger.info(f'监听{port}')
if tools.is_not_windows() and properties.server_processes > 1:
http_server.bind(port) # flask默认的端口
http_server.start(properties.server_processes)
else:
http_server.listen(port) # flask默认的端口
logger.info(f'监听地址: {host}:{port}')
IOLoop.current().start()
def run_f(ssl: bool = False, **kwargs):
"""
flask 启动入口
:param ssl:
:param kwargs:
:return:
"""
run_app_ssl(**kwargs) if ssl else run_app(**kwargs)
def run_t(ssl: bool = False, **kwargs):
"""
tornado 启动入口
:param ssl:
:param kwargs:
:return:
"""
tornado_ssl(**kwargs) if ssl else tornado(**kwargs)
FLASK = 0
TORNADO = 1
def run(run_type: int = FLASK, ssl: bool = False, **kwargs):
"""
统一入口
:param run_type: 启动类型
:param ssl: https开关
:param kwargs: 启动参数
:return:
"""
run_f(ssl, **kwargs) if run_type == FLASK else run_t(ssl, **kwargs) if run_type == TORNADO else 1 / 0
def bootstrap_app():
"""
bootstrap样式页面初始化
:return:
"""
global app
b = __import__('flask_bootstrap')
b.Bootstrap(app)
from dophon.annotation import *
# region 定义内部接口
@GetRoute('/find/function/desc/by/module/')
@ResponseBody()
def find_function_desc_by_module(module_path: str):
print(module_path)
@RequestMapping('/framework/ip/count', ['get', 'post'])
@ResponseBody()
def view_ip_count():
"""
此为公共请求,不计入ip统计
:return:
"""
return ip_count
@RequestMapping('/framework/ip/refuse', ['get', 'post'])
@ResponseBody()
def view_ip_refuse():
"""
此为公共请求,不计入ip统计
:return:
"""
return ip_refuse_list
GC_INFO = properties.debug_trace if properties.debug_trace else False # gc信息开关
@RequestMapping('/framework/gc/show', ['get'])
@ResponseBody()
def show_gc_info():
# return gc.show_gc_leak(print)
return gc.show_gc_leak(logger.info) if GC_INFO else {}
@GetRoute('/rule/')
def show_rule_info(view):
__bind_rule_info_map = {}
for item in get_app().url_map.iter_rules():
# print(next(get_app().url_map.iter_rules(item.endpoint)))
# print(re.sub('_annotation_auto_reg\.', '', item.endpoint))
__bind_rule_info_map[str(next(get_app().url_map.iter_rules(item.endpoint)))] = \
re.sub('_annotation_auto_reg\.', '', item.endpoint)
# print([ v for v in sorted(__bind_rule_info_map.values())] )
from .annotation.description import DESC_INFO
# print(DESC_INFO)
try:
def __sort_inner_desc_info(item_key: str):
def parse_param(info: dict):
__result = ''
for __param_name, __param_info in info.items():
__result += f'''
{__param_name}
'''
__result += '
'
for __name, __value in __param_info.items():
__result += f'''
{__name}
{escape(str(__value))}
'''
__result += '
'
return __result
result = ''
if __bind_rule_info_map[item_key] in DESC_INFO:
__desc_info = DESC_INFO[__bind_rule_info_map[item_key]]
else:
return result
for __arg_name, __arg_info in __desc_info.items():
result += f"""
{'Doc' if __arg_name == 'own_doc' else 'Params'}
{ __arg_info if __arg_name == 'own_doc' else parse_param(__arg_info) }
"""
return result
# 注册路径列表入口
# {'
'.join(
# [
# str(item)
# for item in get_app().url_map.iter_rules()
#
# ]
# )}
body_str = ''
for item in get_app().url_map.iter_rules():
route_method_path = __bind_rule_info_map[str(item)]
if re.search('\.', route_method_path) and not re.match('FrameworkStaticRoute\..*', route_method_path):
# 非框架定义的路由参与路由显示
body_str += f"""
please contact coder or direct to '
'dophon and leave your question
' + \
'request form:' + request.form + \
'request body:' + request.json if request.is_json else ''), 400
elif error_info == error_info_type.JSON:
return self_result.JsonResult(400, request.json if request.is_json else '',
'please check your request data').as_res()
elif error_info == error_info_type.XML:
return self_result.XmlResult(400, request.json if request.is_json else '',
'please check your request data').as_res()
# endregion
PK ! Ϻ dophon/cluster_boot.py# coding: utf-8
from multiprocessing import Process, freeze_support
import time, socket, random
from flask import request, make_response
from urllib3 import PoolManager
from dophon import properties
from dophon_logger import *
logger = get_logger(eval(properties.log_types))
logger.inject_logger(globals())
ports = [] # 记录监听端口
proxy_clusters = {}
pool = PoolManager()
def main_freeze():
freeze_support()
def redirect_request():
logger.info('touch path: %s [success]' % (request.path))
res = pool.request(request.method, '127.0.0.1:' + str(random.choice(ports)) + request.path,
fields=request.json if request.is_json else request.form)
return make_response(res.data)
def outer_entity(boot):
# 重写路由信息(修改为重定向路径)
boot.get_app().before_request(redirect_request)
boot.run_app()
def run_clusters(clusters: int, outer_port: bool = False, start_port: int = 8800):
"""
运行集群式服务器
:param clusters: 集群个数
:param outer_port: 是否开启外部端口映射(映射端口为用户配置文件中配置的端口)
:param start_port: 集群起始监听端口
:return:
"""
from dophon import boot
for i in range(clusters):
current_port = start_port + i
create_cluster_cell(boot=boot, port=current_port)
ports.append(current_port)
while len(ports) != clusters:
time.sleep(5)
logger.info('启动检测端口监听')
for port in ports:
if check_socket(int(port)):
continue
logger.info('集群端口: %s ' % ports)
if outer_port:
logger.info('启动外部端口监听[%s]' % (properties.port))
outer_entity(boot)
def create_cluster_cell(boot, port):
# http协议
proc = Process(target=boot.run_app, kwargs={
'host': '127.0.0.1',
'port': port
})
proc.start()
def check_socket(port: int):
s = socket.socket()
flag = True
while flag:
try:
ex_code = s.connect_ex(('127.0.0.1', port))
flag = False
return int(ex_code) == 0
except Exception as e:
time.sleep(3)
continue
PK ! <-I3 I3 dophon/docker_boot.py# encoding: utf-8
from dophon.tools import is_windows
from dophon import boot
import logging
import re
import os
import socket
import sys
import threading
import time
from urllib import request
def read_self_prop():
try:
def_prop = __import__('dophon.def_prop.default_properties', fromlist=True)
u_prop = __import__('application', fromlist=True)
# 对比配置文件
for name in dir(def_prop):
if re.match('__.*__', name):
continue
if name in dir(u_prop):
continue
setattr(u_prop, name, getattr(def_prop, name))
sys.modules['properties'] = u_prop
sys.modules['dophon.properties'] = u_prop
except Exception as e:
logging.error(e)
sys.modules['properties'] = def_prop
sys.modules['dophon.properties'] = def_prop
try:
read_self_prop()
except Exception as e:
logging.error('没有找到自定义配置:(application.py)')
logging.error('引用默认配置')
from dophon import properties
from dophon_logger import *
logger = get_logger(eval(properties.log_types))
logger.inject_logger(globals())
def IsOpen(ip, port):
"""
检查端口是否被占用
:param ip:
:param port:
:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
logger.error('端口被占用:' + port)
s.close()
return True
except:
return False
def listen(code):
"""
监听命令状态码
:param code: os.system()命令返回码
:return: 当不正常执行会抛出错误,谨慎使用!!!
"""
if code >> 8 is 0:
pass
else:
raise Exception('命令执行错误!')
def listen_container_status(container_port, loop_count: int = 3, wait_sec: int = 10):
"""
检测容器端口存活
:return:
"""
# 一分钟后检测
time.sleep(60)
# 默认检测三次
curr_count = 1
while int(curr_count) <= int(loop_count):
# 默认间隔10秒
time.sleep(wait_sec)
if IsOpen(get_docker_address(), int(container_port)):
raise Exception('端口映射异常')
else:
# 报错证明端口正常占用
# 发起请求
url = f'http://{get_docker_address()}:{container_port}/rule/json'
logger.info('容器存活性检查:' + url)
res = request.urlopen(url)
if not res.read():
raise Exception('服务启动异常')
curr_count += 1
logger.info('容器启动成功,请在命令行输入docker ps查看')
def get_docker_address():
"""
获取容器载体ip
:return:
"""
result = os.popen('ipconfig' if is_windows() else 'ifconfig').readlines()
result_lines = []
r_l_copy = []
while result:
line = result[0]
if re.search('^.*(d|D)(o|O)(c|C)(k|K)(e|E)(r|R).*$', line):
result_lines = result.copy()
break
else:
result.pop(0)
for line in result_lines:
line = re.sub('\s*', '', line)
if line and re.search('([0-9]+\.)+[0-9]+$', line) and re.search('(i|I)(p|P)', line):
r_l_copy.append(
re.search('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', line).group(0)
)
return r_l_copy.pop(0) if r_l_copy else 'can not get container ip'
def attach_container(base_name: str):
"""
进入容器
:return:
"""
os.system('docker attach ' + base_name)
def has_version_expr(info: str):
return re.search('(>=|==|=>|<=|=<)', info)
def run_as_docker(
entity_file_name: str = None,
container_port: str = str(properties.port),
docker_port: str = str(properties.port),
attach_cmd: bool = False,
alive_test: bool = False,
save_image: bool = False,
extra_package: dict = {},
cache_virtual_env_dir: str = '',
package_repository: str = '',
package_cache_path: str = '',
timezone: str = 'Asia/Shanghai',
img_author: str = 'local_user',
img_message: str = 'a new image',
):
"""
利用docker启动项目
:param save_image: 是否保存镜像(选择后不启动,直接导出镜像)
:param alive_test: 容器存活检测(默认不检测)
:param entity_file_name: 入口文件名(包括后缀)
:param container_port: 容器暴露端口
:param docker_port: 容器内部端口 -> 集群模式下的暴露端口,一般为配置文件定义的端口
:param attach_cmd: 是否进入容器内部sh
:param extra_package: 额外需要加载的包以及版本
:param cache_virtual_env_dir: 指定的虚拟器路径,在启动文件同级目录或下级目录
:param package_repository: 自带缓存包路径,若为空会自动执行pip安装
# 阿里云仓库 => https://mirrors.aliyun.com/pypi/simple/
:param package_cache_path: 包缓存路径
:param timezone: 时区代号
:return:
"""
# 依赖包的缓存
package_cache = []
try:
logger.info('容器前期准备')
root = re.sub('\\\\', '/', properties.project_root)
base_name = os.path.basename(root)
import platform
p_version = platform.python_version()
work_dir = './' + base_name
# 生成依赖文件
logger.info('生成依赖文件')
os.system('pip freeze --all >pre_requirements.txt')
with open('./pre_requirements.txt', 'r') as file:
with open('./requirements.txt', 'w') as final_file:
for line in file.readlines():
if line.startswith('-e'):
continue
for key in sys.modules.keys():
if re.search('^dophon(_\w+)*$', key):
pass
else:
if re.search('^_+', key) or re.search('(_|__|\.)+.+$', key):
# print(key,end=']')
continue
module_path = re.sub(
'-',
'_',
re.sub('''(>=|==|=>|<=|=<|<|>|=)['"\w\.]+\s+''', '', line.lower())
)
# print(module_path, '===>', line, '====>', key) if line.startswith('dophon') and key.startswith(
# 'dophon') else None
if re.search(module_path.lower(), key.lower()) or re.search(key.lower(), module_path.lower()):
if module_path in package_cache:
continue
package_cache.append(module_path)
if module_path in extra_package:
final_file.write(f'{module_path}>={extra_package[module_path]}\n')
extra_package.pop(module_path)
else:
final_file.write(line)
continue
# 写入额外包
for package_name, package_version in extra_package.items():
if package_name in extra_package:
final_file.write(
''.join(
[
package_name,
'' if has_version_expr(extra_package[package_name]) else '>=',
extra_package[package_name],
'\n']
)
)
# 会报迭代修改异常
# extra_package.popitem()
else:
final_file.write(
''.join(
[
package_name,
'' if has_version_expr(extra_package[package_name]) else '>=',
package_version,
'\n']
)
)
# 生成Dockerfile
logger.info('生成Dockerfile')
with open('./Dockerfile', 'w') as file:
file.write('FROM python:' + p_version + '\n')
file.write('ADD . ' + work_dir + '\n')
# file.write('ADD . ' + work_dir + '/' + base_name + '\n')
file.write('WORKDIR ' + work_dir + '\n')
if cache_virtual_env_dir:
file.write(f'ADD {cache_virtual_env_dir} ~/.cache_virtual_env' + '\n')
file.write(f'CMD ~/.cache_virtual_env/Scripts/activate' + '\n')
if package_cache_path:
file.write(f'ADD {package_cache_path} ~/.package_cache' + '\n')
file.write(f'CMD python -m import sys;sys.path.append("~/.package_cache");' + '\n')
if package_repository:
# 阿里云仓库 => https://mirrors.aliyun.com/pypi/simple/
file.write(f'RUN pip install -i {package_repository} -r requirements.txt' + '\n')
else:
file.write('RUN pip install --no-cache-dir -r requirements.txt' + '\n')
# 设置系统时区
file.write(f'ENV TZ={timezone}' + '\n')
file.write('RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone' + '\n')
file.write('CMD ["python","./' + (entity_file_name if entity_file_name else 'Bootstrap.py') + '"]' + '\n')
# file.write('CMD ["/bin/bash"]' + '\n')
os.system('cd ' + root)
logger.info('暂停已运行的实例')
os.system('docker stop ' + base_name)
logger.info('移除已运行的实例')
os.system('docker rm ' + base_name)
logger.info('移除旧镜像')
os.system('docker rmi ' + base_name)
logger.info('检测配置合法性')
if IsOpen('127.0.0.1', int(docker_port)):
# 端口被占用
logger.warn('映射端口被占用!!')
logger.info('建立镜像')
listen(os.system('docker build -t ' + base_name + ' .'))
logger.info('运行镜像')
os.system(
'docker run -p ' + container_port
+
':' +
docker_port +
' -d --name ' +
base_name + ' ' +
os.path.basename(
root))
if save_image:
logger.info('检测容器状态')
from subprocess import Popen, PIPE
status_code = '\'{{.State.Status}}\''
__status = 'running'
while __status == 'running':
p = Popen(
f"docker inspect {base_name} -f {status_code}",
stdout=PIPE,
stderr=PIPE
)
p.wait()
__status = eval(p.stdout.read().decode('utf-8'))
from urllib import request
try:
res = request.urlopen(f'http://127.0.0.1:{container_port}/rule/map')
# for name in dir(res):
# print(f'{name}===>{getattr(res, name)}')
if int(res.code) == 200:
break
except:
pass
if __status == 'exited':
raise Exception(f'容器启动失败,状态为{__status}')
logger.info('提交镜像')
from datetime import datetime
__commit_image_name = f'image_{base_name}{datetime.now().timestamp()}'
# 保存镜像
os.system(f"""
docker commit --author "{img_author}" --message "{img_message}" {base_name} {__commit_image_name}
""")
logger.info('生成镜像')
os.system(f"""docker save -o {base_name}.img.bak.__ {__commit_image_name}""")
exit(0)
return
logger.info('打印容器内部地址(空地址代表启动失败)')
os.system('docker inspect --format=\'{{.NetworkSettings.IPAddress}}\' ' + base_name)
logger.info('打印容器载体地址')
print(get_docker_address())
if alive_test:
logger.info('启动检测容器端口')
threading.Thread(target=listen_container_status, args=(container_port,)).start()
if attach_cmd:
logger.info('进入镜像')
# threading.Thread(target=attach_container,args=(base_name,)).start()
attach_container(base_name)
logger.info('容器启动完毕')
except Exception as e:
logger.error(e)
PK ! ֗u9 9 dophon/pre_boot.py# coding: utf-8
from dophon_properties import *
import os, sys
import xml.dom.minidom as dom
get_properties(DOPHON)
import properties
from dophon_logger import *
logger = get_logger(eval(properties.log_types))
logger.inject_logger(globals())
name_sep = '_'
modules_list = []
def check_modules():
"""
加载框架配置模块
:return:
"""
logger.info('校验模块')
# logger.info('更新pip')
# os.system(f'python -m pip install --upgrade pip')
modules_path = properties.project_root + '/module.xml'
if os.path.exists(modules_path):
with open(modules_path) as modules_file:
modules_tree = dom.parse(modules_file)
# print(modules_tree)
modules_info = modules_tree.getElementsByTagName('module')
# print(modules_info)
for module_info in modules_info:
pre_name = module_info.getElementsByTagName('pre-name')
pre_name = pre_name[0].childNodes[0].data if pre_name and pre_name[0].childNodes else None
name = module_info.getElementsByTagName('name')
name = name[0].childNodes[0].data if name and name[0].childNodes else None
version = module_info.getElementsByTagName('version')
version = version[0].childNodes[0].data if version and version[0].childNodes else None
module_name = ((pre_name + name_sep) if pre_name else '') + name
module_code_str = ((pre_name + '.') if pre_name else '') + name
# 添加模块到模块列表
modules_list.append(module_code_str)
while True:
__module = None
try:
# 校验模块安装
module = __module if __module else __import__(module_name)
# 等待模块安装完成
sys.modules[module_code_str] = module
logger.info(f'模块{getattr(module,"__name__")}依赖建立完毕,引入别名{pre_name}.{name}')
break
except Exception as e:
# print(e)
logger.info(f"install {module_name} >={version if version else 'release'}")
# 利用pip模块安装所需模块
pip_arg_list = ['pip', 'install',
module_name + (('>=' + version) if version else ''),
'--user']
exe_pip_args = ['install',
module_name + (('>=' + version) if version else ''),
'--user']
if not version:
pip_arg_list.append('-U')
exe_pip_args.append('-U')
# 利用pip安装模块
import pip
from pip._internal import main as _main
# path = os.path.dirname(os.path.dirname(__file__))
# sys.path.insert(0, path)
os.system(f'pip install {module_name}')
# _main(['list'])
# _main(exe_pip_args)
__module = __import__(module_name)
# raise ModuleNotFoundError(
# f"please use '{' '.join(pip_arg_list)}' to install module %s ")
logger.info('模块校验完毕')
PK ! 0
dophon/tools/__init__.pyimport platform
def is_windows():
return 'Windows' == platform.system()
def is_not_windows():
return not is_windows()
def show_banner():
print(f"""
.o8 oooo
"888 `888
.oooo888 .ooooo. oo.ooooo. 888 .oo. .ooooo. ooo. .oo.
d88' `888 d88' `88b 888' `88b 888P"Y88b d88' `88b `888P"Y88b
888 888 888 888 888 888 888 888 888 888 888 888
888 888 888 888 888 888 888 888 888 888 888 888
`Y8bod88P" `Y8bod8P' 888bod8P' o888o o888o `Y8bod8P' o888o o888o
888
o888o
Author:CallMeE
Base:Flask or Tornado
Url:https://github.com/Ca11MeE/dophon.git
https://gitee.com/callmee/dophon.git
""")
def module_edge_print(module_name):
def fun(f):
def fields(*args, **kwargs):
print('------------------', module_name, '------------------\n')
f(*args, **kwargs)
print('\n------------------', module_name, '------------------\n\n\n')
return fields
return fun
PK ! A7y9
9
dophon/tools/dynamic_import.pyimport os
from subprocess import Popen, PIPE, DEVNULL
from dophon_properties import *
from tqdm import tqdm
get_property(DOPHON)
from dophon_logger import *
from dophon import properties
get_logger(eval(properties.log_types)).inject_logger(globals())
from dophon.tools import is_windows
from importlib import util
def __dynamic_import(module_name, version='*'):
__module = None
while True:
try:
# 校验模块安装
util.find_spec(f'{module_name}')
# exec(f'import {module_name}')
module = __module if __module else __import__(module_name)
# sys.modules[module_name] = module
logger.info(f'模块{module_name}依赖建立完毕')
return module
except Exception as e:
# print(e)
logger.info(f"install {module_name} >={version if version else 'release'}")
# 利用pip模块安装所需模块
pip_arg_list = ['pip', 'install',
module_name + (('>=' + version) if version else ''),
'--user']
exe_pip_args = ['install',
module_name + (('>=' + version) if version else ''),
'--user']
if not version:
pip_arg_list.append('-U')
exe_pip_args.append('-U')
# 利用popen执行命令安装包
# p = Popen(f'pip install {module_name} {"" if is_windows() else "--user"}', stdout=DEVNULL, stderr=DEVNULL)
p = Popen(f'pip install {module_name} {"" if is_windows() else "--user"}',stdout=PIPE,stderr=PIPE)
p.wait()
# tqdm(p.stdout.readlines(), f'install {module_name}')
lines_queue = tqdm(p.stdout.readlines())
for item in lines_queue:
lines_queue.set_description(item)
__module = __import__(module_name)
def __d_import(module_name: str, version: str = '*'):
"""
将需要安装的包信息放入全局字典中
:param module_name:
:param version:
:return:
"""
while True:
try:
__import__(module_name)
break
except Exception as e:
# print(e)
__dynamic_import(module_name=module_name, version=version)
def d_import(module_name, version: str = '*'):
"""
对外暴露的动态添加函数入口
:param module_name:
:param version:
:return:
"""
__d_import(module_name=module_name, version=version)
PK ! ( dophon/tools/framework_const/__init__.pyPK ! eI I / dophon/tools/framework_const/error_info_type.py"""
错误信息类型
"""
HTML = 'HTML'
JSON = 'JSON'
XML = 'XML'
PK ! j( + dophon/tools/framework_const/self_result.pyfrom flask import Response
from flask import jsonify
"""
框架自身返回结果类
"""
class JsonResult:
"""
json类型返回结果类
"""
__body = {}
default_mimetype = 'application/json'
def __init__(self, event=200, data={}, msg=''):
self.__status = event
self.__body['event'] = str(event)
self.__body['data'] = str(data)
self.__body['msg'] = str(msg)
def as_res(self):
return jsonify(self.__body), self.__status
class XmlResult:
"""
XML类型返回结果类
"""
__dom = ''
default_mimetype = 'application/xml'
def __init__(self, event=200, data={}, msg=''):
self.__status = event
self.__dom = self.__dom + '\n' + str(event) + '\n\n'
self.__dom = self.__dom + '\n' + self.parse_to_node(data) + '\n\n'
self.__dom = self.__dom + '\n' + str(msg) + '\n\n'
self.__dom = self.__dom + ''
def parse_to_node(self, data):
nodes = []
if isinstance(data, dict):
for k, v in data.items():
nodes.append(
'<' + str(k) + '>\n' + (
str(v) if not isinstance(v, dict) else self.parse_to_node(v)) + '\n' + str(
k) + '>\n')
elif isinstance(data, list):
for index in range(len(data)):
nodes.append(
'<' + str(index) + '>\n' + (
str(data[index]) if not isinstance(data[index], dict) else self.parse_to_node(
data[index])) + '\n' + str(
index) + '>\n')
else:
nodes.append(str(data))
return '\n'.join(nodes)
def as_res(self):
return Response(str(self.__dom), mimetype=self.default_mimetype, status=self.__status)
PK ! XJ dophon/tools/gc/__init__.pyimport gc
gc.enable() # 开启gc
def show_gc_leak(method):
"""
打印并获取gc信息
1.不可达对象统计
2.不可回收对象统计
:param method: 打印函数引用
:return:
"""
_unreachable = gc.collect()
method(
'%s, %s, %s, %s, %s' %
(
"unreachable object num: %d" % (_unreachable,),
"garbage object num: %d" % (len(gc.garbage)),
"gc object num: %d" % (len(gc.get_objects())),
"gc threshold num: %d" % (len(gc.get_threshold())),
"gc stats num: %d" % (len(gc.get_stats())),
)
)
return {
'unreachable object num': _unreachable,
'garbage object num': len(gc.garbage),
"gc object num": len(gc.get_objects()),
# "gc object info": gc.get_objects(),
"gc threshold num": len(gc.get_threshold()),
"gc threshold info": str(gc.get_threshold()),
"gc stats num": len(gc.get_stats()),
"gc stats info": str(gc.get_stats()),
}
PK ! ! dophon/tools/sys_utils.pyimport re
def to_dict(obj: object) -> dict:
result = {}
for name in dir(obj):
if re.match('__.*__', name):
continue
else:
result[name] = getattr(obj, name)
return result
def to_lower_camel(name: str):
result = []
str_len = len(name)
for c_index in range(str_len):
c = name[c_index]
if c.islower():
result.append(c)
continue
if (c_index + 1) < str_len and name[c_index + 1].islower():
__append = c.lower()
else:
__append = c
if c_index <= 0:
result.append(__append)
continue
result.append(f'_{__append}' if __append.islower() else f'{__append}')
return ''.join(result)
PK ! :U&- &- $ dophon-1.3.1.post2.dist-info/LICENSE Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
PK !HڽT U " dophon-1.3.1.post2.dist-info/WHEEL
A
н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK !H
9 % dophon-1.3.1.post2.dist-info/METADATAJ0y6U&e
¶:t=$edRW~dj~"{M#,vMNL*S-01$wXڎx#ޠ-kTDAŴ&~3DSc~+9m%.9c|hAB"Zw8WK
f颏~qr{Āz1-r",S۟Kj|qIpP 1-88L-r臁U@젦CcctoPK !H'k D # dophon-1.3.1.post2.dist-info/RECORD}ɖJ}?T3QpY A|k>U]][{"Ln?c+Կs0ښ3*,ޗUc*!0ۭ|r=phzrʃRIV6ˣN0ܭgq/A~bKo8J|!I͛kiS[Q.wI,M3ηJ(X͠ST6jQH(63U
6q;mmS14bss?dQ፱!a4Rd{$Ld6(EKxCwcr2'04:6.0kċW^ćL4Du"?#?oǓwB[KťSI/}l:V5]#$F%D_<^C/JqODStչq^;JR
ۢ'r 'f
25WUm?Wݬ?dt9
3f$N|WKaF)6)/+<娡HMsmwAHߩ뚗cF6 Vu$ G"?>l1/s\`L;=f[_ձ
tc%DeBkM!B087{q-U+v_q[Zwb6ٱniw~3)'^ µƵ>OoN7ְ/@}GT`=:hDaFϻ55s:Yc?ꄇH[ǶxbYcjxwuKS_jɫhjVpnhwjSkYS
h!c^d!8Fp4
$Nuu$hf Gz=*=T9#,7o[=?K?2kl:[h&FUx%E^Rroi{ʲn!?˦ 4&6ÚY@k
Hwn-6IG)iiSqpǏx8J_PK ! `& dophon/__init__.pyPK ! Y Y O dophon/annotation/__init__.pyPK ! R
,
, dophon/annotation/AutoWired.pyPK ! e1 1 ) ,9 dophon/annotation/description/__init__.pyPK ! #ئ,r r ! A dophon/annotation/req/__init__.pyPK ! ! Ua dophon/annotation/res/__init__.pyPK ! :͓ If dophon/annotation/side_pkg.pyPK ! Wuv uv i dophon/boot.pyPK ! Ϻ dophon/cluster_boot.pyPK ! <-I3 I3 dophon/docker_boot.pyPK ! ֗u9 9 a dophon/pre_boot.pyPK ! 0
* dophon/tools/__init__.pyPK ! A7y9
9
0 dophon/tools/dynamic_import.pyPK ! ( : dophon/tools/framework_const/__init__.pyPK ! eI I / @; dophon/tools/framework_const/error_info_type.pyPK ! j( + ; dophon/tools/framework_const/self_result.pyPK ! XJ C dophon/tools/gc/__init__.pyPK ! ! H dophon/tools/sys_utils.pyPK ! :U&- &- $ [K dophon-1.3.1.post2.dist-info/LICENSEPK !HڽT U " x dophon-1.3.1.post2.dist-info/WHEELPK !H
9 % Wy dophon-1.3.1.post2.dist-info/METADATAPK !H'k D # z dophon-1.3.1.post2.dist-info/RECORDPK ~