Skip to main content

SNG development Tools Kit

Project description

SNG Tools Kit

SNG开发套件,内部 python 开发通用接口。

模块列表

  • General
  • Log
  • Config
  • MQTTClient

引入示例:

import sng_tk as stk

使用方法

首先安装:

python3 setup.py install

如果需要更新也可在代码更新后执行上面这行命令。

通用模块(General)

示例:

import sng_tk as stk

# 打印公司Logo
stk.general.print_header()
# 处理路径中特殊的格式(目前可以将~解析为主目录)
print(stk.general.parse_path("~/sng/log"))

日志模块(Log)

示例:

import sng_tk as stk

log = stk.Log("sng-tools-kit")
log.info("normal log")
log.debug("debug log")
log.warning("warning log")
log.error("error log")
log.critical("critical log")

上面的示例需要在/etc/sng中存在sng-tools-kit.yaml配置文件,其中包含以下配置:

或者也可传入一个dict类型的数据作为配置文件,但要求必须与以下文件保持一致。

# 日志模块相关配置
Log:

  # 关闭日志输出(不建议设置为true),若该项设置为true,则完全关闭日志输出,下面的所有配置也无法生效。
  Close: false

  # 日志输出等级,有以下选项,从上到下越来越高:
  #    0. DEBUG: 调试日志
  #    1. INFO: 普通日志(默认)
  #    2. WARNING: 警告日志
  #    3. ERROR: 错误日志
  #    4. CRITICAL: 严重错误日志
  # 系统将输出指定等级及其更高等级的日志。
  Level: 1

  # 日志文件统一的输出目录,其中会包含来自各个模块的日志。
  # 需要注意的是,程序必须拥有这里提供的路径的写权限,否则程序将无法正常运行。
  Directory: "~/sng/log"

  # 日志的最大尺寸,单位:MB
  MaxSize: 10

配置文件模块(Config)

示例:

import sng_tk as stk

config_data = stk.Config("sng-tools-kit").read()
print("Config test:", config_data["config"]["test"])

上面的示例需要在/etc/sng中存在sng-tools-kit.yaml配置文件,其中包含以下配置:

config:
  test: "config read success!"

MQTT客户端模块(MQTTClient)

示例:

import time
from sng_tk import Config as SConfig
from sng_tk import Log as SLog
from sng_tk import MQTTClient as SMQTTClient

log = SLog("sng-tools-kit-example")
conf = SConfig("sng-tools-kit-example").read()
mqtt = SMQTTClient(conf["mqtt"], log)


def on_connect(client, userdata, flags, rc):
    print(f"mqtt connect success!{flags}")


def on_subscribe(client, userdata, mid, granted_qos):
    print(f"subscribe qos: {granted_qos[0]}")


def on_message(client, userdata, message):
    print(message.payload.decode("utf-8"))


# 绑定事件
mqtt.set_connect_event(on_connect)
mqtt.set_subscribe_event(on_subscribe)
mqtt.set_message_event(on_message)

# 连接、订阅、执行
mqtt.connect()
mqtt.subscribe("abc")
mqtt.start()

# 防止主线程退出
while True:
    time.sleep(10)

配置文件示例可以在config/sng-tools-kit-example.yaml中找到。

例如:

mqtt:
  # 中转站配置
  broker:
    ip: "127.0.0.1"
    port: 1883
  username: "sng"
  password: "sng"
  client_id: "FASTAPI_SERVER_S2020_SN01"
  tls_enable: false
  TSL:
    ca_certs: '~/SNG-Project/data-access-server/conf/ca/ca.crt'
    cert_file: '~/SNG-Project/data-access-server/conf/ca/client.crt'
    key_file: '~/SNG-Project/data-access-server/conf/ca/client.key'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

sng_tk-1.2.3-py3-none-any.whl (13.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page