Browse code

Fix most flake8 errors

Peter Hill authored on 02/07/2016 10:27:27 • Jose Fonseca committed on 10/07/2016 08:40:15
Showing 12 changed files

1 1
similarity index 100%
2 2
rename from HACKING.markdown
3 3
rename to HACKING.md
4 4
similarity index 100%
5 5
rename from README.markdown
6 6
rename to README.md
... ...
@@ -33,9 +33,9 @@ class MyDotWindow(xdot.DotWindow):
33 33
 
34 34
     def on_url_clicked(self, widget, url, event):
35 35
         dialog = Gtk.MessageDialog(
36
-                parent = self, 
37
-                buttons = Gtk.ButtonsType.OK,
38
-                message_format="%s clicked" % url)
36
+            parent=self,
37
+            buttons=Gtk.ButtonsType.OK,
38
+            message_format="%s clicked" % url)
39 39
         dialog.connect('response', lambda dialog, response: dialog.destroy())
40 40
         dialog.run()
41 41
         return True
... ...
@@ -26,7 +26,6 @@ gi.require_version('Gtk', '3.0')
26 26
 
27 27
 from gi.repository import Gtk
28 28
 from gi.repository import Gdk
29
-from gi.repository import GdkPixbuf
30 29
 
31 30
 from xdot import DotWidget, DotWindow
32 31
 
... ...
@@ -68,7 +68,7 @@ Shortcuts:
68 68
     width = height = 512
69 69
     if options.geometry:
70 70
         try:
71
-            width,height = (int(i) for i in options.geometry.split('x'))
71
+            width, height = (int(i) for i in options.geometry.split('x'))
72 72
         except ValueError:
73 73
             parser.error('invalid window geometry')
74 74
 
... ...
@@ -44,7 +44,9 @@ class ParseError(Exception):
44 44
         self.col = col
45 45
 
46 46
     def __str__(self):
47
-        return ':'.join([str(part) for part in (self.filename, self.line, self.col, self.msg) if part != None])
47
+        return ':'.join([str(part) for part in
48
+                         (self.filename, self.line, self.col, self.msg)
49
+                         if part is not None])
48 50
 
49 51
 
50 52
 class Lexer:
... ...
@@ -55,7 +57,7 @@ class Lexer:
55 57
 
56 58
     newline_re = re.compile(br'\r\n?|\n')
57 59
 
58
-    def __init__(self, buf = None, pos = 0, filename = None, fp = None):
60
+    def __init__(self, buf=None, pos=0, filename=None, fp=None):
59 61
         if fp is not None:
60 62
             try:
61 63
                 fileno = fp.fileno()
... ...
@@ -69,7 +71,7 @@ class Lexer:
69 71
                 # map the whole file into memory
70 72
                 if length:
71 73
                     # length must not be zero
72
-                    buf = mmap.mmap(fileno, length, access = mmap.ACCESS_READ)
74
+                    buf = mmap.mmap(fileno, length, access=mmap.ACCESS_READ)
73 75
                     pos = os.lseek(fileno, 0, 1)
74 76
                 else:
75 77
                     buf = b''
... ...
@@ -108,7 +110,7 @@ class Lexer:
108 110
                 raise ParseError(msg, self.filename, line, col)
109 111
             else:
110 112
                 break
111
-        return Token(type = type, text = text, line = line, col = col)
113
+        return Token(type=type, text=text, line=line, col=col)
112 114
 
113 115
     def consume(self, text):
114 116
         # update line number
... ...
@@ -124,7 +126,7 @@ class Lexer:
124 126
             if tabpos == -1:
125 127
                 break
126 128
             self.col += tabpos - pos
127
-            self.col = ((self.col - 1)//self.tabsize + 1)*self.tabsize + 1
129
+            self.col = ((self.col - 1) // self.tabsize + 1) * self.tabsize + 1
128 130
             pos = tabpos + 1
129 131
         self.col += len(text) - pos
130 132
 
... ...
@@ -68,19 +68,19 @@ class Parser:
68 68
     def match(self, type):
69 69
         if self.lookahead.type != type:
70 70
             raise ParseError(
71
-                msg = 'unexpected token %r' % self.lookahead.text,
72
-                filename = self.lexer.filename,
73
-                line = self.lookahead.line,
74
-                col = self.lookahead.col)
71
+                msg='unexpected token {}'.format(self.lookahead.text),
72
+                filename=self.lexer.filename,
73
+                line=self.lookahead.line,
74
+                col=self.lookahead.col)
75 75
 
76 76
     def skip(self, type):
77 77
         while self.lookahead.type != type:
78 78
             if self.lookahead.type == EOF:
79 79
                 raise ParseError(
80
-                   msg = 'unexpected end of file',
81
-                   filename = self.lexer.filename,
82
-                   line = self.lookahead.line,
83
-                   col = self.lookahead.col)
80
+                    msg='unexpected end of file',
81
+                    filename=self.lexer.filename,
82
+                    line=self.lookahead.line,
83
+                    col=self.lookahead.col)
84 84
             self.consume()
