From 5deb3f9e0f5b7cd81602f90f859c2ec0e4e41eec Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 18 Nov 2014 18:03:34 +0100 Subject: [PATCH] pep8: fix indentation to be a multiple of four note: i used pycharm "reformat file", so there are other reformattings also --- onionshare/web.py | 37 ++++++++++++++++++++++++++++--------- test/test_helpers.py | 18 ++++++++++-------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/onionshare/web.py b/onionshare/web.py index d9e6b866..07d1fd9c 100644 --- a/onionshare/web.py +++ b/onionshare/web.py @@ -28,11 +28,13 @@ app = Flask(__name__) file_info = [] zip_filename = None zip_filesize = None + + def set_file_info(filenames): global file_info, zip_filename, zip_filesize # build file info list - file_info = {'files':[], 'dirs':[]} + file_info = {'files': [], 'dirs': []} for filename in filenames: info = { 'filename': filename, @@ -59,6 +61,7 @@ def set_file_info(filenames): zip_filename = z.zip_filename zip_filesize = os.path.getsize(zip_filename) + REQUEST_LOAD = 0 REQUEST_DOWNLOAD = 1 REQUEST_PROGRESS = 2 @@ -66,24 +69,31 @@ REQUEST_OTHER = 3 REQUEST_CANCELED = 4 q = Queue.Queue() + def add_request(type, path, data=None): global q q.put({ - 'type': type, - 'path': path, - 'data': data + 'type': type, + 'path': path, + 'data': data }) + slug = helpers.random_string(16) download_count = 0 stay_open = False + + def set_stay_open(new_stay_open): global stay_open stay_open = new_stay_open + + def get_stay_open(): return stay_open + def debug_mode(): import logging @@ -96,6 +106,7 @@ def debug_mode(): log_handler.setLevel(logging.WARNING) app.logger.addHandler(log_handler) + @app.route("/") def index(slug_candidate): if not helpers.constant_time_compare(slug.encode('ascii'), slug_candidate.encode('ascii')): @@ -112,6 +123,7 @@ def index(slug_candidate): strings=strings.strings ) + @app.route("//download") def download(slug_candidate): global download_count @@ -128,13 +140,13 @@ def download(slug_candidate): path = request.path # tell GUI the download started - add_request(REQUEST_DOWNLOAD, path, { 'id':download_id }) + add_request(REQUEST_DOWNLOAD, path, {'id': download_id}) dirname = os.path.dirname(zip_filename) basename = os.path.basename(zip_filename) def generate(): - chunk_size = 102400 # 100kb + chunk_size = 102400 # 100kb fp = open(zip_filename, 'rb') done = False @@ -150,16 +162,17 @@ def download(slug_candidate): # tell GUI the progress downloaded_bytes = fp.tell() percent = round((1.0 * downloaded_bytes / zip_filesize) * 100, 2) - sys.stdout.write("\r{0}, {1}% ".format(helpers.human_readable_filesize(downloaded_bytes), percent)) + sys.stdout.write( + "\r{0}, {1}% ".format(helpers.human_readable_filesize(downloaded_bytes), percent)) sys.stdout.flush() - add_request(REQUEST_PROGRESS, path, { 'id':download_id, 'bytes':downloaded_bytes }) + add_request(REQUEST_PROGRESS, path, {'id': download_id, 'bytes': downloaded_bytes}) except: # looks like the download was canceled done = True canceled = True # tell the GUI the download has canceled - add_request(REQUEST_CANCELED, path, { 'id':download_id }) + add_request(REQUEST_CANCELED, path, {'id': download_id}) fp.close() sys.stdout.write("\n") @@ -181,6 +194,7 @@ def download(slug_candidate): r.headers.add('Content-Type', content_type) return r + @app.errorhandler(404) def page_not_found(e): add_request(REQUEST_OTHER, request.path) @@ -188,6 +202,8 @@ def page_not_found(e): # shutting down the server only works within the context of flask, so the easiest way to do it is over http shutdown_slug = helpers.random_string(16) + + @app.route("//shutdown") def shutdown(shutdown_slug_candidate): if not helpers.constant_time_compare(shutdown_slug.encode('ascii'), shutdown_slug_candidate.encode('ascii')): @@ -201,16 +217,19 @@ def shutdown(shutdown_slug_candidate): return "" + def start(port, stay_open=False): set_stay_open(stay_open) app.run(port=port, threaded=True) + def stop(port): # to stop flask, load http://127.0.0.1://shutdown if helpers.get_platform() == 'Tails': # in Tails everything is proxies over Tor, so we need to get lower level # to connect not over the proxy import socket + s = socket.socket() s.connect(('127.0.0.1', port)) s.sendall('GET /{0}/shutdown HTTP/1.1\r\n\r\n'.format(shutdown_slug)) diff --git a/test/test_helpers.py b/test/test_helpers.py index c4705142..cab2dd34 100644 --- a/test/test_helpers.py +++ b/test/test_helpers.py @@ -18,22 +18,24 @@ along with this program. If not, see . """ import tempfile + class MockSubprocess(): - def __init__(self): - self.last_call = None + def __init__(self): + self.last_call = None - def call(self, args): - self.last_call = args + def call(self, args): + self.last_call = args + + def last_call_args(self): + return self.last_call - def last_call_args(self): - return self.last_call def write_tempfile(text): tempdir = tempfile.mkdtemp() path = tempdir + "/test-file.txt" with open(path, "w") as f: - f.write(text) - f.close() + f.write(text) + f.close() return path