[tor-commits] [stem/master] Move imports to the top of files

atagar at torproject.org atagar at torproject.org
Thu Dec 31 17:05:05 UTC 2015


commit e9060e62fa0317f229ec3bfdd3d821c7eb256a84
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Dec 31 08:45:15 2015 -0800

    Move imports to the top of files
    
    Stylistic complaint E402 in the new version of pep8...
    
      line 870  - E402 module level import not at top of file | import stem.descriptor.extrainfo_descriptor
    
    Usually this is simply because I put __all__ blocks above them which is trivial
    to change. In a few cases though I reorder imports for a good reason. As such,
    adding the ability to ignore more targeted pep8 issues.
---
 stem/__init__.py                 |   22 +++++++++++-----------
 stem/descriptor/__init__.py      |   28 ++++++++++++++--------------
 stem/interpreter/__init__.py     |   14 +++++++-------
 stem/response/__init__.py        |   10 +++++-----
 stem/util/__init__.py            |    4 ++--
 stem/util/test_tools.py          |   28 ++++++++++++++++++++++++++--
 test/settings.cfg                |    8 ++++++++
 test/unit/__init__.py            |    7 +++----
 test/unit/descriptor/__init__.py |    4 ++--
 test/unit/util/__init__.py       |   12 ++++++------
 10 files changed, 84 insertions(+), 53 deletions(-)

diff --git a/stem/__init__.py b/stem/__init__.py
index 273578a..e951ec2 100644
--- a/stem/__init__.py
+++ b/stem/__init__.py
@@ -464,6 +464,17 @@ Library for working with the tor process.
   ================= ===========
 """
 
+import stem.prereq
+
+if stem.prereq.is_python_3():
+  str_type = str
+  int_type = int
+else:
+  str_type = unicode
+  int_type = long
+
+import stem.util.enum
+
 __version__ = '1.4.1-dev'
 __author__ = 'Damian Johnson'
 __contact__ = 'atagar at torproject.org'
@@ -517,17 +528,6 @@ __all__ = [
   'TimeoutSetType',
 ]
 
-import stem.prereq
-
-if stem.prereq.is_python_3():
-  str_type = str
-  int_type = int
-else:
-  str_type = unicode
-  int_type = long
-
-import stem.util.enum
-
 # Constant to indicate an undefined argument default. Usually we'd use None for
 # this, but users will commonly provide None as the argument so need something
 # else fairly unique...
diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index af96bde..e46f30d 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -36,20 +36,6 @@ Package for parsing and processing descriptor data.
   =================== ===========
 """
 
-__all__ = [
-  'export',
-  'reader',
-  'remote',
-  'extrainfo_descriptor',
-  'server_descriptor',
-  'microdescriptor',
-  'networkstatus',
-  'router_status_entry',
-  'tordnsel',
-  'parse_file',
-  'Descriptor',
-]
-
 import base64
 import codecs
 import copy
@@ -71,6 +57,20 @@ try:
 except ImportError:
   from stem.util.ordereddict import OrderedDict
 
+__all__ = [
+  'export',
+  'reader',
+  'remote',
+  'extrainfo_descriptor',
+  'server_descriptor',
+  'microdescriptor',
+  'networkstatus',
+  'router_status_entry',
+  'tordnsel',
+  'parse_file',
+  'Descriptor',
+]
+
 KEYWORD_CHAR = 'a-zA-Z0-9-'
 WHITESPACE = ' \t'
 KEYWORD_LINE = re.compile('^([%s]+)(?:[%s]+(.*))?$' % (KEYWORD_CHAR, WHITESPACE))
diff --git a/stem/interpreter/__init__.py b/stem/interpreter/__init__.py
index f8dce7e..195e32a 100644
--- a/stem/interpreter/__init__.py
+++ b/stem/interpreter/__init__.py
@@ -6,13 +6,6 @@ Interactive interpreter for interacting with Tor directly. This adds usability
 features such as tab completion, history, and IRC-style functions (like /help).
 """
 
-__all__ = [
-  'arguments',
-  'autocomplete',
-  'commands',
-  'help',
-]
-
 import os
 import sys
 
@@ -26,6 +19,13 @@ import stem.util.term
 
 from stem.util.term import Attr, Color, format
 
+__all__ = [
+  'arguments',
+  'autocomplete',
+  'commands',
+  'help',
+]
+
 PROMPT = format('>>> ', Color.GREEN, Attr.BOLD, Attr.READLINE_ESCAPE)
 
 STANDARD_OUTPUT = (Color.BLUE, )
diff --git a/stem/response/__init__.py b/stem/response/__init__.py
index 0612007..1c391c0 100644
--- a/stem/response/__init__.py
+++ b/stem/response/__init__.py
@@ -30,6 +30,11 @@ Parses replies from the control socket.
     +- pop_mapping - removes and returns the next entry as a KEY=VALUE mapping
 """
 
