Commits

Berry Phillips committed 8cdece4

Added configurable settings.

Comments (0)

Files changed (4)

 syntax: glob
+settings.yaml
 *pyc
 *swp
 logs/*.log
 gevent==0.13.6
 greenlet==0.3.3
 nose==1.1.2
+pyyaml==3.10

s3_example_settings.yaml

+host: localhost
+port: 8000
+ignore_favicon: True
+cloudfront_ugliness: True
+storage_backend: S3Storage
+s3_key: xxx
+s3_secret: xxx
+s3_bucket: xxx
 #!/usr/bin/env python
 
+"""
+Thumpy web process to resize images.
+"""
+
+import argparse
 import os
 import urlparse
+import yaml
 from cStringIO import StringIO
 
 try:
 from PIL import ImageOps, Image as PILImage
 from boto import connect_s3
 
+
+config = {
+    'host': 'localhost',
+    'port': 8000,
+    'ignore_favicon': True,
+    'cloudfront_ugliness': False,
+    'storage_backend': 'LocalStorage',
+}
+
+
 class MissingImage(Exception):
     pass
 
     return od
 
 
-IGNORE_FAVICON = True
-CLOUDFRONT_UGLINESS = False
-
 def Http404(start_response):
     start_response("404 NOT FOUND", [('Content-Type','text/plain')])
     return ["File not found"]
     sto = LocalStorage()
 
     # throw away the leading slash on the path.
-    path = environ['PATH_INFO'][1:]
+    path = environ['PATH_INFO']
+    if path.startswith('/'):
+        path = path[1:]
 
-    if CLOUDFRONT_UGLINESS:
+    if config['cloudfront_ugliness']:
         # DIRTY CLOUDFRONT HACK HERE
-        # Stupid CloudFront doesn't pass query string arguments, so we have to put
-        # image change params into the path.
+        # Stupid CloudFront doesn't pass query string arguments, so we have to
+        # put image change params into the path.
         pathparts = path.split('/')
 
         #path = environ['PATH_INFO'][1:]
         filepath = path
         params = oparse_qs(environ['QUERY_STRING'])
 
-    if IGNORE_FAVICON and filepath == 'favicon.ico':
+    if config['ignore_favicon'] and filepath == 'favicon.ico':
         return Http404(start_response)
 
     try:
     start_response("200 OK", [('Content-Type',im.mimetype)])
     return [im.contents(options=params)]
 
+
 if __name__ == '__main__':
     from gevent.wsgi import WSGIServer
 
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('--config', dest='config_file',
+                    default='',
+                    help='Path to the YAML config file.')
+
+    args = parser.parse_args()
+
+    if args.config_file:
+        with open(args.config_file, 'r') as f:
+            config.update(yaml.safe_load(f))
+
     # monkeypatching with gevent should make our calls out to S3 non-blocking.
     # It won't do anything for out CPU-bound image processing though.
     #from gevent import monkey; monkey.patch_socket()
-    address = "0.0.0.0", 8000
+    address = config['host'], config['port']
     server = WSGIServer(address, app)
     try:
         print "Server running on port %s:%d. Ctrl+C to quit" % address
     except KeyboardInterrupt:
         server.stop()
         print "Bye bye"
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.