pep8: fix empty line counts

top level: 2, else 1

I also advice to not put "pass" into empty classes, but rather a docstring instead of "pass".
This commit is contained in:
Thomas Waldmann 2014-11-18 18:29:32 +01:00
parent 5deb3f9e0f
commit 179eefae29
16 changed files with 74 additions and 15 deletions

@ -75,13 +75,11 @@ def main():
key = m.group(1)
translate_keys.add(key)
if args.show_all_keys:
for k in sorted(translate_keys):
print k
sys.exit()
locale_files = [f for f in files_in(dir, 'locale') if f.endswith('.json')]
for locale_file in locale_files:
with codecs.open(locale_file, 'r', encoding='utf-8') as f:

@ -32,6 +32,7 @@ def get_platform():
p = 'Tails'
return p
def get_onionshare_dir():
if get_platform() == 'Darwin':
onionshare_dir = os.path.dirname(__file__)
@ -39,6 +40,7 @@ def get_onionshare_dir():
onionshare_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
return onionshare_dir
def constant_time_compare(val1, val2):
_builtin_constant_time_compare = getattr(hmac, 'compare_digest', None)
if _builtin_constant_time_compare is not None:
@ -55,6 +57,7 @@ def constant_time_compare(val1, val2):
result |= x ^ y
return result == 0
def random_string(num_bytes, output_len=None):
b = os.urandom(num_bytes)
h = hashlib.sha256(b).digest()[:16]
@ -63,6 +66,7 @@ def random_string(num_bytes, output_len=None):
return s
return s[:output_len]
def human_readable_filesize(b):
thresh = 1024.0
if b < thresh:
@ -75,9 +79,11 @@ def human_readable_filesize(b):
u += 1
return '{0} {1}'.format(round(b, 1), units[u])
def is_root():
return os.geteuid() == 0
def dir_size(start_path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
@ -87,6 +93,7 @@ def dir_size(start_path):
total_size += os.path.getsize(fp)
return total_size
def get_tmp_dir():
if get_platform() == "Windows":
if 'Temp' in os.environ:
@ -97,6 +104,7 @@ def get_tmp_dir():
temp = '/tmp'
return temp
class ZipWriter(object):
def __init__(self, zip_filename=None):
if zip_filename:
@ -120,4 +128,3 @@ class ZipWriter(object):
def close(self):
self.z.close()

@ -25,8 +25,14 @@ from stem import SocketError
import strings, helpers, web
class NoTor(Exception): pass
class TailsError(Exception): pass
class NoTor(Exception):
pass
class TailsError(Exception):
pass
def hsdic2list(dic):
"""Convert what we get from get_conf_map to what we need for set_options"""
@ -37,6 +43,7 @@ def hsdic2list(dic):
] for pair in pairs
]
class OnionShare(object):
def __init__(self, debug=False, local_only=False, stay_open=False):
self.port = None
@ -56,7 +63,6 @@ class OnionShare(object):
# files and dirs to delete on shutdown
self.cleanup_filenames = []
def cleanup(self):
if self.controller:
# Get fresh hidden services (maybe changed since last time)
@ -191,6 +197,7 @@ class OnionShare(object):
return False
return True
def tails_root():
# if running in Tails and as root, do only the things that require root
if helpers.get_platform() == 'Tails' and helpers.is_root():
@ -217,6 +224,7 @@ def tails_root():
# close hole in firewall on shutdown
import signal
def handler(signum = None, frame = None):
subprocess.call(['/sbin/iptables', '-D', 'OUTPUT', '-o', 'lo', '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
sys.exit()
@ -227,6 +235,7 @@ def tails_root():
while True:
time.sleep(1)
def main():
strings.load_strings()
tails_root()

@ -65,6 +65,7 @@ PRINTABLE_PROXY_TYPES = {SOCKS4: "SOCKS4", SOCKS5: "SOCKS5", HTTP: "HTTP"}
_orgsocket = _orig_socket = socket.socket
class ProxyError(IOError):
"""
socket_err contains original socket.error exception.
@ -79,12 +80,30 @@ class ProxyError(IOError):
def __str__(self):
return self.msg
class GeneralProxyError(ProxyError): pass
class ProxyConnectionError(ProxyError): pass
class SOCKS5AuthError(ProxyError): pass
class SOCKS5Error(ProxyError): pass
class SOCKS4Error(ProxyError): pass
class HTTPError(ProxyError): pass
class GeneralProxyError(ProxyError):
pass
class ProxyConnectionError(ProxyError):
pass
class SOCKS5AuthError(ProxyError):
pass
class SOCKS5Error(ProxyError):
pass
class SOCKS4Error(ProxyError):
pass
class HTTPError(ProxyError):
pass
SOCKS4_ERRORS = { 0x5B: "Request rejected or failed",
0x5C: "Request rejected because SOCKS server cannot connect to identd on the client",
@ -106,6 +125,7 @@ DEFAULT_PORTS = { SOCKS4: 1080,
HTTP: 8080
}
def set_default_proxy(proxy_type=None, addr=None, port=None, rdns=True, username=None, password=None):
"""
set_default_proxy(proxy_type, addr[, port[, rdns[, username, password]]])
@ -119,6 +139,7 @@ def set_default_proxy(proxy_type=None, addr=None, port=None, rdns=True, username
setdefaultproxy = set_default_proxy
def get_default_proxy():
"""
Returns the default proxy, set by set_default_proxy.
@ -127,6 +148,7 @@ def get_default_proxy():
getdefaultproxy = get_default_proxy
def wrap_module(module):
"""
Attempts to replace a module's socket library with a SOCKS socket. Must set
@ -141,7 +163,8 @@ def wrap_module(module):
wrapmodule = wrap_module
def create_connection(dest_pair, proxy_type=None, proxy_addr=None,
def create_connection(dest_pair, proxy_type=None, proxy_addr=None,
proxy_port=None, proxy_username=None,
proxy_password=None, timeout=None):
"""create_connection(dest_pair, **proxy_args) -> socket object
@ -161,6 +184,7 @@ def create_connection(dest_pair, proxy_type=None, proxy_addr=None,
sock.connect(dest_pair)
return sock
class socksocket(socket.socket):
"""socksocket([family[, type[, proto]]]) -> socket object
@ -446,7 +470,6 @@ class socksocket(socket.socket):
self.proxy_sockname = (b"0.0.0.0", 0)
self.proxy_peername = addr, dest_port
def connect(self, dest_pair):
"""
Connects to the specified destination through a proxy.
@ -465,7 +488,6 @@ class socksocket(socket.socket):
or not isinstance(dest_port, int)):
raise GeneralProxyError("Invalid destination-connection (host, port) pair")
if proxy_type is None:
# Treat like regular socket object
_orig_socket.connect(self, (dest_addr, dest_port))

@ -22,6 +22,7 @@ import helpers
strings = {}
def load_strings(default="en"):
global strings
@ -49,6 +50,7 @@ def load_strings(default="en"):
if key in translated[lang]:
strings[key] = translated[lang][key]
def translated(k, gui=False):
if gui:
return strings[k].encode("utf-8").decode('utf-8', 'replace')

@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os, sys, inspect, platform
def get_onionshare_gui_dir():
if platform.system() == 'Darwin':
onionshare_gui_dir = os.path.dirname(__file__)
@ -28,6 +29,7 @@ def get_onionshare_gui_dir():
onionshare_gui_dir = get_onionshare_gui_dir()
def get_image_path(filename):
if platform.system() == 'Linux':
prefix = os.path.join(sys.prefix, 'share/onionshare/images')

@ -22,6 +22,7 @@ from PyQt4 import QtCore, QtGui
import common
from onionshare import strings, helpers
class Downloads(QtGui.QVBoxLayout):
def __init__(self):
super(Downloads, self).__init__()

@ -23,6 +23,7 @@ from PyQt4 import QtCore, QtGui
import common
from onionshare import strings, helpers
class FileList(QtGui.QListWidget):
files_dropped = QtCore.pyqtSignal()
files_updated = QtCore.pyqtSignal()
@ -137,6 +138,7 @@ class FileList(QtGui.QListWidget):
u += 1
return '{0} {1}'.format(round(b, 1), units[u])
class FileSelection(QtGui.QVBoxLayout):
def __init__(self):
super(FileSelection, self).__init__()

@ -35,6 +35,7 @@ from server_status import ServerStatus
from downloads import Downloads
from options import Options
class Application(QtGui.QApplication):
def __init__(self):
platform = helpers.get_platform()
@ -42,6 +43,7 @@ class Application(QtGui.QApplication):
self.setAttribute(QtCore.Qt.AA_X11InitThreads, True)
QtGui.QApplication.__init__(self, sys.argv)
class OnionShareGui(QtGui.QWidget):
start_server_finished = QtCore.pyqtSignal()
stop_server_finished = QtCore.pyqtSignal()
@ -206,6 +208,7 @@ class OnionShareGui(QtGui.QWidget):
def clear_message(self):
self.status_bar.clearMessage()
def alert(msg, icon=QtGui.QMessageBox.NoIcon):
dialog = QtGui.QMessageBox()
dialog.setWindowTitle("OnionShare")
@ -214,6 +217,7 @@ def alert(msg, icon=QtGui.QMessageBox.NoIcon):
dialog.setIcon(icon)
dialog.exec_()
def main():
strings.load_strings()

@ -22,6 +22,7 @@ from PyQt4 import QtCore, QtGui
import common
from onionshare import strings, helpers
class Options(QtGui.QHBoxLayout):
def __init__(self, web):
super(Options, self).__init__()

@ -23,6 +23,7 @@ from PyQt4 import QtCore, QtGui
import common
from onionshare import strings, helpers
class ServerStatus(QtGui.QVBoxLayout):
server_started = QtCore.pyqtSignal()
server_stopped = QtCore.pyqtSignal()

@ -27,6 +27,7 @@ try:
except ImportError:
from distutils.core import setup
def file_list(path):
files = []
for filename in os.listdir(path):

@ -21,11 +21,13 @@ from nose import with_setup
import test_helpers
def test_get_platform_on_tails():
"""get_platform() returns 'Tails' when hostname is 'amnesia'"""
helpers.platform.uname = lambda: ('Linux', 'amnesia', '3.14-1-amd64', '#1 SMP Debian 3.14.4-1 (2014-05-13)', 'x86_64', '')
assert helpers.get_platform() == 'Tails'
def test_get_platform_returns_platform_system():
"""get_platform() returns platform.system() when ONIONSHARE_PLATFORM is not defined"""
helpers.platform.system = lambda: 'Sega Saturn'

@ -21,16 +21,19 @@ import locale
from onionshare import strings
from nose import with_setup
def test_starts_with_empty_strings():
"""creates an empty strings dict by default"""
assert strings.strings == {}
def test_load_strings_defaults_to_english():
"""load_strings() loads English by default"""
locale.getdefaultlocale = lambda: ('en_US', 'UTF-8')
strings.load_strings()
assert strings._('wait_for_hs') == "Waiting for HS to be ready:"
def test_load_strings_loads_other_languages():
"""load_strings() loads other languages in different locales"""
locale.getdefaultlocale = lambda: ('fr_FR', 'UTF-8')

@ -20,12 +20,14 @@ import socket
from onionshare import OnionShare
from nose import with_setup
def test_choose_port_returns_a_port_number():
"""choose_port() returns a port number"""
app = OnionShare()
app.choose_port()
assert 1024 <= app.port <= 65535
def test_choose_port_returns_an_open_port():
"""choose_port() returns an open port"""
app = OnionShare()

@ -19,10 +19,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from onionshare import web
from nose import with_setup
def test_generate_slug_length():
"""generates a 26-character slug"""
assert len(web.slug) == 26
def test_generate_slug_characters():
"""generates a base32-encoded slug"""