diff --git a/create_config.py b/create_config.py index cdee344..7b92962 100644 --- a/create_config.py +++ b/create_config.py @@ -2,25 +2,26 @@ from werkzeug.security import generate_password_hash from os import urandom from base64 import b32encode +from six import string_types import sys import getpass if "--help" in sys.argv: - print "create_config.py:" - print "Options:" - print " * --fresh" - print " Over-write existing config if it exists" - print " * --update" - print " Update existing config (Default if the config exists)" - print " * --changepass" - print " Change the admin password" + print("create_config.py:") + print("Options:") + print(" * --fresh") + print(" Over-write existing config if it exists") + print(" * --update") + print(" Update existing config (Default if the config exists)") + print(" * --changepass") + print(" Change the admin password") sys.exit(1) try: import settings if not "--fresh" in sys.argv and not "--changepass" in sys.argv: sys.argv.append("--update") - print "Updating. Use --fresh to over-write existing config" + print("Updating. Use --fresh to over-write existing config") except (ImportError, SyntaxError): settings = None @@ -36,11 +37,23 @@ def input_with_default(*args, **kwargs): try: return name, _type(res) except ValueError: - print "Error: Value %s is not the correct type. Please re-enter" % res + print("Error: Value %s is not the correct type. Please re-enter" % (res)) return input_with_default(*args, _type=_type, **kwargs) +def _default_input_func(*args, **kwargs): + """ + Check wether we can use `input()` from python3. + If not, fallback to `raw_input()`. + """ + func = None + try: + func = input(*args, **kwargs) + except NameError: + func = raw_input(*args, **kwargs) -def _input_with_default(name, prompt, default, func=lambda v: v, _input_func=raw_input): + return func + +def _input_with_default(name, prompt, default, func=lambda v: v, _input_func=_default_input_func): """ Small wrapper around raw_input for prompting and defaulting """ if ("--update" in sys.argv or ("--changepass" in sys.argv and name != "ADMIN_PASSWORD")) and settings is not None: # We are updating. If the name already exists in the settings object @@ -66,7 +79,7 @@ def input_password(*args, **kwargs): return name, generate_password_hash(response) -print "%s a Simple config file. Please answer some questions:" % ("Updating" if "--update" in sys.argv else "Generating") +print("%s a Simple config file. Please answer some questions:" % ("Updating" if "--update" in sys.argv else "Generating")) SETTINGS = ( input_with_default("POSTS_PER_PAGE", "Posts per page", 5, _type=int), input_with_default("POST_CONTENT_ON_HOMEPAGE", "Show the post content on the homepage", @@ -91,9 +104,9 @@ def input_password(*args, **kwargs): with open("settings.py", "w") as fd: fd.write("# -*- coding: utf-8 -*-\n") for name, value in SETTINGS: - if isinstance(value, basestring): + if isinstance(value, string_types): value = "'%s'" % value.replace("'", "\\'") fd.write("%s = %s\n" % (name, value)) fd.flush() -print "Created!" +print("Created!") diff --git a/requirements.txt b/requirements.txt index 3de8654..ae721ca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,10 @@ -Flask==0.10.1 -Flask-SQLAlchemy==1.0 -Jinja2==2.7.1 -Markdown==2.3.1 -MarkupSafe==0.18 -Pygments==1.6 -SQLAlchemy==0.8.2 -Werkzeug==0.9.4 -itsdangerous==0.23 +Flask==0.10.1 +Flask-SQLAlchemy==1.0 +Jinja2==2.7.1 +Markdown==2.3.1 +MarkupSafe==0.18 +Pygments==1.6 +SQLAlchemy==0.8.2 +Werkzeug==0.9.4 +itsdangerous==0.23 +six==1.7.3 diff --git a/simple-debug.py b/simple-debug.py index 97fb7f6..7cce1cd 100644 --- a/simple-debug.py +++ b/simple-debug.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python import simple simple.app.debug = True simple.app.run() diff --git a/simple.py b/simple.py index cf2946e..63b3010 100644 --- a/simple.py +++ b/simple.py @@ -10,6 +10,7 @@ from os import urandom from base64 import b32encode from email import utils +from six import u as unicode # web stuff and markdown imports import markdown from flask.ext.sqlalchemy import SQLAlchemy @@ -42,9 +43,9 @@ cache_directory = os.path.dirname(__file__) try: cache = FileSystemCache(os.path.join(cache_directory, "cache")) -except Exception, e: - print "Could not create cache folder, caching will be disabled." - print "Error: %s" % e +except Exception as e: + print("Could not create cache folder, caching will be disabled.") + print("Error: %s" % (e)) cache = NullCache() _punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+') @@ -306,11 +307,11 @@ def allowed_file(filename): def upload_file(): if request.method == 'POST': file_upload = request.files['file'] - if file and allowed_file(file_upload.filename): + if file_upload and allowed_file(file_upload.filename): filename = secure_filename(file_upload.filename) key = b32encode(urandom(5)) filename, extension = os.path.splitext(filename) - filename = filename + '_' + key + extension + filename = filename + '_' + str(key) + extension file_upload.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) url = url_for('uploaded_file', filename=filename) return json.dumps({'status': 'ok', 'url': url, 'name': filename})