添加了slack的集成
divinegift的Python项目详细描述
最有用的软件包,年轻的s7 it程序员:)
如何使用
日志记录
# Import necessary functions
from divinegift.logger import log_info, log_err, log_warning, set_loglevel
# info msg
log_info('Your message')
# error msg
log_err('Error msg',
src='Error source', # e.g. str(sys.argv[0])
mode=['telegram', 'email'] # May be empty
channel={ "telegram": -1001343660695, "email_to": ["youremail@s7.ru"]}) # You can add "email_cc" for add copy_to address
# error msg with out sending problem to external system
log_err('Error msg', src='Error src')
通过命令参数传递日志级别和日志名称
若要指定应用的日志级别和日志名称,可以通过参数发送:
if __name__ == '__main__':
# Get all args from cmd:
args = get_args()
# Get log_level, log_name, log_dir
lp = get_log_param(args)
# Set log_level and log_name:
set_loglevel(lp.get('log_level'), lp.get('log_name'), lp.get('log_dir'))
您应该成对传递参数:键值,例如--日志级别信息 可供记录的变量有:
- --日志级别或快捷方式为-ll
- --登录名或快捷方式为-ln
- --日志目录或快捷方式为-ld
日志级别可以是debug、info、warning、error
带参数的启动应用程序示例:
python test.py -ll INFO -ln test.log
配置解析(json)
from divinegift.main import Settings # Necessary imports
settings = {}
# You should use divinegift.logger.set_loglevel before config parsing
s = Settings()
s.parse_settings('./settings.conf')
settings = s.get_settings()
配置示例
{
"log_level": "INFO",
"log_name": "YourAwesomeProject.log",
"monitoring": [
"telegram",
"email"
],
"monitoring_channel": {
"telegram": -1001343660695,
"email_to": [
"aims.control@s7.ru"
]
}
}
使用数据库(sqlalchemy)
您应该用db_conn creditional定义dict。 例如:
甲骨文
安装Oracle驱动程序:
pip install cx_oracle
db_conn = {
"db_user": "dbuser", # username
"db_pass": "dbpass", # password
"db_host": "dbhost", # host (ip, fqdn). could be empty if we connect via tns
"db_port": "", # port (string). could be empty if we connect via tns
"db_name": "dbname", # database name
"db_schm": "", # db scheme if not equal username
"dialect": "oracle" # if use cx_Oracle or oracle+another_dialect
}
MSSQL
安装MSSQL驱动程序:
pip install sqlalchemy-pytds
db_conn = {
"db_user": "dbuser", # username
"db_pass": "dbpass", # password
"db_host": "", # host (ip, fqdn). could be empty if we connect via tns
"db_port": "", # port (string). could be empty if we connect via tns
"db_name": "dbname", # database name
"db_schm": "", # db scheme if not equal username
"dialect": "mssql+pytds" # mssql dialect
}
研究生
安装Postgres驱动程序:
pip install psycopg2
db_conn = {
"db_user": "dbuser", # username
"db_pass": "dbpass", # password
"db_host": "", # host (ip, fqdn). could be empty if we connect via tns
"db_port": "", # port (string). could be empty if we connect via tns
"db_name": "dbname", # database name
"db_schm": "", # db scheme if not equal username
"dialect": "postgresql+psycopg2" # dialect
}
创建连接
对于create connect,应该使用函数divinegift.smain.get_conn。
from divinegift.db import get_conn, close_conn
engine, conn, metadata = get_conn(db_conn) # db_conn - variable which was described above
如果需要使用db游标调用存储过程,则应使用原始连接。
from divinegift.db import get_raw_conn
conn = get_raw_conn(db_conn) # db_conn - variable which was described above
从sript(文件或字符串)获取数据
当得到“conn”变量时,可以直接从文件或str变量获取数据。
result = get_data('path/to/scripts/some_script.sql', db_conn)
# or you can use str variable:
script = 'select * from dual'
result = get_data(script, db_conn)
print(result)
>>>[{'dummy': 'X'}]
您可以对文件使用特定的编码(默认为“cp1251”)。 把它放入args:
result = get_data('path/to/scripts/some_script.sql', db_conn, encoding='utf8')
也可以在脚本中添加一些变量(例如日期),然后将其传递到函数中:
script = """select * from dual
where dummy = '$param'"""
parameters = {'param': 'X'}
result = get_data(script, db_conn, **parameters)
# Or another variant
result = get_data(script, db_conn, param='X')
print(result)
>>>[{'dummy': 'X'}]
在不获取数据的情况下运行脚本
您可以在不接收数据的情况下运行脚本。 您应该使用divinegift.db.run_script来获取数据,例如:
run_script('path/to/scripts/some_script.sql', db_conn)
发送电子邮件
您可以使用函数divinegift.sender.send_email
您应该设置邮件、主题和收件人列表。 简单示例:
from divinegift.sender import send_email
send_email('Test message', 'Test subject', TO=['your@domain.com'])
您可以指定到、抄送、密件抄送、主机、发件人和附件。也可以像发送html消息或类似文本一样发送。
您应该传递带有绝对路径的附件文件列表。您可以使用函数divinegift.main.get_list_files来获取它。 用于发送带有附件的电子邮件:
from divinegift.main import get_list_files
from divinegift.sender import send_email
attachment_list = get_list_files('/path/to/files', add_path=True)
send_email('Hello! This are files in attach', 'Test sending attachments', ['your@domain.com'], attachments=attachment_list)
# Also you can send only one file:
attachment = '/path/to/file/file_name'
send_email('Hello! There is file in attach', 'File', ['your@domain.com'], attachments=attachment)
如果将is html设置为false(默认为true),则可以发送类似于简单文本消息的电子邮件,而不是html
使用json
您可以简单地分析和创建json
要创建json,您应该使用divinegift.main.create\u json 要解析它,应该使用divinegift.main.parse_json
例如:
from divinegift.main import create_json, parse_json
A = {'key1': 'data1', 'key2': 'data2'}
create_json('json_file_name.json', A)
B = parse_json('json_file_name.json')
print(B)
>>> {'key1': 'data1', 'key2': 'data2'}
在俄语和英语之间音译字符串并返回
从版本1.0.8开始,您可以使用音译库在语言之间对字符串进行音译
示例:
from divinegift.translit import translit
name = 'SHEVCHENKO ANDREY'
name_r = translit(name, 'ru_ext')
name_e = translit(name_r, 'ru_ext', reversed=True)
name_r_cap = translit(name, 'ru_ext').capitalize()
name_r_low = translit(name, 'ru_ext').lower()
print(f'From English to Russian: {name}\t->\t{name_r}')
print(f'From Russian to English: {name_r}\t->\t{name_e}')
print(f'Capitalize : {name}\t->\t{name_r_cap}')
print(f'Lower : {name}\t->\t{name_r_low}')
下面将显示上面的代码:
From English to Russian: SHEVCHENKO ANDREY -> ШЕВЧЕНКО АНДРЕЙ
From Russian to English: ШЕВЧЕНКО АНДРЕЙ -> SHEVCHENKO ANDREI
Capitalize : SHEVCHENKO ANDREY -> Шевченко андрей
Lower : SHEVCHENKO ANDREY -> шевченко андрей
加密
从版本1.0.10开始,您可以使用加密模块
简单示例
示例:
from divinegift.cipher import get_key, get_cipher, encrypt_str, decrypt_str
cipher_key = get_key()
cipher = get_cipher(cipher_key)
text = 'qwerty1234!!'
text_enc = encrypt_str(text, cipher)
print(text_enc)
text_dec = decrypt_str(text_enc, cipher)
print(text_dec)
下面将输出上面的代码:
gAAAAABcanXfhUr9i__R_24rPyrHzZoMgQSTYiBmx9ZtVqdcMiGZPOxoSz4gkAW0Y9TDWpAJ6jzAjPo-mrK_IcJcdByyfWrbhQ==
qwerty1234!!
如果在encrypt和decrypt函数中使用参数get_str=false,则此函数将返回二进制字符串
使用文件中的密钥
通过write_key函数将密钥保存到文件中:
from divinegift.cipher import get_key, write_key
cipher_key = get_key()
write_key('key.ck', cipher_key)
通过read_key函数读取密钥文件:
from divinegift.cipher import read_key
cipher_key = read_key('key.ck')
凯撒
您可以使用caesar加密/解密:
from divinegift.cipher import caesar_code
text = caesar_code('Hello, World!', shift=5)
print(text)
它将输出下一个:
Mjqqt, 1twqi!
轻松加密/解密数据库密码,提高安全性
在你应该用设置来加密你的文件之前。 使用下一个代码执行此操作一次:
from divinegift.main import Settings
s = Settings()
s.parse_settings('settings.conf')
s.initialize_cipher()
s.encrypt_password('db_conn') # db_conn - name of db connection in settings.conf which contains "db_pass"
s.save_settings('settings.conf')
之后,您在'db_conn'部分的密码将自动加密。 如果您有更多的数据库连接,请在保存函数之前添加s.encrypt_password('db_conn_name_you_have')。
接下来你必须使用解密函数in使用连接的代码:
from divinegift.main import Settings
s = Settings()
s.parse_settings('settings.conf')
s.decrypt_password('db_conn') # db_conn - name of db connection in settings.conf which contains "db_pass"
实时模板。开始尽可能轻松地创建应用程序
从版本1.0.11可以从模板创建文件。 您应该为此使用模块templator。
示例: 使用以下文本创建tmp.py并运行它:
from divinegift.templator import Templator
t = Templator()
# create console app, or main logic (you can omit the file extension, '.py' will add automaticaly.):
t.create_console('your_awesome_name.py')
# or
t.create_console() # it will create 'main.py' file
# create QT-app:
t.create_gui(your_awesome_name.py')
# or
t.create_gui() # it will create 'main_gui.py' file
# create config file:
t.create_config('your_config_name.ini')
# or
t.create_config() # it will create 'settings.ini' file
# After creating file with config you can add email section on it:
t.add_email_config('your_config_name.ini')
# or
t.add_email_config() # it will add email section to 'settings.ini'
卡夫卡客户
从版本1.3.0开始,您可以使用Kafka客户端从/到主题读取和写入数据
示例:
from divinegift.kafka_client import KafkaClient
kafka_client = KafkaClient()
# Reader
kafka_client.set_consumer(**s.settings.get('consumer_config'))
messages = kafka_client.read_messages(topic_name)
# You can read all messages from begin if you needed:
messages = kafka_client.read_messages(topic_name, from_beginning=True)
# Writer
kafka_client.set_consumer(**s.settings.get('producer_config'))
kafka_client.send_message(topic_name, msg)
以上示例中的配置示例:
kafka_config: &kafka_config
bootstrap_servers:
- server.domain:9093
security_protocol: SSL
ssl_check_hostname: False
ssl_cafile: CARoot.pem
ssl_certfile: certificate.pem
ssl_keyfile: key.pem
producer_config:
<<: *kafka_config
consumer_config:
<<: *kafka_config
consumer_timeout_ms: 1000