+import re
+import threading
+
+import stem.socket
+
 __all__ = [
   'add_onion',
   'events',
@@ -43,16 +48,11 @@ __all__ = [
   'SingleLineResponse',
 ]
 
-import re
-import threading
-
 try:
   from StringIO import StringIO
 except ImportError:
   from io import StringIO
 
-import stem.socket
-
 KEY_ARG = re.compile('^(\S+)=')
 
 # Escape sequences from the 'esc_for_log' function of tor's 'common/util.c'.
diff --git a/stem/util/__init__.py b/stem/util/__init__.py
index ecdf288..2ee9b9b 100644
--- a/stem/util/__init__.py
+++ b/stem/util/__init__.py
@@ -5,6 +5,8 @@
 Utility functions used by the stem library.
 """
 
+import datetime
+
 __all__ = [
   'conf',
   'connection',
@@ -20,8 +22,6 @@ __all__ = [
   'datetime_to_unix',
 ]
 
-import datetime
-
 
 def datetime_to_unix(timestamp):
   """
diff --git a/stem/util/test_tools.py b/stem/util/test_tools.py
index bc54f94..533f8d3 100644
--- a/stem/util/test_tools.py
+++ b/stem/util/test_tools.py
@@ -139,6 +139,8 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa
     pep8.ignore E121
     pep8.ignore E501
 
+    pep8.ignore run_tests.py => E402: import stem.util.enum
+
   ... you can then run tests with...
 
   ::
@@ -177,6 +179,26 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa
 
   issues = {}
 
+  ignore_rules = []
+  ignore_for_file = []
+
+  for rule in CONFIG['pep8.ignore']:
+    if '=>' in rule:
+      path, rule_entry = rule.split('=>', 1)
+
+      if ':' in rule_entry:
+        rule, code = rule_entry.split(':', 1)
+        ignore_for_file.append((path.strip(), rule.strip(), code.strip()))
+    else:
+      ignore_rules.append(rule)
+
+  def is_ignored(path, rule, code):
+    for ignored_path, ignored_rule, ignored_code in ignore_for_file:
+      if path.endswith(ignored_path) and ignored_rule == rule and ignored_code == code.strip():
+        return True
+
+    return False
+
   if is_pep8_available():
     import pep8
 
@@ -189,9 +211,11 @@ def stylistic_issues(paths, check_newlines = False, check_exception_keyword = Fa
 
         if code:
           line = linecache.getline(self.filename, line_number)
-          issues.setdefault(self.filename, []).append(Issue(line_number, text, line))
 
-    style_checker = pep8.StyleGuide(ignore = CONFIG['pep8.ignore'], reporter = StyleReport)
+          if not is_ignored(self.filename, code, line):
+            issues.setdefault(self.filename, []).append(Issue(line_number, text, line))
+
+    style_checker = pep8.StyleGuide(ignore = ignore_rules, reporter = StyleReport)
     style_checker.check_files(list(_python_files(paths)))
 
   if check_newlines or check_exception_keyword:
diff --git a/test/settings.cfg b/test/settings.cfg
index 7443544..fd199e1 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -128,6 +128,14 @@ pep8.ignore E251
 pep8.ignore E127
 pep8.ignore E131
 
+pep8.ignore stem/__init__.py => E402: import stem.util.enum
+pep8.ignore stem/descriptor/__init__.py => E402: import stem.descriptor.server_descriptor
+pep8.ignore stem/descriptor/__init__.py => E402: import stem.descriptor.extrainfo_descriptor
+pep8.ignore stem/descriptor/__init__.py => E402: import stem.descriptor.networkstatus
+pep8.ignore stem/descriptor/__init__.py => E402: import stem.descriptor.microdescriptor
+pep8.ignore stem/descriptor/__init__.py => E402: import stem.descriptor.tordnsel
+pep8.ignore stem/descriptor/__init__.py => E402: import stem.descriptor.hidden_service_descriptor
+
 # False positives from pyflakes. These are mappings between the path and the
 # issue.
 
diff --git a/test/unit/__init__.py b/test/unit/__init__.py
index d76ecfe..94cd059 100644
--- a/test/unit/__init__.py
+++ b/test/unit/__init__.py
@@ -2,6 +2,9 @@
 Unit tests for the stem library.
 """
 
+import os
+import test.util
+
 __all__ = [
   'connection',
   'control',
@@ -13,10 +16,6 @@ __all__ = [
 ]
 
 
-import os
-import test.util
-
-
 def exec_documentation_example(filename):
   path = os.path.join(test.util.STEM_BASE, 'docs', '_static', 'example', filename)
 
diff --git a/test/unit/descriptor/__init__.py b/test/unit/descriptor/__init__.py
index b182f42..b7696ac 100644
--- a/test/unit/descriptor/__init__.py
+++ b/test/unit/descriptor/__init__.py
@@ -2,6 +2,8 @@
 Unit tests for stem.descriptor.
 """
 
+import os
+
 __all__ = [
   'export',
   'extrainfo_descriptor',
@@ -12,8 +14,6 @@ __all__ = [
   'server_descriptor',
 ]
 
-import os
-
 DESCRIPTOR_TEST_DATA = os.path.join(os.path.dirname(__file__), 'data')
 
 
diff --git a/test/unit/util/__init__.py b/test/unit/util/__init__.py
index e755625..866ec4b 100644
--- a/test/unit/util/__init__.py
+++ b/test/unit/util/__init__.py
@@ -2,6 +2,12 @@
 Unit tests for stem.util.* contents.
 """
 
+import datetime
+import time
+import unittest
+
+from stem.util import datetime_to_unix
+
 __all__ = [
   'conf',
   'connection',
@@ -12,12 +18,6 @@ __all__ = [
   'tor_tools',
 ]
 
-import datetime
-import time
-import unittest
-
-from stem.util import datetime_to_unix
-
 
 class TestBaseUtil(unittest.TestCase):
   def test_datetime_to_unix(self):



More information about the tor-commits mailing list