85 85
 
86 86
     def consume(self):
... ...
@@ -115,7 +115,7 @@ class XDotAttrParser:
115 115
         return res
116 116
 
117 117
     def skip_space(self):
118
-        while self.pos < len(self.buf) and self.buf[self.pos : self.pos + 1].isspace():
118
+        while self.pos < len(self.buf) and self.buf[self.pos:self.pos+1].isspace():
119 119
             self.pos += 1
120 120
 
121 121
     def read_int(self):
... ...
@@ -439,7 +439,8 @@ class DotParser(Parser):
439 439
         else:
440 440
             port = None
441 441
             compass_pt = None
442
-        # XXX: we don't really care about port and compass point values when parsing xdot
442
+            # XXX: we don't really care about port and compass point
443
+            # values when parsing xdot
443 444
         return node_id
444 445
 
445 446
     def parse_id(self):
... ...
@@ -463,7 +464,7 @@ class XDotParser(DotParser):
463 464
     XDOTVERSION = '1.7'
464 465
 
465 466
     def __init__(self, xdotcode):
466
-        lexer = DotLexer(buf = xdotcode)
467
+        lexer = DotLexer(buf=xdotcode)
467 468
         DotParser.__init__(self, lexer)
468 469
 
469 470
         self.nodes = []
... ...
@@ -483,7 +484,8 @@ class XDotParser(DotParser):
483 484
                 pass
484 485
             else:
485 486
                 if float(xdotversion) > float(self.XDOTVERSION):
486
-                    sys.stderr.write('warning: xdot version %s, but supported is %s\n' % (xdotversion, self.XDOTVERSION))
487
+                    sys.stderr.write('warning: xdot version %s, but supported is %s\n' %
488
+                                     (xdotversion, self.XDOTVERSION))
487 489
 
488 490
             # Parse bounding box
489 491
             try:
... ...
@@ -500,7 +502,7 @@ class XDotParser(DotParser):
500 502
                 self.yscale = -1.0
501 503
                 # FIXME: scale from points to pixels
502 504
 
503
-                self.width  = max(xmax - xmin, 1)
505
+                self.width = max(xmax - xmin, 1)
504 506
                 self.height = max(ymax - ymin, 1)
505 507
 
506 508
                 self.top_graph = False
... ...
@@ -54,10 +54,11 @@ class Scanner:
54 54
         flags = re.DOTALL
55 55
         if self.ignorecase:
56 56
             flags |= re.IGNORECASE
57
-        self.tokens_re = re.compile(
58
-            b'|'.join([b'(' + regexp + b')' for type, regexp, test_lit in self.tokens]),
59
-             flags
60
-        )
57
+            self.tokens_re = re.compile(
58
+                b'|'.join([b'(' + regexp + b')'
59
+                           for type, regexp, test_lit in self.tokens]),
60
+                flags
61
+            )
61 62
 
62 63
     def next(self, buf, pos):
63 64
         if pos >= len(buf):
... ...
@@ -71,7 +72,7 @@ class Scanner:
71 72
                 type = self.literals.get(text, type)
72 73
             return type, text, pos
73 74
         else:
74
-            c = buf[pos : pos + 1]
75
+            c = buf[pos:pos+1]
75 76
             return self.symbols.get(c, None), c, pos + 1
76 77
 
77 78
 
... ...
@@ -81,11 +82,11 @@ class DotScanner(Scanner):
81 82
     tokens = [
82 83
         # whitespace and comments
83 84
         (SKIP,
84
-            br'[ \t\f\r\n\v]+|'
85
-            br'//[^\r\n]*|'
86
-            br'/\*.*?\*/|'
87
-            br'#[^\r\n]*',
88
-        False),
85
+         br'[ \t\f\r\n\v]+|'
86
+         br'//[^\r\n]*|'
87
+         br'/\*.*?\*/|'
88
+         br'#[^\r\n]*',
89
+         False),
89 90
 
90 91
         # Alphanumeric IDs
91 92
         (ID, br'[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*', True),
... ...
@@ -25,7 +25,7 @@ from gi.repository import GLib
25 25
 
26 26
 class Animation(object):
27 27
 
