Support Python 3 for running the app

This commit is contained in:
Noah 2015-07-10 00:27:13 -07:00
parent 9b890a1cf8
commit e38ccc7190
24 changed files with 44 additions and 40 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Flask app for Rophako.""" """Flask app for Rophako."""
@ -35,8 +35,14 @@ from rophako.log import logger
#import rophako.model.tracking as Tracking #import rophako.model.tracking as Tracking
import rophako.utils import rophako.utils
# String escaping for the secret key (processes \ escapes properly), the
# escape encoding name varies between Python 2 and 3.
string_escape = "string_escape" if sys.version_info[0] == 2 \
else "unicode_escape"
app.DEBUG = Config.site.debug == "true" app.DEBUG = Config.site.debug == "true"
app.secret_key = Config.security.secret_key.decode("string_escape") app.secret_key = bytes(Config.security.secret_key.encode("utf-8")) \
.decode(string_escape)
# Security? # Security?
if Config.security.force_ssl == "true": if Config.security.force_ssl == "true":

View File

@ -1,12 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, print_function, absolute_import
"""JSON flat file database system.""" """JSON flat file database system."""
import codecs import codecs
import os import os
import os.path import os.path
import glob
import re import re
from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN
import redis import redis
@ -215,7 +214,7 @@ def get_redis():
) )
redis_client.ping() redis_client.ping()
except Exception as e: except Exception as e:
logger.error("Couldn't connect to Redis; memory caching will be disabled! {}".format(e.message)) logger.error("Couldn't connect to Redis; memory caching will be disabled! {}".format(e))
redis_client = None redis_client = None
disable_redis = True disable_redis = True
return redis_client return redis_client

View File

@ -1,11 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, print_function, absolute_import
"""Debug and logging functions.""" """Debug and logging functions."""
from __future__ import print_function
from flask import g, request
import logging import logging
from rophako.settings import Config from rophako.settings import Config
@ -17,9 +14,6 @@ class LogHandler(logging.Handler):
# The initial log line, which has the $prefix$ in it. # The initial log line, which has the $prefix$ in it.
line = self.format(record) line = self.format(record)
# Is the user logged in?
name = "-nobody-"
line = line.replace('$prefix$', '') line = line.replace('$prefix$', '')
print(line) print(line)

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Blog models.""" """Blog models."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Commenting models.""" """Commenting models."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Emoticon models.""" """Emoticon models."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Photo album models.""" """Photo album models."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Visitor tracking models.""" """Visitor tracking models."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""User account models.""" """User account models."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Wiki models.""" """Wiki models."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Endpoints for user login and out.""" """Endpoints for user login and out."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Endpoints for admin functions.""" """Endpoints for admin functions."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Endpoints for the web blog.""" """Endpoints for the web blog."""

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
"""Endpoints for the commenting subsystem.""" """Endpoints for the commenting subsystem."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Endpoints for contacting the site owner.""" """Endpoints for contacting the site owner."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Endpoints for the commenting subsystem.""" """Endpoints for the commenting subsystem."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Endpoints for the photo albums.""" """Endpoints for the photo albums."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Endpoints for visitor tracking functions.""" """Endpoints for visitor tracking functions."""

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Endpoints for the wiki.""" """Endpoints for the wiki."""

View File

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
"""Dynamic CMS plugin loader.""" """Dynamic CMS plugin loader."""
import os import os
from importlib import import_module from importlib import import_module
from rophako.app import app, BLUEPRINT_PATHS
def load_plugin(name, as_blueprint=True, template_path=None): def load_plugin(name, as_blueprint=True, template_path=None):
"""Load a Rophako CMS plugin. """Load a Rophako CMS plugin.
@ -17,6 +16,7 @@ def load_plugin(name, as_blueprint=True, template_path=None):
* `template_path` is a filesystem path where the blueprint's templates * `template_path` is a filesystem path where the blueprint's templates
can be found. If not provided, the path is automatically determined can be found. If not provided, the path is automatically determined
based on the module name, which is suitable for the built-in plugins.""" based on the module name, which is suitable for the built-in plugins."""
from rophako.app import app, BLUEPRINT_PATHS
module = import_module(name) module = import_module(name)
if as_blueprint: if as_blueprint:
mod = getattr(module, "mod") mod = getattr(module, "mod")

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, print_function, absolute_import
import os import os
import datetime import datetime
@ -33,7 +33,7 @@ class ConfigHandler(object):
def print_settings(self): def print_settings(self):
"""Pretty-print the contents of the configuration.""" """Pretty-print the contents of the configuration."""
print self.settings print(self.settings)
def load_plugins(self): def load_plugins(self):
"""Load all the plugins specified by the config file.""" """Load all the plugins specified by the config file."""

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, print_function, absolute_import
from flask import (g, session, request, render_template, flash, redirect, from flask import (g, session, request, render_template, flash, redirect,
url_for, current_app) url_for, current_app)
@ -14,7 +14,10 @@ import importlib
import smtplib import smtplib
import markdown import markdown
import json import json
import urlparse try:
import urlparse
except ImportError:
from urllib import parse as urlparse
import traceback import traceback
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
@ -207,7 +210,7 @@ def send_email(to, subject, message, sender=None, reply_to=None):
def handle_exception(error): def handle_exception(error):
"""Send an e-mail to the site admin when an exception occurs.""" """Send an e-mail to the site admin when an exception occurs."""
if current_app.config.get("DEBUG"): if current_app.config.get("DEBUG"):
print traceback.format_exc() print(traceback.format_exc())
raise raise
import rophako.jsondb as JsonDB import rophako.jsondb as JsonDB

View File

@ -1,8 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals, absolute_import
import sys
import argparse import argparse
from rophako.app import app from rophako.app import app
@ -49,4 +48,3 @@ if __name__ == '__main__':
flask_options["ssl_context"] = context flask_options["ssl_context"] = context
app.run(**flask_options) app.run(**flask_options)

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import unicode_literals from __future__ import unicode_literals, print_function, absolute_import
"""Locate any orphaned photos. """Locate any orphaned photos.
@ -12,11 +12,14 @@ import json
import glob import glob
sys.path.append(".") sys.path.append(".")
from rophako.settings import Config
Config.load_settings()
import rophako.jsondb as JsonDB import rophako.jsondb as JsonDB
def main(): def main():
if len(sys.argv) == 1: if len(sys.argv) == 1:
print "Usage: {} <path/to/static/photos>".format(__file__) print("Usage: {} <path/to/static/photos>".format(__file__))
sys.exit(1) sys.exit(1)
photo_root = sys.argv[1] photo_root = sys.argv[1]
@ -32,7 +35,7 @@ def main():
for img in glob.glob("{}/*.*".format(photo_root)): for img in glob.glob("{}/*.*".format(photo_root)):
fname = img.split("/")[-1] fname = img.split("/")[-1]
if not fname in photos: if not fname in photos:
print "Orphan:", fname print("Orphan:", fname)
if __name__ == "__main__": if __name__ == "__main__":
main() main()