Browse Source

Support Python 3 for running the app

pull/2/head
Noah Petherbridge 3 years ago
parent
commit
e38ccc7190

+ 8
- 2
rophako/app.py View File

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

+ 2
- 3
rophako/jsondb.py View File

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

+ 1
- 7
rophako/log.py View File

@@ -1,11 +1,8 @@
1 1
 # -*- coding: utf-8 -*-
2
-from __future__ import unicode_literals
2
+from __future__ import unicode_literals, print_function, absolute_import
3 3
 
4 4
 """Debug and logging functions."""
5 5
 
6
-from __future__ import print_function
7
-
8
-from flask import g, request
9 6
 import logging
10 7
 
11 8
 from rophako.settings import Config
@@ -17,9 +14,6 @@ class LogHandler(logging.Handler):
17 14
         # The initial log line, which has the $prefix$ in it.
18 15
         line = self.format(record)
19 16
 
20
-        # Is the user logged in?
21
-        name = "-nobody-"
22
-
23 17
         line = line.replace('$prefix$', '')
24 18
         print(line)
25 19
 

+ 1
- 1
rophako/model/blog.py View File

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

+ 1
- 1
rophako/model/comment.py View File

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

+ 1
- 1
rophako/model/emoticons.py View File

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

+ 1
- 1
rophako/model/photo.py View File

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

+ 1
- 1
rophako/model/tracking.py View File

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

+ 1
- 1
rophako/model/user.py View File

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

+ 1
- 1
rophako/model/wiki.py View File

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

+ 1
- 1
rophako/modules/account/__init__.py View File

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

+ 1
- 1
rophako/modules/admin/__init__.py View File

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

+ 1
- 1
rophako/modules/blog/__init__.py View File

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

+ 1
- 0
rophako/modules/comment/__init__.py View File

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

+ 1
- 1
rophako/modules/contact/__init__.py View File

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

+ 1
- 1
rophako/modules/emoticons/__init__.py View File

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

+ 1
- 1
rophako/modules/photo/__init__.py View File

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

+ 1
- 1
rophako/modules/tracking/__init__.py View File

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

+ 1
- 1
rophako/modules/wiki/__init__.py View File

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

+ 2
- 2
rophako/plugin.py View File

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

+ 2
- 2
rophako/settings.py View File

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

+ 6
- 3
rophako/utils.py View File

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

+ 1
- 3
runserver.py View File

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

+ 6
- 3
scripts/orphaned-photos.py View File

@@ -1,5 +1,5 @@
1 1
 #!/usr/bin/env python
2
-from __future__ import unicode_literals
2
+from __future__ import unicode_literals, print_function, absolute_import
3 3
 
4 4
 """Locate any orphaned photos.
5 5
 
@@ -12,11 +12,14 @@ import json
12 12
 import glob
13 13
 
14 14
 sys.path.append(".")
15
+from rophako.settings import Config
16
+Config.load_settings()
17
+
15 18
 import rophako.jsondb as JsonDB
16 19
 
17 20
 def main():
18 21
     if len(sys.argv) == 1:
19
-        print "Usage: {} <path/to/static/photos>".format(__file__)
22
+        print("Usage: {} <path/to/static/photos>".format(__file__))
20 23
         sys.exit(1)
21 24
 
22 25
     photo_root = sys.argv[1]
@@ -32,7 +35,7 @@ def main():
32 35
     for img in glob.glob("{}/*.*".format(photo_root)):
33 36
         fname = img.split("/")[-1]
34 37
         if not fname in photos:
35
-            print "Orphan:", fname
38
+            print("Orphan:", fname)
36 39
 
37 40
 if __name__ == "__main__":
38 41
     main()

Loading…
Cancel
Save