2
0

curl_test_data.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2017 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.haxx.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. """Module for extracting test data from the test data folder"""
  23. from __future__ import (absolute_import, division, print_function,
  24. unicode_literals)
  25. import os
  26. import re
  27. import logging
  28. log = logging.getLogger(__name__)
  29. REPLY_DATA = re.compile("<reply>[ \t\n\r]*<data[^<]*>(.*?)</data>", re.MULTILINE | re.DOTALL)
  30. class TestData(object):
  31. def __init__(self, data_folder):
  32. self.data_folder = data_folder
  33. def get_test_data(self, test_number):
  34. # Create the test file name
  35. filename = os.path.join(self.data_folder,
  36. "test{0}".format(test_number))
  37. log.debug("Parsing file %s", filename)
  38. with open(filename, "rb") as f:
  39. contents = f.read().decode("utf-8")
  40. m = REPLY_DATA.search(contents)
  41. if not m:
  42. raise Exception("Couldn't find a <reply><data> section")
  43. # Left-strip the data so we don't get a newline before our data.
  44. return m.group(1).lstrip()
  45. if __name__ == '__main__':
  46. td = TestData("./data")
  47. data = td.get_test_data(1)
  48. print(data)