Skip to content
This repository was archived by the owner on Oct 25, 2018. It is now read-only.
Open

Py3 #45

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions create_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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!")
19 changes: 10 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion simple-debug.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
import simple
simple.app.debug = True
simple.app.run()
11 changes: 6 additions & 5 deletions simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
Expand Down Expand Up @@ -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})
Expand Down