Quellcode durchsuchen

Simplify list generating code with style updates.

- Use single quotes
- No more backslashes at line end
Matt Wang vor 6 Jahren
Ursprung
Commit
1ba4bc8f02
1 geänderte Dateien mit 27 neuen und 37 gelöschten Zeilen
  1. 27
    37
      cdiff.py

+ 27
- 37
cdiff.py Datei anzeigen

@@ -27,7 +27,7 @@ META_INFO = {
27 27
 }
28 28
 
29 29
 if sys.hexversion < 0x02050000:
30
-    raise SystemExit("*** Requires python >= 2.5.0")    # pragma: no cover
30
+    raise SystemExit('*** Requires python >= 2.5.0')    # pragma: no cover
31 31
 
32 32
 # Python < 2.6 does not have next()
33 33
 try:
@@ -99,7 +99,7 @@ def strsplit(text, width):
99 99
     found_colors = []
100 100
     chars_cnt = 0
101 101
     bytes_cnt = 0
102
-    while len(text) > 0:
102
+    while text:
103 103
         # First of all, check if current string begins with any escape
104 104
         # sequence.
105 105
         append_len = 0
@@ -129,7 +129,7 @@ def strsplit(text, width):
129 129
 
130 130
     # If the first string has some active colors at the splitting point,
131 131
     # reset it and append the same colors to the second string
132
-    if len(found_colors) > 0:
132
+    if found_colors:
133 133
         first += COLORS['reset']
134 134
         for color in found_colors:
135 135
             second = COLORS[color] + second
@@ -148,11 +148,10 @@ def strtrim(text, width, wrap_char, pad):
148 148
     text, _, tlen = strsplit(text, width + 1)
149 149
     if tlen > width:
150 150
         text, _, _ = strsplit(text, width - 1)
151
-
152 151
         text += wrap_char
153 152
     elif pad:
154 153
         # The string is short enough, but it might need to be padded.
155
-        text = "%s%*s" % (text, width - tlen, '')
154
+        text = '%s%*s' % (text, width - tlen, '')
156 155
     return text
157 156
 
158 157
 
@@ -189,18 +188,10 @@ class Hunk(object):
189 188
         return difflib._mdiff(self._get_old_text(), self._get_new_text())
190 189
 
191 190
     def _get_old_text(self):
192
-        out = []
193
-        for (attr, line) in self._hunk_list:
194
-            if attr != '+':
195
-                out.append(line)
196
-        return out
191
+        return [line for (attr, line) in self._hunk_list if attr != '+']
197 192
 
198 193
     def _get_new_text(self):
199
-        out = []
200
-        for (attr, line) in self._hunk_list:
201
-            if attr != '-':
202
-                out.append(line)
203
-        return out
194
+        return [line for (attr, line) in self._hunk_list if attr != '-']
204 195
 
205 196
     def is_completed(self):
206 197
         old_completed = self._old_addr[1] == len(self._get_old_text())
@@ -227,8 +218,8 @@ class UnifiedDiff(object):
227 218
         might occur after the ending @@, e.g. in git log.  '## ' usually
228 219
         indicates svn property changes in output from `svn log --diff`
229 220
         """
230
-        return (line.startswith('@@ -') and line.find(' @@') >= 8) or \
231
-               (line.startswith('## -') and line.find(' ##') >= 8)
221
+        return (line.startswith('@@ -') and line.find(' @@') >= 8 or
222
+                line.startswith('## -') and line.find(' ##') >= 8)
232 223
 
233 224
     def parse_hunk_meta(self, hunk_meta):
234 225
         # @@ -3,7 +3,6 @@
@@ -255,8 +246,8 @@ class UnifiedDiff(object):
255 246
         """Exclude old path and header line from svn log --diff output, allow
256 247
         '----' likely to see in diff from yaml file
