예제 #1
0
def reflect_request_with_database_callback(database: PySQLPool,
                                           do_action: object, *args):
    """
    用于数据库连接池自释放,提供一个可供用户使用的数据库连接和回调函数

    @Args:
    * [database] 用于访问的数据库
    * [do_action] 回调函数, 函数结构为callback(conn, [args]), 如果不需要附加参数,那么回调函数只为callback(conn)
    * [args] 用户需要添加的参数
    """
    if not isinstance(database, PySQLPool):
        raise Exceptions.NoAvailableResourcesFoundException(
            "database is not available")

    conn = None

    try:
        # get connection from pool
        conn = database.get_connection()

        # check the connection status
        if not PySQLConnection.check_connection(conn):
            PySQLConnection.reconnect(conn)

        # send database connection to callback function
        if args is None:
            return do_action(conn)
        else:
            return do_action(conn, args)

    finally:
        if conn:
            database.put_connection(conn)
예제 #2
0
    def parsing_reg_file(self, rule_file: str, args: dict):
        """
        Parse the configuration file, and then verify the validity of the input parameters

        @Args:
        * [ruleFile] str, path of file
        * [args] dict, data contained with key and value

        @Returns:
        * [dict(str:obj)] dict type
        """

        if not FileUtils.exists(rule_file):
            raise Exceptions.NoAvailableResourcesFoundException(
                "file: {} not found".format(rule_file))

        # parse the file
        for line in open(rule_file, "rt"):
            # trim tailor
            line = self.trim_tailer_from_text(line)

            # split token from spaces
            seps = line.split(' ')

            # check size
            if len(seps) != 3:
                raise Exceptions.InvalidArithException(
                    "regular line: {} is broken, [argument key] [mapping key] ["
                    "regular expression]".format(line))

                # append regular token to list
            self.tokens.append(
                RegularToken(seps[0], seps[1], R"{}".format(seps[2])))

        # After parsing the file, first extract the dictionary consisting of {old key-regular match} from the file,
        # and then perform regular filtering on the parameters
        filtered_args = self.use_args_regular_check(args)

        return filtered_args