# author = "hang" # created 10.07.2019 # encoding:utf-8 import datetime import os import ruamel import warnings import jsonlines from ruamel import yaml from configparser import ConfigParser from base import log_base logger = log_base.MyLog("config_base") def project_dir(): # get current project directory current_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) return current_dir class ConfigBase: """used to read and write files""" def __init__(self, tgt_file=None): # ready file and current project directory self.__tgt_file = project_dir() + tgt_file # logger.info(self.__tgt_file)
# author = "hang" # created 12.10.2019 import redis from base import log_base logger = log_base.MyLog("redis_base") class RedisBase: def __init__(self): self.__host = 'localhost' # redis数据库地址 self.__port = 6379 # redis数据库端口号 self.__db = 1 # redis数据库名称 self.__red = self.__redis_ini() def __enter__(self): return self def __redis_ini(self): pool = redis.ConnectionPool(host=self.__host, port=self.__port, db=self.__db) red = redis.Redis(connection_pool=pool) # logger.debug("redis数据库初始化成功。") logger.info("redis数据库初始化成功。") return red def redis_set(self, key, value, timeout=500): # 设置存储在redis数据库的失效时间 res = self.__red.set(name=key, value=value, ex=timeout)
# author = "hang" # created 13.08.2019 from base import log_base from base import config_base from requests import RequestException logger = log_base.MyLog("base_exception") # 读取配置文件 def get_config_datas(error_name): with config_base.ConfigBase("\\configs\\error_codes.yaml") as config: res = config.operation_yaml("r") for i in list(res): if i.get(error_name) is not None: return i.get(error_name) class ErrorReason(object): def __init__(self, code, message): self.code = code self.message = message class BaseException(Exception): """base exception class, derived Exception""" def __init__(self, err_code=None, massage=None): self.__massage = massage self.__err_code = err_code
# author ="demo" # created 05.07.2019 import json import requests from base import log_base from base.exception_base import BaseException from base.config_base import ConfigBase logger = log_base.MyLog("requests_base") with ConfigBase("\\configs\\config.ini") as c_b: config = c_b.read_ini() my_host = config['host']['my_host'] class RequestsBase(object): logger.info("测试开始,服务器连接中...") def __init__(self, **items): self.__host = my_host self.__run_mothed = items.get("run_mothed") self.__api_path = items.get("api_path") self.__time_out = items.get("time_out") self.__files = items.get("files") self.__case_name = items.get("case_name") with BaseException(): if "params" in items.keys(): self.__params = items.get("params") if "params" not in items.keys(): self.__params = None
# author = "hang" # created 10.07.2019 import pymysql.cursors from base import log_base logger = log_base.MyLog("data_base") class DatabaseBase: """connect DB mysql and access database""" def __init__(self, host, user, password, db, port, charset): self.__host = host self.__user = user self.__password = password self.__db = db self.__port = port self.__charset = charset def __enter__(self): self.connect = pymysql.Connect( host= self.__host, user= self.__user, password= self.__password, db= self.__db, port= self.__port, charset= self.__charset ) if self.connect.server_status == 0: logger.info("数据库连接成功。")
# created 27.06.2019 # author = "hang" import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from base import log_base logger = log_base.MyLog("mail_base") class SendMail(object): def __init__(self, smtp_server, smtp_port, smtp_sender, smtp_senderpassword, smtp_receiver, smtp_subject, smtp_body, smtp_file=None): """ to init parameter :param smtp_server: 邮件服务器 :param smtp_port:端口号 :param smtp_sender:发件人 :param smtp_senderpassword:密码 :param smtp_receiver:收件人 :param smtp_subject:邮件主题 :param smtp_body:邮件内容