/usr/share/pyshared/pyphantomjs/plugincontroller.py is in python-pyphantomjs 1.4.0+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | '''
This file is part of the PyPhantomJS project.
Copyright (C) 2011 James Roe <roejames12@hotmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import sys
from collections import defaultdict
from glob import glob
hooks = defaultdict(dict)
def createHook(func):
'''Decorator that will create a hook if the hook doesn't exist'''
def innerWrap(*args, **kwargs):
if args[0] not in hooks:
hooks[args[0]]['count'] = 0
hooks[args[0]]['plugins'] = []
return func(*args, **kwargs)
return innerWrap
def checkHookExists(func):
'''Decorator that will raise LookupError if the hook doesn't exist'''
def innerWrap(*args, **kwargs):
if args[0] not in hooks:
raise LookupError("Hook '%s' was not found" % args[0])
return func(*args, **kwargs)
return innerWrap
@createHook
def add_action(hook, priority=10):
'''Decorator to be used for registering a function to
a specific hook. Functions with lower priority are
executed first (priority of 1 for example).
'''
def register(func):
hooks[hook]['plugins'].append((priority, func))
return func
return register
@checkHookExists
def did_action(hook):
'''Find out how many times a hook was fired'''
return hooks[hook]['count']
@createHook
def do_action(hook, *args, **kwargs):
'''Trigger a hook. It will run any functions that have registered
themselves to the hook. Any additional arguments or keyword
arguments you pass in will be passed to the functions.
'''
# sort plugins by priority
hooks[hook]['plugins'].sort()
for plugin in hooks[hook]['plugins']:
hooks[hook]['count'] += 1
plugin[1](*args, **kwargs)
def get(name, depth=4, scope='local'):
'''Gets the value of a variable in the parents namespace
Args:
depth controls how deep to go into the stack
scope controls the namespace used; valid values are: local,global,builtin
'''
if scope == 'local':
scope = 'f_locals'
elif scope == 'global':
scope = 'f_globals'
elif scope == 'builtin':
scope = 'f_builtins'
return getattr(sys._getframe(depth), scope)[name]
def has_action(hook, func=None):
'''Check if hook exists. If function is specified,
check if function has been registered for hook.
'''
if func is None:
if hook in hooks:
return True
else:
if hook not in hooks:
raise LookupError("Hook '%s' was not found" % hook)
for plugin in hooks[hook]['plugins']:
if plugin[1] is func:
return True
return False
def remove_action(hook, func=None, priority=10):
'''Remove hook if hook exists. If function is specified,
remove function from hook. If priority is not same as
functions priority, it will not remove the function.
'''
if func is None:
if hook in hooks:
del hooks[hook]
return True
else:
if hook not in hooks:
raise LookupError("Hook '%s' was not found" % hook)
for i, plugin in enumerate(hooks[hook]['plugins']):
if plugin[1] is func and plugin[0] == priority:
del hooks[hook]['plugins'][i]
return True
return False
@checkHookExists
def remove_all_actions(hook, priority=None):
'''Remove all functions that have been registered to hook.
If priority is used, remove all actions from that priority
instead.
'''
if priority is None:
del hooks[hook][:]
else:
indexes = []
for i, plugin in enumerate(hooks[hook]['plugins']):
if plugin[0] == priority:
indexes.append(i)
for index in indexes:
del hooks[hook]['plugins'][index]
def set_(name, value, depth=4, scope='local'):
'''Sets the value of a variable in the parents namespace
Args:
depth controls how deep to go into the stack
scope controls the namespace used; valid values are: local,global,builtin
'''
if scope == 'local':
scope = 'f_locals'
elif scope == 'global':
scope = 'f_globals'
elif scope == 'builtin':
scope = 'f_builtins'
getattr(sys._getframe(depth), scope)[name] = value
def load_plugins():
''' Loads the plugins.
Plugins must be in folders under plugins/ (or plugins_path),
and must also be the same name as the plugin folder.
E.g. a plugin folder named my_plugin will
have my_plugin.py inside the folder loaded.
'''
plugins_path = os.environ.get('PYPHANTOMJS_PLUGINS_PATH')
if plugins_path is None:
# path is different when frozen
if hasattr(sys, 'frozen'):
plugins_path = os.path.dirname(os.path.abspath(sys.executable))
else:
plugins_path = os.path.dirname(os.path.abspath(__file__))
plugins_path = os.path.join(plugins_path, 'plugins')
else:
# make sure it's an absolute path
plugins_path = os.path.abspath(plugins_path)
sys.path.insert(1, plugins_path)
plugin_list = glob(os.path.join(plugins_path, '*/*.py'))
# now convert list to [('plugin_folder', 'file'), ...]
plugin_list = [(os.path.split(os.path.dirname(f))[1], os.path.splitext(os.path.split(f)[1])[0]) for f in plugin_list]
# initialize plugins
for plugin in plugin_list:
if plugin[0] == plugin[1]:
moduleName = '.'.join((plugin[0], plugin[1]))
__import__(moduleName, globals(), locals(), [], -1)
|