Permalink
Browse files

Simplified plugins inputs/outputs

  • Loading branch information...
gbrindisi committed Jul 23, 2012
1 parent 816172c commit 99eda64f5bc2772bbe8a2d503cf890f790a4cb70
View
@@ -9,7 +9,6 @@
from wordpot import app, pm, parse_options, check_options
from wordpot.logger import *
-
import os
check_options()
@@ -1,32 +1,26 @@
from wordpot.plugins_manager import BasePlugin
class Plugin(BasePlugin):
- def run(self, **kwargs):
- # Result dict to return
- res = {}
- res['template_vars'] = {}
-
- # Store input arguments
- args = {}
- for k, v in kwargs.iteritems():
- args[k] = v
+ def run(self):
+ # Initialize template vars dict
+ self.outputs['template_vars'] = {}
# First check if the file is wp-login.php
- if not (args['file'] == 'wp-login' and args['ext'] == 'php'):
- return {}
+ if not (self.inputs['filename'] == 'wp-login' and self.inputs['ext'] == 'php'):
+ return
# Logic
- origin = args['request'].remote_addr
+ origin = self.inputs['request'].remote_addr
- if args['request'].method == 'POST':
- username = args['request'].form['log']
- password = args['request'].form['pwd']
- res['log'] = '%s tried to login with username %s and password %s' % (origin, username, password)
- res['template_vars']['BADLOGIN'] = True
- res['template'] = 'wp-login.html'
+ if self.inputs['request'].method == 'POST':
+ username = self.inputs['request'].form['log']
+ password = self.inputs['request'].form['pwd']
+ self.outputs['log'] = '%s tried to login with username %s and password %s' % (origin, username, password)
+ self.outputs['template_vars']['BADLOGIN'] = True
+ self.outputs['template'] = 'wp-login.html'
else:
- res['log'] = '%s probed for the login page' % origin
- res['template_vars']['BADLOGIN'] = False
- res['template'] = 'wp-login.html'
+ self.outputs['log'] = '%s probed for the login page' % origin
+ self.outputs['template_vars']['BADLOGIN'] = False
+ self.outputs['template'] = 'wp-login.html'
- return res
+ return
@@ -1,15 +1,9 @@
from wordpot.plugins_manager import BasePlugin
class Plugin(BasePlugin):
- def run(self, **kwargs):
- # Result dict to return
- res = {}
- res['template_vars'] = {}
-
- # Store input arguments
- args = {}
- for k, v in kwargs.iteritems():
- args[k] = v
+ def run(self):
+ # Initialize template vars dict
+ self.outputs['template_vars'] = {}
# Common files:
# Real File -> Template
@@ -19,12 +13,12 @@ def run(self, **kwargs):
}
# Logic
- origin = args['request'].remote_addr
- if args['file'] is not None and args['ext'] is not None:
- filename = args['file'] + '.' + args['ext']
+ origin = self.inputs['request'].remote_addr
+ if self.inputs['filename'] is not None and self.inputs['ext'] is not None:
+ filename = self.inputs['filename'] + '.' + self.inputs['ext']
if filename in common:
- res['log'] = '%s probed for: %s' % (origin, filename)
- res['template'] = common[filename]
+ self.outputs['log'] = '%s probed for: %s' % (origin, filename)
+ self.outputs['template'] = common[filename]
- return res
+ return
@@ -4,22 +4,14 @@
TIMTHUMB_RE = re.compile('[tim]*thumb|uploadify', re.I)
class Plugin(BasePlugin):
- def run(self, **kwargs):
- # Result dict to return
- res = {}
-
- # Store input arguments
- args = {}
- for k, v in kwargs.iteritems():
- args[k] = v
-
+ def run(self):
# Logic
- if TIMTHUMB_RE.search(args['subpath']) is not None:
+ if TIMTHUMB_RE.search(self.inputs['subpath']) is not None:
# Message to log
- log = '%s probed for timthumb: %s' % (args['request'].remote_addr, args['subpath'])
- res['log'] = log
+ log = '%s probed for timthumb: %s' % (self.inputs['request'].remote_addr, self.inputs['subpath'])
+ self.outputs['log'] = log
# Template to render
- res['template'] = 'timthumb.html'
+ self.outputs['template'] = 'timthumb.html'
- return res
+ return
@@ -2,26 +2,20 @@
from wordpot import app
class Plugin(BasePlugin):
- def run(self, **kwargs):
- # Result dict to return
- res = {}
- res['template_vars'] = {}
-
- # Store input arguments
- args = {}
- for k, v in kwargs.iteritems():
- args[k] = v
+ def run(self):
+ # Initialize template vars dict
+ self.outputs['template_vars'] = {}
# Logic
- origin = args['request'].remote_addr
- req_args = args['request'].args
+ origin = self.inputs['request'].remote_addr
+ req_args = self.inputs['request'].args
if 'author' in req_args:
for k, a in enumerate(app.config['AUTHORS']):
if (k + 1) == int(req_args['author']):
- res['log'] = '%s probed author page for user: %s' % (origin, a)
- res['template_vars']['AUTHORPAGE'] = True
- res['template_vars']['CURRENTAUTHOR'] = (k+1, a)
- res['template'] = 'dummy.html'
+ self.outputs['log'] = '%s probed author page for user: %s' % (origin, a)
+ self.outputs['template_vars']['AUTHORPAGE'] = True
+ self.outputs['template_vars']['CURRENTAUTHOR'] = (k+1, a)
+ self.outputs['template'] = app.config['THEME'] + '.html'
- return res
+ return
View
@@ -1,6 +1,7 @@
#!/usr/bin/env python
from flask import request
+from wordpot.logger import *
import os
import ConfigParser
@@ -52,6 +53,9 @@ def __init__(self, slug=None):
self.request = None
+ self.inputs = {}
+ self.outputs = {}
+
def _load_config(self, slug=None):
self.slug = slug
try:
@@ -69,6 +73,19 @@ def _load_config(self, slug=None):
self.hooks = [v.strip() for v in config.get('plugin', 'hooks').split(',')]
except Exception, e:
pass
+
+ def start(self, **kwargs):
+ # First flush previous inputs/outputs
+ self.inputs = {}
+ self.outputs = {}
+
+ # Parse arguments
+ for k, v in kwargs.iteritems():
+ self.inputs[k] = v
+ try:
+ self.run()
+ except Exception, e:
+ LOGGER.error('Unable to run plugin: %s\n%s', self.name, e.message)
def run(self):
- return {}
+ return
View
@@ -8,25 +8,22 @@
TEMPLATE = app.config['THEME'] + '.html'
@app.route('/', methods=['GET', 'POST'])
-@app.route('/<file>.<ext>', methods=['GET', 'POST'])
-def commons(file=None, ext=None):
+@app.route('/<filename>.<ext>', methods=['GET', 'POST'])
+def commons(filename=None, ext=None):
# Plugins hook
for p in pm.hook('commons'):
- try:
- res = p.run(file=file, ext=ext, request=request)
- if 'log' in res:
- LOGGER.info(res['log'])
- if 'template' in res:
- if 'template_vars' in res:
- return render_template(res['template'], vars=res['template_vars'])
- return render_template(res['template'], vars={})
- except Exception, e:
- LOGGER.error('Unable to run plugin: %s\n%s', p.name, e.message)
+ p.start(filename=filename, ext=ext, request=request)
+ if 'log' in p.outputs:
+ LOGGER.info(p.outputs['log'])
+ if 'template' in p.outputs:
+ if 'template_vars' in p.outputs:
+ return render_template(p.outputs['template'], vars=p.outputs['template_vars'])
+ return render_template(p.outputs['template'], vars={})
- if file is None and ext is None:
+ if filename is None and ext is None:
return render_template(TEMPLATE, vars={})
- elif file == 'index' and ext == 'php':
+ elif filename == 'index' and ext == 'php':
return render_template(TEMPLATE, vars={})
else:
abort(404)
@@ -40,16 +37,13 @@ def admin(subpath='/'):
# Plugins hook
for p in pm.hook('plugins'):
- try:
- res = p.run(subpath=subpath, request=request)
- if 'log' in res:
- LOGGER.info(res['log'])
- if 'template' in res:
- if 'template_vars' in res:
- return render_template(res['template'], vars=res['template_vars'])
- return render_template(res['template'], vars={})
- except Exception, e:
- LOGGER.error('Unable to run plugin: %s\n%s', p.name, e.message)
+ p.start(subpath=subpath, request=request)
+ if 'log' in p.outputs:
+ LOGGER.info(p.outputs['log'])
+ if 'template' in p.outputs:
+ if 'template_vars' in p.outputs:
+ return render_template(p.outputs['template'], vars=p.outputs['template_vars'])
+ return render_template(p.outputs['template'], vars={})
return redirect('wp-login.php')
@@ -66,16 +60,13 @@ def plugin(plugin, subpath='/'):
# Plugins hook
for p in pm.hook('plugins'):
- try:
- res = p.run(plugin=plugin, subpath=subpath, request=request)
- if 'log' in res:
- LOGGER.info(res['log'])
- if 'template' in res:
- if 'template_vars' in res:
- return render_template(res['template'], vars=res['template_vars'])
- return render_template(res['template'], vars={})
- except Exception, e:
- LOGGER.error('Unable to run plugin: %s\n%s', p.name, e.message)
+ p.start(plugin=plugin, subpath=subpath, request=request)
+ if 'log' in p.outputs:
+ LOGGER.info(p.outputs['log'])
+ if 'template' in p.outputs:
+ if 'template_vars' in p.outputs:
+ return render_template(p.outputs['template'], vars=p.outputs['template_vars'])
+ return render_template(p.outputs['template'], vars={})
return render_template(TEMPLATE, vars={})
@@ -92,16 +83,13 @@ def theme(theme, subpath='/'):
# Plugins hook
for p in pm.hook('themes'):
- try:
- res = p.run(theme=theme, subpath=subpath, request=request)
- if 'log' in res:
- LOGGER.info(res['log'])
- if 'template' in res:
- if 'template_vars' in res:
- return render_template(res['template'], vars=res['template_vars'])
- return render_template(res['template'], vars={})
- except Exception, e:
- LOGGER.error('Unable to run plugin: %s\n%s', p.name, e.message)
+ p.start(theme=theme, subpath=subpath, request=request)
+ if 'log' in p.outputs:
+ LOGGER.info(p.outputs['log'])
+ if 'template' in p.outputs:
+ if 'template_vars' in p.outputs:
+ return render_template(p.outputs['template'], vars=p.outputs['template_vars'])
+ return render_template(p.outputs['template'], vars={})
return render_template(TEMPLATE, vars={})

0 comments on commit 99eda64

Please sign in to comment.