257 248
         """
258
-        return line.startswith('-') and not self.is_old_path(line) and \
259
-            not re.match(r'^-{72}$', line.rstrip())
249
+        return (line.startswith('-') and not self.is_old_path(line) and
250
+                not re.match(r'^-{72}$', line.rstrip()))
260 251
 
261 252
     def is_new(self, line):
262 253
         return line.startswith('+') and not self.is_new_path(line)
@@ -434,8 +425,7 @@ class DiffParser(object):
434 425
                 # ignore
435 426
                 pass
436 427
 
437
-            elif diff.is_only_in_dir(line) or \
438
-                    diff.is_binary_differ(line):
428
+            elif diff.is_only_in_dir(line) or diff.is_binary_differ(line):
439 429
                 # 'Only in foo:' and 'Binary files ... differ' are considered
440 430
                 # as separate diffs, so yield current diff, then this line
441 431
                 #
@@ -509,10 +499,10 @@ class DiffMarker(object):
509 499
                         yield self._markup_old(line)
510 500
                     else:
511 501
                         # DEBUG: yield 'CHG: %s %s\n' % (old, new)
512
-                        yield self._markup_old('-') + \
513
-                            self._markup_mix(old[1], 'red')
514
-                        yield self._markup_new('+') + \
515
-                            self._markup_mix(new[1], 'green')
502
+                        yield (self._markup_old('-') +
503
+                               self._markup_mix(old[1], 'red'))
504
+                        yield (self._markup_new('+') +
505
+                               self._markup_mix(new[1], 'green'))
516 506
                 else:
517 507
                     yield self._markup_common(' ' + old[1])
518 508
 
@@ -571,7 +561,7 @@ class DiffMarker(object):
571 561
         if width <= 0:
572 562
             # Autodetection of text width according to terminal size
573 563
             try:
574
-                # Each line is like "nnn TEXT nnn TEXT\n", so width is half of
564
+                # Each line is like 'nnn TEXT nnn TEXT\n', so width is half of
575 565
                 # [terminal size minus the line number columns and 3 separating
576 566
                 # spaces
577 567
                 #
@@ -583,8 +573,8 @@ class DiffMarker(object):
583 573
         # Setup lineno and line format
584 574
         left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
585 575
         right_num_fmt = colorize('%%(right_num)%ds' % num_width, 'yellow')
586
-        line_fmt = left_num_fmt + ' %(left)s ' + COLORS['reset'] + \
587
-            right_num_fmt + ' %(right)s\n'
576
+        line_fmt = (left_num_fmt + ' %(left)s ' + COLORS['reset'] +
577
+                    right_num_fmt + ' %(right)s\n')
588 578
 
589 579
         # yield header, old path and new path
590 580
         for line in diff._headers:
@@ -638,7 +628,7 @@ class DiffMarker(object):
638 628
                     # be printed only for the first part.
639 629
                     lncur = left_num
640 630
                     rncur = right_num
641
-                    while len(left) > 0 or len(right) > 0:
631
+                    while left or right:
642 632
                         # Split both left and right lines, preserving escaping
643 633
                         # sequences correctly.
644 634
                         lcur, left, llen = strsplit(left, width)
@@ -646,7 +636,7 @@ class DiffMarker(object):
646 636
 
647 637
                         # Pad left line with spaces if needed
648 638
                         if llen < width:
649
-                            lcur = "%s%*s" % (lcur, width - llen, '')
639
+                            lcur = '%s%*s' % (lcur, width - llen, '')
650 640
 
651 641
                         yield line_fmt % {
652 642
                             'left_num': lncur,
@@ -849,11 +839,11 @@ def main():
849 839
 
850 840
     # Hack: use OptionGroup text for extra help message after option list
851 841
     option_group = OptionGroup(
852
-        parser, "Note", ("Option parser will stop on first unknown option "
853
-                         "and pass them down to underneath revision control. "
854
-                         "Environment variable CDIFF_OPTIONS may be used to "
855
-                         "specify default options that will be placed at the "
856
-                         "beginning of the argument list."))
842
+        parser, 'Note', ('Option parser will stop on first unknown option '
843
+                         'and pass them down to underneath revision control. '
844
+                         'Environment variable CDIFF_OPTIONS may be used to '
845
+                         'specify default options that will be placed at the '
846
+                         'beginning of the argument list.'))
857 847
     parser.add_option_group(option_group)
858 848
 
859 849
     # Place possible options defined in CDIFF_OPTIONS at the beginning of argv
@@ -883,8 +873,8 @@ def main():
883 873
     if stream.is_empty():
884 874
         return 0
885 875
 
886
-    if opts.color == 'always' or \
887
-            (opts.color == 'auto' and sys.stdout.isatty()):
876
+    if (opts.color == 'always' or
877
+            (opts.color == 'auto' and sys.stdout.isatty())):
888 878
         markup_to_pager(stream, opts)
889 879
     else:
890 880
         # pipe out stream untouched to make sure it is still a patch