소스 검색

Simplify list generating code with style updates.

- Use single quotes
- No more backslashes at line end
Matt Wang 6 년 전
부모
커밋
1ba4bc8f02
1개의 변경된 파일27개의 추가작업 그리고 37개의 파일을 삭제
  1. 27
    37
      cdiff.py

+ 27
- 37
cdiff.py 파일 보기

27
 }
27
 }
28
 
28
 
29
 if sys.hexversion < 0x02050000:
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
 # Python < 2.6 does not have next()
32
 # Python < 2.6 does not have next()
33
 try:
33
 try:
99
     found_colors = []
99
     found_colors = []
100
     chars_cnt = 0
100
     chars_cnt = 0
101
     bytes_cnt = 0
101
     bytes_cnt = 0
102
-    while len(text) > 0:
102
+    while text:
103
         # First of all, check if current string begins with any escape
103
         # First of all, check if current string begins with any escape
104
         # sequence.
104
         # sequence.
105
         append_len = 0
105
         append_len = 0
129
 
129
 
130
     # If the first string has some active colors at the splitting point,
130
     # If the first string has some active colors at the splitting point,
131
     # reset it and append the same colors to the second string
131
     # reset it and append the same colors to the second string
132
-    if len(found_colors) > 0:
132
+    if found_colors:
133
         first += COLORS['reset']
133
         first += COLORS['reset']
134
         for color in found_colors:
134
         for color in found_colors:
135
             second = COLORS[color] + second
135
             second = COLORS[color] + second
148
     text, _, tlen = strsplit(text, width + 1)
148
     text, _, tlen = strsplit(text, width + 1)
149
     if tlen > width:
149
     if tlen > width:
150
         text, _, _ = strsplit(text, width - 1)
150
         text, _, _ = strsplit(text, width - 1)
151
-
152
         text += wrap_char
151
         text += wrap_char
153
     elif pad:
152
     elif pad:
154
         # The string is short enough, but it might need to be padded.
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
     return text
155
     return text
157
 
156
 
158
 
157
 
189
         return difflib._mdiff(self._get_old_text(), self._get_new_text())
188
         return difflib._mdiff(self._get_old_text(), self._get_new_text())
190
 
189
 
191
     def _get_old_text(self):
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
     def _get_new_text(self):
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
     def is_completed(self):
196
     def is_completed(self):
206
         old_completed = self._old_addr[1] == len(self._get_old_text())
197
         old_completed = self._old_addr[1] == len(self._get_old_text())
227
         might occur after the ending @@, e.g. in git log.  '## ' usually
218
         might occur after the ending @@, e.g. in git log.  '## ' usually
228
         indicates svn property changes in output from `svn log --diff`
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
     def parse_hunk_meta(self, hunk_meta):
224
     def parse_hunk_meta(self, hunk_meta):
234
         # @@ -3,7 +3,6 @@
225
         # @@ -3,7 +3,6 @@
255
         """Exclude old path and header line from svn log --diff output, allow
246
         """Exclude old path and header line from svn log --diff output, allow
256
         '----' likely to see in diff from yaml file
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
     def is_new(self, line):
252
     def is_new(self, line):
262
         return line.startswith('+') and not self.is_new_path(line)
253
         return line.startswith('+') and not self.is_new_path(line)
434
                 # ignore
425
                 # ignore
435
                 pass
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
                 # 'Only in foo:' and 'Binary files ... differ' are considered
429
                 # 'Only in foo:' and 'Binary files ... differ' are considered
440
                 # as separate diffs, so yield current diff, then this line
430
                 # as separate diffs, so yield current diff, then this line
441
                 #
431
                 #
509
                         yield self._markup_old(line)
499
                         yield self._markup_old(line)
510
                     else:
500
                     else:
511
                         # DEBUG: yield 'CHG: %s %s\n' % (old, new)
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
                 else:
506
                 else:
517
                     yield self._markup_common(' ' + old[1])
507
                     yield self._markup_common(' ' + old[1])
518
 
508
 
571
         if width <= 0:
561
         if width <= 0:
572
             # Autodetection of text width according to terminal size
562
             # Autodetection of text width according to terminal size
573
             try:
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
                 # [terminal size minus the line number columns and 3 separating
565
                 # [terminal size minus the line number columns and 3 separating
576
                 # spaces
566
                 # spaces
577
                 #
567
                 #
583
         # Setup lineno and line format
573
         # Setup lineno and line format
584
         left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
574
         left_num_fmt = colorize('%%(left_num)%ds' % num_width, 'yellow')
585
         right_num_fmt = colorize('%%(right_num)%ds' % num_width, 'yellow')
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
         # yield header, old path and new path
579
         # yield header, old path and new path
590
         for line in diff._headers:
580
         for line in diff._headers:
638
                     # be printed only for the first part.
628
                     # be printed only for the first part.
639
                     lncur = left_num
629
                     lncur = left_num
640
                     rncur = right_num
630
                     rncur = right_num
641
-                    while len(left) > 0 or len(right) > 0:
631
+                    while left or right:
642
                         # Split both left and right lines, preserving escaping
632
                         # Split both left and right lines, preserving escaping
643
                         # sequences correctly.
633
                         # sequences correctly.
644
                         lcur, left, llen = strsplit(left, width)
634
                         lcur, left, llen = strsplit(left, width)
646
 
636
 
647
                         # Pad left line with spaces if needed
637
                         # Pad left line with spaces if needed
648
                         if llen < width:
638
                         if llen < width:
649
-                            lcur = "%s%*s" % (lcur, width - llen, '')
639
+                            lcur = '%s%*s' % (lcur, width - llen, '')
650
 
640
 
651
                         yield line_fmt % {
641
                         yield line_fmt % {
652
                             'left_num': lncur,
642
                             'left_num': lncur,
849
 
839
 
850
     # Hack: use OptionGroup text for extra help message after option list
840
     # Hack: use OptionGroup text for extra help message after option list
851
     option_group = OptionGroup(
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
     parser.add_option_group(option_group)
847
     parser.add_option_group(option_group)
858
 
848
 
859
     # Place possible options defined in CDIFF_OPTIONS at the beginning of argv
849
     # Place possible options defined in CDIFF_OPTIONS at the beginning of argv
883
     if stream.is_empty():
873
     if stream.is_empty():
884
         return 0
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
         markup_to_pager(stream, opts)
878
         markup_to_pager(stream, opts)
889
     else:
879
     else:
890
         # pipe out stream untouched to make sure it is still a patch
880
         # pipe out stream untouched to make sure it is still a patch