113498266Sopenharmony_ci#!/usr/bin/env python3 213498266Sopenharmony_ci# -*- coding: utf-8 -*- 313498266Sopenharmony_ci# 413498266Sopenharmony_ci# Project ___| | | | _ \| | 513498266Sopenharmony_ci# / __| | | | |_) | | 613498266Sopenharmony_ci# | (__| |_| | _ <| |___ 713498266Sopenharmony_ci# \___|\___/|_| \_\_____| 813498266Sopenharmony_ci# 913498266Sopenharmony_ci# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 1013498266Sopenharmony_ci# 1113498266Sopenharmony_ci# This software is licensed as described in the file COPYING, which 1213498266Sopenharmony_ci# you should have received as part of this distribution. The terms 1313498266Sopenharmony_ci# are also available at https://curl.se/docs/copyright.html. 1413498266Sopenharmony_ci# 1513498266Sopenharmony_ci# You may opt to use, copy, modify, merge, publish, distribute and/or sell 1613498266Sopenharmony_ci# copies of the Software, and permit persons to whom the Software is 1713498266Sopenharmony_ci# furnished to do so, under the terms of the COPYING file. 1813498266Sopenharmony_ci# 1913498266Sopenharmony_ci# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 2013498266Sopenharmony_ci# KIND, either express or implied. 2113498266Sopenharmony_ci# 2213498266Sopenharmony_ci# SPDX-License-Identifier: curl 2313498266Sopenharmony_ci# 2413498266Sopenharmony_ci"""Module for extracting test data from the test data folder and other utils""" 2513498266Sopenharmony_ci 2613498266Sopenharmony_cifrom __future__ import (absolute_import, division, print_function, 2713498266Sopenharmony_ci unicode_literals) 2813498266Sopenharmony_ci 2913498266Sopenharmony_ciimport logging 3013498266Sopenharmony_ciimport os 3113498266Sopenharmony_ciimport re 3213498266Sopenharmony_ci 3313498266Sopenharmony_cilog = logging.getLogger(__name__) 3413498266Sopenharmony_ci 3513498266Sopenharmony_ci 3613498266Sopenharmony_ciREPLY_DATA = re.compile("<reply>[ \t\n\r]*<data[^<]*>(.*?)</data>", re.MULTILINE | re.DOTALL) 3713498266Sopenharmony_ci 3813498266Sopenharmony_ci 3913498266Sopenharmony_ciclass ClosingFileHandler(logging.StreamHandler): 4013498266Sopenharmony_ci def __init__(self, filename): 4113498266Sopenharmony_ci super(ClosingFileHandler, self).__init__() 4213498266Sopenharmony_ci self.filename = os.path.abspath(filename) 4313498266Sopenharmony_ci self.setStream(None) 4413498266Sopenharmony_ci 4513498266Sopenharmony_ci def emit(self, record): 4613498266Sopenharmony_ci with open(self.filename, "a") as fp: 4713498266Sopenharmony_ci self.setStream(fp) 4813498266Sopenharmony_ci super(ClosingFileHandler, self).emit(record) 4913498266Sopenharmony_ci self.setStream(None) 5013498266Sopenharmony_ci 5113498266Sopenharmony_ci def setStream(self, stream): 5213498266Sopenharmony_ci setStream = getattr(super(ClosingFileHandler, self), 'setStream', None) 5313498266Sopenharmony_ci if callable(setStream): 5413498266Sopenharmony_ci return setStream(stream) 5513498266Sopenharmony_ci if stream is self.stream: 5613498266Sopenharmony_ci result = None 5713498266Sopenharmony_ci else: 5813498266Sopenharmony_ci result = self.stream 5913498266Sopenharmony_ci self.acquire() 6013498266Sopenharmony_ci try: 6113498266Sopenharmony_ci self.flush() 6213498266Sopenharmony_ci self.stream = stream 6313498266Sopenharmony_ci finally: 6413498266Sopenharmony_ci self.release() 6513498266Sopenharmony_ci return result 6613498266Sopenharmony_ci 6713498266Sopenharmony_ciclass TestData(object): 6813498266Sopenharmony_ci def __init__(self, data_folder): 6913498266Sopenharmony_ci self.data_folder = data_folder 7013498266Sopenharmony_ci 7113498266Sopenharmony_ci def get_test_data(self, test_number): 7213498266Sopenharmony_ci # Create the test file name 7313498266Sopenharmony_ci filename = os.path.join(self.data_folder, 7413498266Sopenharmony_ci "test{0}".format(test_number)) 7513498266Sopenharmony_ci 7613498266Sopenharmony_ci log.debug("Parsing file %s", filename) 7713498266Sopenharmony_ci 7813498266Sopenharmony_ci with open(filename, "rb") as f: 7913498266Sopenharmony_ci contents = f.read().decode("utf-8") 8013498266Sopenharmony_ci 8113498266Sopenharmony_ci m = REPLY_DATA.search(contents) 8213498266Sopenharmony_ci if not m: 8313498266Sopenharmony_ci raise Exception("Couldn't find a <reply><data> section") 8413498266Sopenharmony_ci 8513498266Sopenharmony_ci # Left-strip the data so we don't get a newline before our data. 8613498266Sopenharmony_ci return m.group(1).lstrip() 8713498266Sopenharmony_ci 8813498266Sopenharmony_ci 8913498266Sopenharmony_ciif __name__ == '__main__': 9013498266Sopenharmony_ci td = TestData("./data") 9113498266Sopenharmony_ci data = td.get_test_data(1) 9213498266Sopenharmony_ci print(data) 93