From ba69efde72720cb8727aa3221de9829b76f43c8b Mon Sep 17 00:00:00 2001
From: Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
Date: Wed, 6 Jan 2016 00:02:31 +0100
Subject: [PATCH 1/2] wrap-and-sort: Pass options dict for brevity

---
 scripts/wrap-and-sort | 40 +++++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/scripts/wrap-and-sort b/scripts/wrap-and-sort
index 98a4bb5..15c8bb0 100755
--- a/scripts/wrap-and-sort
+++ b/scripts/wrap-and-sort
@@ -64,34 +64,30 @@ SUPPORTED_FILES = (
 
 
 class WrapAndSortControl(Control):
-    def __init__(self, filename, max_line_length):
+    def __init__(self, filename, options):
         super().__init__(filename)
-        self.max_line_length = max_line_length
+        self.opts = options
 
-    def wrap_and_sort(self, wrap_always, short_indent, sort_paragraphs,
-                      keep_first, trailing_comma):
+    def wrap_and_sort(self):
         for paragraph in self.paragraphs:
             for field in CONTROL_LIST_FIELDS:
                 if field in paragraph:
-                    self._wrap_field(paragraph, field, wrap_always,
-                                     short_indent, trailing_comma)
+                    self._wrap_field(paragraph, field, True)
             if "Uploaders" in paragraph:
-                self._wrap_field(paragraph, "Uploaders", wrap_always,
-                                 short_indent, trailing_comma, False)
+                self._wrap_field(paragraph, "Uploaders", False)
             if "Architecture" in paragraph:
                 archs = set(paragraph["Architecture"].split())
                 # Sort, with wildcard entries (such as linux-any) first:
                 archs = sorted(archs, key=lambda x: ("any" not in x, x))
                 paragraph["Architecture"] = " ".join(archs)
 
-        if sort_paragraphs:
-            first = self.paragraphs[:1 + int(keep_first)]
-            sortable = self.paragraphs[1 + int(keep_first):]
+        if self.opts.sort_binary_packages:
+            first = self.paragraphs[:1 + int(self.opts.keep_first)]
+            sortable = self.paragraphs[1 + int(self.opts.keep_first):]
             key = lambda x: x.get("Package")
             self.paragraphs = first + sorted(sortable, key=key)
 
-    def _wrap_field(self, control, entry, wrap_always, short_indent,
-                    trailing_comma, sort=True):
+    def _wrap_field(self, control, entry, sort):
         # An empty element is not explicitly disallowed by Policy but known to
         # break QA tools, so remove any
         packages = list(filter(None, [x.strip() for x in control[entry].split(",")]))
@@ -105,20 +101,20 @@ class WrapAndSortControl(Control):
             packages = sort_list(packages)
 
         length = len(entry) + sum([2 + len(package) for package in packages])
-        if wrap_always or length > self.max_line_length:
+        if self.opts.wrap_always or length > self.opts.max_line_length:
             indentation = " "
-            if not short_indent:
-                indentation *= len(entry) + 2
+            if not self.opts.short_indent:
+                indentation *= len(entry) + len(": ")
             packages_with_indention = [indentation + x for x in packages]
             packages_with_indention = ",\n".join(packages_with_indention)
-            if trailing_comma:
+            if self.opts.trailing_comma:
                 packages_with_indention += ','
-            if short_indent:
+            if self.opts.short_indent:
                 control[entry] = "\n" + packages_with_indention
             else:
                 control[entry] = packages_with_indention.strip()
         else:
-            control[entry] = ", ".join(packages).strip()
+            control[entry] = ", ".join(packages)
 
 
 class Install(object):
@@ -168,12 +164,10 @@ def wrap_and_sort(options):
     for control_file in control_files:
         if options.verbose:
             print(control_file)
-        control = WrapAndSortControl(control_file, options.max_line_length)
+        control = WrapAndSortControl(control_file, options)
         if options.cleanup:
             control.strip_trailing_spaces()
-        control.wrap_and_sort(options.wrap_always, options.short_indent,
-                              options.sort_binary_packages, options.keep_first,
-                              options.trailing_comma)
+        control.wrap_and_sort()
         control.save()
 
     copyright_files = [f for f in options.files
-- 
2.6.4

