pep8: fix indentation to be a multiple of four

note: i used pycharm "reformat file", so there are other reformattings also
This commit is contained in:
Thomas Waldmann 2014-11-18 18:03:34 +01:00
parent d51f762ddd
commit 5deb3f9e0f
2 changed files with 38 additions and 17 deletions

View File

@ -28,11 +28,13 @@ app = Flask(__name__)
file_info = [] file_info = []
zip_filename = None zip_filename = None
zip_filesize = None zip_filesize = None
def set_file_info(filenames): def set_file_info(filenames):
global file_info, zip_filename, zip_filesize global file_info, zip_filename, zip_filesize
# build file info list # build file info list
file_info = {'files':[], 'dirs':[]} file_info = {'files': [], 'dirs': []}
for filename in filenames: for filename in filenames:
info = { info = {
'filename': filename, 'filename': filename,
@ -59,6 +61,7 @@ def set_file_info(filenames):
zip_filename = z.zip_filename zip_filename = z.zip_filename
zip_filesize = os.path.getsize(zip_filename) zip_filesize = os.path.getsize(zip_filename)
REQUEST_LOAD = 0 REQUEST_LOAD = 0
REQUEST_DOWNLOAD = 1 REQUEST_DOWNLOAD = 1
REQUEST_PROGRESS = 2 REQUEST_PROGRESS = 2
@ -66,24 +69,31 @@ REQUEST_OTHER = 3
REQUEST_CANCELED = 4 REQUEST_CANCELED = 4
q = Queue.Queue() q = Queue.Queue()
def add_request(type, path, data=None): def add_request(type, path, data=None):
global q global q
q.put({ q.put({
'type': type, 'type': type,
'path': path, 'path': path,
'data': data 'data': data
}) })
slug = helpers.random_string(16) slug = helpers.random_string(16)
download_count = 0 download_count = 0
stay_open = False stay_open = False
def set_stay_open(new_stay_open): def set_stay_open(new_stay_open):
global stay_open global stay_open
stay_open = new_stay_open stay_open = new_stay_open
def get_stay_open(): def get_stay_open():
return stay_open return stay_open
def debug_mode(): def debug_mode():
import logging import logging
@ -96,6 +106,7 @@ def debug_mode():
log_handler.setLevel(logging.WARNING) log_handler.setLevel(logging.WARNING)
app.logger.addHandler(log_handler) app.logger.addHandler(log_handler)
@app.route("/<slug_candidate>") @app.route("/<slug_candidate>")
def index(slug_candidate): def index(slug_candidate):
if not helpers.constant_time_compare(slug.encode('ascii'), slug_candidate.encode('ascii')): if not helpers.constant_time_compare(slug.encode('ascii'), slug_candidate.encode('ascii')):
@ -112,6 +123,7 @@ def index(slug_candidate):
strings=strings.strings strings=strings.strings
) )
@app.route("/<slug_candidate>/download") @app.route("/<slug_candidate>/download")
def download(slug_candidate): def download(slug_candidate):
global download_count global download_count
@ -128,13 +140,13 @@ def download(slug_candidate):
path = request.path path = request.path
# tell GUI the download started # 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) dirname = os.path.dirname(zip_filename)
basename = os.path.basename(zip_filename) basename = os.path.basename(zip_filename)
def generate(): def generate():
chunk_size = 102400 # 100kb chunk_size = 102400 # 100kb
fp = open(zip_filename, 'rb') fp = open(zip_filename, 'rb')
done = False done = False
@ -150,16 +162,17 @@ def download(slug_candidate):
# tell GUI the progress # tell GUI the progress
downloaded_bytes = fp.tell() downloaded_bytes = fp.tell()
percent = round((1.0 * downloaded_bytes / zip_filesize) * 100, 2) 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() 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: except:
# looks like the download was canceled # looks like the download was canceled
done = True done = True
canceled = True canceled = True
# tell the GUI the download has canceled # 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() fp.close()
sys.stdout.write("\n") sys.stdout.write("\n")
@ -181,6 +194,7 @@ def download(slug_candidate):
r.headers.add('Content-Type', content_type) r.headers.add('Content-Type', content_type)
return r return r
@app.errorhandler(404) @app.errorhandler(404)
def page_not_found(e): def page_not_found(e):
add_request(REQUEST_OTHER, request.path) 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 # 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) shutdown_slug = helpers.random_string(16)
@app.route("/<shutdown_slug_candidate>/shutdown") @app.route("/<shutdown_slug_candidate>/shutdown")
def shutdown(shutdown_slug_candidate): def shutdown(shutdown_slug_candidate):
if not helpers.constant_time_compare(shutdown_slug.encode('ascii'), shutdown_slug_candidate.encode('ascii')): 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 "" return ""
def start(port, stay_open=False): def start(port, stay_open=False):
set_stay_open(stay_open) set_stay_open(stay_open)
app.run(port=port, threaded=True) app.run(port=port, threaded=True)
def stop(port): def stop(port):
# to stop flask, load http://127.0.0.1:<port>/<shutdown_slug>/shutdown # to stop flask, load http://127.0.0.1:<port>/<shutdown_slug>/shutdown
if helpers.get_platform() == 'Tails': if helpers.get_platform() == 'Tails':
# in Tails everything is proxies over Tor, so we need to get lower level # in Tails everything is proxies over Tor, so we need to get lower level
# to connect not over the proxy # to connect not over the proxy
import socket import socket
s = socket.socket() s = socket.socket()
s.connect(('127.0.0.1', port)) s.connect(('127.0.0.1', port))
s.sendall('GET /{0}/shutdown HTTP/1.1\r\n\r\n'.format(shutdown_slug)) s.sendall('GET /{0}/shutdown HTTP/1.1\r\n\r\n'.format(shutdown_slug))

View File

@ -18,22 +18,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import tempfile import tempfile
class MockSubprocess(): class MockSubprocess():
def __init__(self): def __init__(self):
self.last_call = None self.last_call = None
def call(self, args): def call(self, args):
self.last_call = 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): def write_tempfile(text):
tempdir = tempfile.mkdtemp() tempdir = tempfile.mkdtemp()
path = tempdir + "/test-file.txt" path = tempdir + "/test-file.txt"
with open(path, "w") as f: with open(path, "w") as f:
f.write(text) f.write(text)
f.close() f.close()
return path return path