Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Mauro Carvalho Chehab <mauro.chehab@linux.intel.com>
To: igt-dev@lists.freedesktop.org
Subject: [igt-dev] [PATCH i-g-t v4 08/11] scripts/test_list.py: add support to return a string instead of print
Date: Thu, 31 Aug 2023 16:52:10 +0200	[thread overview]
Message-ID: <20230831145509.477543-9-mauro.chehab@linux.intel.com> (raw)
In-Reply-To: <20230831145509.477543-1-mauro.chehab@linux.intel.com>

From: Mauro Carvalho Chehab <mchehab@kernel.org>

Sometimes, we don't want to print a testlist, but instead to return
it for post-processing. One such example is to add a Sphinx extension
to parse it and place the results on some existing ReST file.

Add support for it.

A side effect is that we don't need to redirect capture anymore to
to store the output on a file.

Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
---
 scripts/test_list.py | 79 ++++++++++++++++++++------------------------
 1 file changed, 36 insertions(+), 43 deletions(-)

diff --git a/scripts/test_list.py b/scripts/test_list.py
index e5bf24f9b5ef..8ef3a136688f 100644
--- a/scripts/test_list.py
+++ b/scripts/test_list.py
@@ -596,20 +596,13 @@ class TestList:
     # Output methods
     #
 
-    def print_rest_flat(self, filename = None):
+    def print_rest_flat(self, filename = None, return_string = False):
 
         """Print tests and subtests ordered by tests"""
 
-        handler = None
-        if filename:
-            original_stdout = sys.stdout
-            handler = open(filename, "w", encoding='utf8') # pylint: disable=R1732
-            sys.stdout = handler
-
-        print("=" * len(self.title))
-        print(self.title)
-        print("=" * len(self.title))
-        print()
+        out = "=" * len(self.title) + "\n"
+        out += self.title + "\n"
+        out += "=" * len(self.title) + "\n\n"
 
         for test in sorted(self.doc.keys()):
             fname = self.doc[test]["File"]
@@ -631,10 +624,9 @@ class TestList:
             if not subtest_array:
                 continue
 
-            print(len(name) * '=')
-            print(name)
-            print(len(name) * '=')
-            print()
+            out += len(name) * '=' + "\n"
+            out += name + "\n"
+            out += len(name) * '=' + "\n\n"
 
             for field in sorted(self.doc[test].keys()):
                 if field == "subtest":
@@ -642,29 +634,30 @@ class TestList:
                 if field in self.internal_fields:
                     continue
 
-                print(f":{field}: {self.doc[test][field]}")
+                out += f":{field}: {self.doc[test][field]}\n"
 
             for subtest in subtest_array:
 
-                print()
-                print(subtest["_summary_"])
-                print(len(subtest["_summary_"]) * '=')
-                print("")
+                out += "\n" + subtest["_summary_"] + "\n"
+                out += len(subtest["_summary_"]) * '=' + "\n\n"
 
                 for field in sorted(subtest.keys()):
                     if field in self.internal_fields:
                         continue
 
-                    print(f":{field}:", subtest[field])
+                    out += f":{field}:" + subtest[field] + "\n"
 
-                print()
+                out += "\n"
 
-            print()
-            print()
+            out += "\n\n"
 
-        if handler:
-            handler.close()
-            sys.stdout = original_stdout
+        if filename:
+            with open(filename, "w", encoding='utf8') as handle:
+                handle.write(out)
+        elif not return_string:
+            print(out)
+        else:
+            return out
 
     def get_spreadsheet(self):
 
@@ -708,7 +701,7 @@ class TestList:
                     sheet[row].append('')
         return sheet
 
-    def print_nested_rest(self, filename = None):
+    def print_nested_rest(self, filename = None, return_string = False):
 
         """Print tests and subtests ordered by tests"""
 
@@ -718,10 +711,9 @@ class TestList:
             handler = open(filename, "w", encoding='utf8') # pylint: disable=R1732
             sys.stdout = handler
 
-        print("=" * len(self.title))
-        print(self.title)
-        print("=" * len(self.title))
-        print()
+        out = "=" * len(self.title) + "\n"
+        out += self.title + "\n"
+        out += "=" * len(self.title) + "\n\n"
 
         # Identify the sort order for the fields
         fields_order = []
@@ -765,14 +757,11 @@ class TestList:
 
                 title_str = fields_order[i] + ": " + fields[fields_order[i]]
 
-                print(title_str)
-                print(level_markers[marker] * len(title_str))
-                print()
+                out += title_str + "\n"
+                out += level_markers[marker] * len(title_str) + "\n\n"
                 marker += 1
 
-            print()
-            print("``" + subtest + "``")
-            print()
+            out += "\n``" + subtest + "``" + "\n\n"
 
             # print non-hierarchy fields
             for field in fields_order:
@@ -780,7 +769,7 @@ class TestList:
                     continue
 
                 if field in fields:
-                    print(f":{field}: {fields[field]}")
+                    out += f":{field}: {fields[field]}" + "\n"
 
             # Store current values
             for i in range(cur_level, len(fields_order)):
@@ -792,11 +781,15 @@ class TestList:
                 else:
                     old_fields[i] = ''
 
-            print()
+            out += "\n"
 
-        if handler:
-            handler.close()
-            sys.stdout = original_stdout
+        if filename:
+            with open(filename, "w", encoding='utf8') as handle:
+                handle.write(out)
+        elif not return_string:
+            print(out)
+        else:
+            return out
 
     def print_json(self, out_fname):
 
-- 
2.41.0

  parent reply	other threads:[~2023-08-31 14:55 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31 14:52 [igt-dev] [PATCH i-g-t v4 00/11] Make test_list.py more generic Mauro Carvalho Chehab
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 01/11] scripts/test_list.py: make the class " Mauro Carvalho Chehab
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 02/11] scripts/test_list.py: rename the internal summary value Mauro Carvalho Chehab
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 03/11] scripts/test_list.py: document what BAT stands for Mauro Carvalho Chehab
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 04/11] scripts/test_list.py: close config file before start processing Mauro Carvalho Chehab
2023-08-31 17:13   ` Kamil Konieczny
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 05/11] scripts/test_list.py: filename arguments for print_ methods are optional Mauro Carvalho Chehab
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 06/11] scripts/test_list.py: add a check before deleting properties Mauro Carvalho Chehab
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 07/11] scripts/test_list.py: skip some internal fields Mauro Carvalho Chehab
2023-08-31 17:16   ` Kamil Konieczny
2023-08-31 14:52 ` Mauro Carvalho Chehab [this message]
2023-08-31 17:17   ` [igt-dev] [PATCH i-g-t v4 08/11] scripts/test_list.py: add support to return a string instead of print Kamil Konieczny
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 09/11] scripts/test_list.py: don't rely on file name to parse config Mauro Carvalho Chehab
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 10/11] scripts/test_list.py: allow passing a config dict directly Mauro Carvalho Chehab
2023-08-31 14:52 ` [igt-dev] [PATCH i-g-t v4 11/11] scripts/igt_doc.py: use field names when create a TestList instance Mauro Carvalho Chehab
2023-08-31 19:35 ` [igt-dev] ✗ GitLab.Pipeline: warning for Make test_list.py more generic Patchwork
2023-08-31 20:02 ` [igt-dev] ✓ CI.xeBAT: success " Patchwork
2023-08-31 20:12 ` [igt-dev] ✗ Fi.CI.BAT: failure " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230831145509.477543-9-mauro.chehab@linux.intel.com \
    --to=mauro.chehab@linux.intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox