[Libguestfs] [PATCH 5/8] python: PEP 8: avoid too long lines

Pino Toscano ptoscano at redhat.com
Wed May 4 14:23:15 UTC 2016


Reindent Python scripts to make sure lines are not longer than 80
columns.

Regarding autogenerated code (guestfs.py and bindtests.py): add an
helper function to make sure comma-separated lists are wrapped at the
wanted length.

This produces only differences in the indentation of long Python lines,
with no behaviour changes.
---
 generator/bindtests.ml       |  4 +++-
 generator/python.ml          | 50 ++++++++++++++++++++++++++++++++++----------
 generator/python.mli         |  8 +++++++
 python/t/test090RetValues.py |  9 +++++---
 python/t/tests_helper.py     |  6 ++++--
 tests/http/test-http.py      |  4 ++--
 6 files changed, 62 insertions(+), 19 deletions(-)

diff --git a/generator/bindtests.ml b/generator/bindtests.ml
index c6a4c6b..742cb1b 100644
--- a/generator/bindtests.ml
+++ b/generator/bindtests.ml
@@ -467,7 +467,9 @@ g = guestfs.GuestFS()
   in
 
   generate_lang_bindtests (
-    fun f args optargs -> pr "g.%s(%s)\n" f (mkargs args optargs)
+    fun f args optargs ->
+      pr "g.%s(%s)\n" f
+        (Python.indent_python (mkargs args optargs) (3 + String.length f) 78)
   );
 
   pr "print(\"EOF\")\n"
diff --git a/generator/python.ml b/generator/python.ml
index b5ab168..d577ef1 100644
--- a/generator/python.ml
+++ b/generator/python.ml
@@ -789,16 +789,22 @@ class GuestFS(object):
 
 ";
 
+  let map_join f l =
+    String.concat "" (List.map f l)
+  in
+
   List.iter (
     fun f ->
       let ret, args, optargs = f.style in
-      pr "    def %s(self" f.name;
-      List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
-      List.iter (
-        fun optarg ->
-          pr ", %s=None" (name_of_optargt optarg)
-      ) optargs;
-      pr "):\n";
+      let len_name = String.length f.name in
+      let decl_string =
+        "self" ^
+        map_join (fun arg ->sprintf ", %s" (name_of_argt arg))
+          args ^
+        map_join (fun optarg -> sprintf ", %s=None" (name_of_optargt optarg))
+          optargs in
+      pr "    def %s(%s):\n"
+        f.name (indent_python decl_string (9 + len_name) 78);
 
       if is_documented f then (
         let doc = replace_str f.longdesc "C<guestfs_" "C<g." in
@@ -849,10 +855,12 @@ class GuestFS(object):
           pr "        %s = %s.c_pointer()\n" n n
       ) args;
       pr "        self._check_not_closed()\n";
-      pr "        r = libguestfsmod.%s(self._o" f.name;
-      List.iter (fun arg -> pr ", %s" (name_of_argt arg))
-        (args @ args_of_optargs optargs);
-      pr ")\n";
+      let function_string =
+        "self._o" ^
+        map_join (fun arg -> sprintf ", %s" (name_of_argt arg))
+          (args @ args_of_optargs optargs) in
+      pr "        r = libguestfsmod.%s(%s)\n"
+        f.name (indent_python function_string (27 + len_name) 78);
 
       (* For RHashtable, if self._python_return_dict=True then we
        * have to convert the result to a dict.
@@ -872,3 +880,23 @@ class GuestFS(object):
           pr "    %s = %s\n\n" alias f.name
       ) f.non_c_aliases
   ) external_functions_sorted
+
+and indent_python str indent columns =
+  let rec loop str endpos =
+    let len = String.length str in
+    if len + indent > columns then
+      try
+        let pos = String.rindex_from str endpos ',' in
+        if pos + indent > columns then
+          loop str (pos - 1)
+        else (
+          let rest = String.sub str (pos + 2) (len - pos - 2) in
+          String.sub str 0 pos :: loop rest (String.length rest - 1)
+        )
+      with Not_found ->
+        [str]
+    else
+      [str]
+  in
+  let lines = loop str (String.length str - 1) in
+  String.concat (",\n" ^ String.make indent ' ') lines
diff --git a/generator/python.mli b/generator/python.mli
index 919dab3..d1d0247 100644
--- a/generator/python.mli
+++ b/generator/python.mli
@@ -18,3 +18,11 @@
 
 val generate_python_c : unit -> unit
 val generate_python_py : unit -> unit
+
+val indent_python : string -> int -> int -> string
+(** [indent_python str indent columns] indents a Python comma-based string
+    like "foo, bar, etc" (with space after the comma), splitting at commas
+    so each line does not take more than [columns] characters, including
+    [indent].
+
+    Lines after the first are indented with [indent] spaces. *)
diff --git a/python/t/test090RetValues.py b/python/t/test090RetValues.py
index c310cf1..d9a68d1 100644
--- a/python/t/test090RetValues.py
+++ b/python/t/test090RetValues.py
@@ -33,7 +33,8 @@ class Test090PythonRetValues(unittest.TestCase):
     def test_rint64(self):
         g = guestfs.GuestFS()
 
-        self.assertAlmostEqual(g.internal_test_rint64("10"), int_type(10), places=1)
+        self.assertAlmostEqual(g.internal_test_rint64("10"),
+                               int_type(10), places=1)
 
         self.assertRaises(RuntimeError, g.internal_test_rint64err)
 
@@ -55,7 +56,8 @@ class Test090PythonRetValues(unittest.TestCase):
     def test_rconstoptstring(self):
         g = guestfs.GuestFS()
 
-        self.assertEqual(g.internal_test_rconstoptstring("test"), "static string")
+        self.assertEqual(g.internal_test_rconstoptstring("test"),
+                         "static string")
 
         # this never fails
         self.assertIsNone(g.internal_test_rconstoptstringerr())
@@ -71,7 +73,8 @@ class Test090PythonRetValues(unittest.TestCase):
         g = guestfs.GuestFS()
 
         self.assertEqual(g.internal_test_rstringlist("0"), [])
-        self.assertEqual(g.internal_test_rstringlist("5"), ["0", "1", "2", "3", "4"])
+        self.assertEqual(g.internal_test_rstringlist("5"),
+                         ["0", "1", "2", "3", "4"])
 
         self.assertRaises(RuntimeError, g.internal_test_rstringlisterr)
 
diff --git a/python/t/tests_helper.py b/python/t/tests_helper.py
index f47b157..8493c40 100644
--- a/python/t/tests_helper.py
+++ b/python/t/tests_helper.py
@@ -37,7 +37,8 @@ def skipUnlessLibvirtHasCPointer():
         import libvirt
     except:
         return unittest.skip("could not import libvirt")
-    # Check we're using the version of libvirt-python that has c_pointer() methods.
+    # Check we're using the version of libvirt-python that has
+    # c_pointer() methods.
     if not "c_pointer" in dir(libvirt.open(None)):
         return unittest.skip("libvirt-python doesn't support c_pointer()")
     return lambda func: func
@@ -66,5 +67,6 @@ def skipUnlessArchMatches(arch_re):
     machine = platform.machine()
     rex = re.compile(arch_re)
     if not rex.match(machine):
-        return unittest.skip("the current architecture (%s) does not match '%s'" % (machine, arch_re))
+        return unittest.skip("the current architecture (%s) does not match "
+                             "'%s'" % (machine, arch_re))
     return lambda func: func
diff --git a/tests/http/test-http.py b/tests/http/test-http.py
index 2876ea5..a353925 100755
--- a/tests/http/test-http.py
+++ b/tests/http/test-http.py
@@ -95,8 +95,8 @@ if pid > 0:
         exit(1)
     if len(roots) > 1:
         print >>sys.stderr, \
-            ("%s: error: inspection found a multi-boot OS which is not expected" %
-             progname)
+            ("%s: error: inspection found a multi-boot OS which is not "
+             "expected" % progname)
         exit(1)
 
     type_ = g.inspect_get_type(roots[0])
-- 
2.5.5




More information about the Libguestfs mailing list