28
-    step = 0.03 # seconds
28
+    step = 0.03  # seconds
29 29
 
30 30
     def __init__(self, dot_widget):
31 31
         self.dot_widget = dot_widget
... ...
@@ -45,7 +45,7 @@ class Animation(object):
45 45
             if not self.tick():
46 46
                 self.stop()
47 47
                 return False
48
-        except e:
48
+        except AttributeError as e:
49 49
             self.stop()
50 50
             raise e
51 51
         return True
... ...
@@ -92,8 +92,8 @@ class MoveToAnimation(LinearAnimation):
92 92
     def animate(self, t):
93 93
         sx, sy = self.source_x, self.source_y
94 94
         tx, ty = self.target_x, self.target_y
95
-        self.dot_widget.x = tx * t + sx * (1-t)
96
-        self.dot_widget.y = ty * t + sy * (1-t)
95
+        self.dot_widget.x = tx * t + sx * (1 - t)
96
+        self.dot_widget.y = ty * t + sy * (1 - t)
97 97
         self.dot_widget.queue_draw()
98 98
 
99 99
 
... ...
@@ -118,6 +118,6 @@ class ZoomToAnimation(MoveToAnimation):
118 118
 
119 119
     def animate(self, t):
120 120
         a, b, c = self.source_zoom, self.extra_zoom, self.target_zoom
121
-        self.dot_widget.zoom_ratio = c*t + b*t*(1-t) + a*(1-t)
121
+        self.dot_widget.zoom_ratio = c*t + b*t*(1 - t) + a*(1 - t)
122 122
         self.dot_widget.zoom_to_fit_on_resize = False
123 123
         MoveToAnimation.animate(self, t)
... ...
@@ -83,7 +83,8 @@ class TextShape(Shape):
83 83
                 # 'TypeError: font_options must be a cairo.FontOptions or None'
84 84
                 pass
85 85
             except KeyError:
86
-                # cairo.FontOptions is not registered as a foreign struct in older PyGObject versions.
86
+                # cairo.FontOptions is not registered as a foreign
87
+                # struct in older PyGObject versions.
87 88
                 # https://git.gnome.org/browse/pygobject/commit/?id=b21f66d2a399b8c9a36a1758107b7bdff0ec8eaa
88 89
                 pass
89 90
 
... ...
@@ -121,7 +122,7 @@ class TextShape(Shape):
121 122
         else:
122 123
             PangoCairo.update_layout(cr, layout)
123 124
 
124
-        descent = 2 # XXX get descender from font metrics
125
+        descent = 2  # XXX get descender from font metrics
125 126
 
126 127
         width, height = layout.get_size()
127 128
         width = float(width)/Pango.SCALE
... ...
@@ -132,7 +133,7 @@ class TextShape(Shape):
132 133
         # scale it so that the text fits inside its box
133 134
         if width > self.w:
134 135
             f = self.w / width
135
-            width = self.w # equivalent to width *= f
136
+            width = self.w  # equivalent to width *= f
136 137
             height *= f
137 138
             descent *= f
138 139
         else:
... ...
@@ -157,7 +158,7 @@ class TextShape(Shape):
157 158
         PangoCairo.show_layout(cr, layout)
158 159
         cr.restore()
159 160
 
160
-        if 0: # DEBUG
161
+        if 0:  # DEBUG
161 162
             # show where dot thinks the text should appear
162 163
             cr.set_source_rgba(1, 0, 0, .9)
163 164
             if self.j == self.LEFT:
... ...
@@ -21,6 +21,7 @@ SUBSCRIPT = 16
21 21
 STRIKE_THROUGH = 32
22 22
 OVERLINE = 64
23 23
 
24
+
24 25
 class Pen:
25 26
     """Store pen attributes."""
26 27
 
... ...
@@ -47,10 +47,10 @@ from .elements import Graph
47 47
 class DotWidget(Gtk.DrawingArea):
48 48
     """GTK widget that draws dot graphs."""
49 49
 
50
-    #TODO GTK3: Second argument has to be of type Gdk.EventButton instead of object.
50
+    # TODO GTK3: Second argument has to be of type Gdk.EventButton instead of object.
51 51
     __gsignals__ = {
52
-        'clicked' : (GObject.SIGNAL_RUN_LAST, None, (str, object)),
53
-        'error' : (GObject.SIGNAL_RUN_LAST, None, (str,))
52
+        'clicked': (GObject.SIGNAL_RUN_LAST, None, (str, object)),
53
+        'error': (GObject.SIGNAL_RUN_LAST, None, (str,))
54 54
     }
55 55
 
56 56
     filter = 'dot'
... ...
@@ -64,7 +64,8 @@ class DotWidget(Gtk.DrawingArea):
64 64
         self.set_can_focus(True)
65 65
 
66 66
         self.connect("draw", self.on_draw)
67
-        self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK)
67
+        self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
68
+                        Gdk.EventMask.BUTTON_RELEASE_MASK)
68 69
         self.connect("button-press-event", self.on_area_button_press)
69 70
         self.connect("button-release-event", self.on_area_button_release)
70 71
         self.add_events(Gdk.EventMask.POINTER_MOTION_MASK |
... ...
@@ -317,10 +318,11 @@ class DotWidget(Gtk.DrawingArea):
317 318
         return False
318 319
 
319 320
     print_settings = None
321
+
320 322
     def on_print(self, action=None):
321 323
         print_op = Gtk.PrintOperation()
322 324
 
323
-        if self.print_settings != None:
325
+        if self.print_settings is not None:
324 326
             print_op.set_print_settings(self.print_settings)
325 327
 
326 328
         print_op.connect("begin_print", self.begin_print)
... ...
@@ -346,7 +348,7 @@ class DotWidget(Gtk.DrawingArea):
346 348
 
347 349
     def get_drag_action(self, event):
348 350
         state = event.state
349
-        if event.button in (1, 2): # left or middle button
351
+        if event.button in (1, 2):  # left or middle button
350 352
             modifiers = Gtk.accelerator_get_default_mod_mask()
351 353
             if state & modifiers == Gdk.ModifierType.CONTROL_MASK:
352 354
                 return ZoomAction
... ...
@@ -376,8 +378,8 @@ class DotWidget(Gtk.DrawingArea):
376 378
         # for gtk's clicked event instead?
377 379
         deltax = self.pressx - event.x
378 380
         deltay = self.pressy - event.y
379
-        return (time.time() < self.presstime + click_timeout
380
-                and math.hypot(deltax, deltay) < click_fuzz)
381
+        return (time.time() < self.presstime + click_timeout and
382
+                math.hypot(deltax, deltay) < click_fuzz)
381 383
 
382 384
     def on_click(self, element, event):
383 385
         """Override this method in subclass to process
... ...
@@ -513,7 +515,8 @@ class DotWindow(Gtk.Window):
513 515
         actiongroup.add_actions((
514 516
             ('Open', Gtk.STOCK_OPEN, None, None, None, self.on_open),
515 517
             ('Reload', Gtk.STOCK_REFRESH, None, None, None, self.on_reload),
516
-            ('Print', Gtk.STOCK_PRINT, None, None, "Prints the currently visible part of the graph", self.dotwidget.on_print),
518
+            ('Print', Gtk.STOCK_PRINT, None, None,
519
+             "Prints the currently visible part of the graph", self.dotwidget.on_print),
517 520
             ('ZoomIn', Gtk.STOCK_ZOOM_IN, None, None, None, self.dotwidget.on_zoom_in),
518 521
             ('ZoomOut', Gtk.STOCK_ZOOM_OUT, None, None, None, self.dotwidget.on_zoom_out),
519 522
             ('ZoomFit', Gtk.STOCK_ZOOM_FIT, None, None, None, self.dotwidget.on_zoom_fit),
... ...
@@ -521,7 +524,7 @@ class DotWindow(Gtk.Window):
521 524
         ))
522 525
 
523 526
         find_action = FindMenuToolAction("Find", None,
524
-                                          "Find a node by name", None)
527
+                                         "Find a node by name", None)
525 528
         actiongroup.add_action(find_action)
526 529
 
527 530
         # Add the actiongroup to the uimanager
... ...
@@ -547,8 +550,8 @@ class DotWindow(Gtk.Window):
547 550
         find_toolitem.add(self.textentry)
548 551
 
549 552
         self.textentry.set_activates_default(True)
550
-        self.textentry.connect ("activate", self.textentry_activate, self.textentry);
551
-        self.textentry.connect ("changed", self.textentry_changed, self.textentry);
553
+        self.textentry.connect("activate", self.textentry_activate, self.textentry);
554
+        self.textentry.connect("changed", self.textentry_changed, self.textentry);
552 555
 
553 556
         self.show_all()
554 557
 
... ...
@@ -576,7 +579,7 @@ class DotWindow(Gtk.Window):
576 579
         dot_widget = self.dotwidget
577 580
         if not entry_text:
578 581
             dot_widget.set_highlight(None, search=True)
579
-            return;
582
+            return
580 583
 
581 584
         found_items = self.find_text(entry_text)
582 585
         dot_widget.set_highlight(found_items, search=True)