| 37 | | from PyLucid.models import Plugin, Preference |
| 38 | | |
| 39 | | |
| 40 | | def _import(request, from_name, object_name): |
| 41 | | """ |
| 42 | | from 'from_name' import 'object_name' |
| 43 | | """ |
| 44 | | try: |
| 45 | | return __import__(from_name, {}, {}, [object_name]) |
| 46 | | except (ImportError, SyntaxError), e: |
| 47 | | if request.user.is_superuser or request.debug: |
| 48 | | raise |
| 49 | | raise ImportError, "Can't import %s from %s: %s" % ( |
| 50 | | object_name, from_name, e |
| 51 | | ) |
| 52 | | |
| 53 | | def get_plugin_class(request, package_name, plugin_name): |
| 54 | | """ |
| 55 | | import the plugin/plugin and returns the class object |
| 56 | | """ |
| 57 | | plugin = _import(request, |
| 58 | | from_name = ".".join([package_name, plugin_name, plugin_name]), |
| 59 | | object_name = plugin_name |
| 60 | | ) |
| 61 | | plugin_class = getattr(plugin, plugin_name) |
| 62 | | return plugin_class |
| 63 | | |
| 64 | | def debug_plugin_config(page_msg, plugin_config): |
| 65 | | for item in dir(plugin_config): |
| 66 | | if item.startswith("_"): |
| 67 | | continue |
| 68 | | page_msg("'%s':" % item) |
| 69 | | page_msg(getattr(plugin_config, item)) |
| 70 | | |
| 71 | | def get_plugin_config(request, package_name, plugin_name, |
| 72 | | dissolve_version_string=False, extra_verbose=False): |
| 73 | | """ |
| 74 | | imports the plugin and the config plugin and returns a merge config-object |
| 75 | | |
| 76 | | dissolve_version_string == True -> get the version string (__version__) |
| 77 | | from the plugin and put it into the config object |
| 78 | | """ |
| 79 | | config_name = "%s_cfg" % plugin_name |
| 80 | | |
| 81 | | def get_plugin(object_name): |
| 82 | | from_name = ".".join([package_name, plugin_name, object_name]) |
| 83 | | if extra_verbose: |
| 84 | | print "from %s import %s" % (from_name, object_name) |
| 85 | | return _import(request, from_name, object_name) |
| 86 | | |
| 87 | | config_plugin = get_plugin(config_name) |
| 88 | | |
| 89 | | if dissolve_version_string: |
| 90 | | plugin_plugin = get_plugin(plugin_name) |
| 91 | | |
| 92 | | plugin_version = getattr(plugin_plugin, "__version__", None) |
| 93 | | if plugin_version: |
| 94 | | # Cleanup a SVN Revision Number |
| 95 | | plugin_version = plugin_version.strip("$ ") |
| 96 | | config_plugin.__version__ = plugin_version |
| 97 | | |
| 98 | | return config_plugin |
| | 40 | from PyLucid.models import Plugin |
| | 41 | |
| | 42 | |
| | 43 | |
| 169 | | plugin_class=get_plugin_class(request, plugin.package_name, plugin_name) |
| | 114 | plugin_module = get_plugin_module(request, plugin.package_name, plugin_name) |
| | 115 | |
| | 116 | if plugin_name in preference_cache: |
| | 117 | context.preferences = preference_cache[plugin_name] |
| | 118 | else: |
| | 119 | if hasattr(plugin_module, "PreferencesForm"): |
| | 120 | # Get the preferences dict data from the database |
| | 121 | try: |
| | 122 | p = Preferences() |
| | 123 | p.set_plugin(plugin) |
| | 124 | p.load_from_db() |
| | 125 | context.preferences = p.data_dict |
| | 126 | except PreferenceDoesntExist, e: |
| | 127 | error("Can't get preferences: %s" % e) |
| | 128 | return |
| | 129 | else: |
| | 130 | # plugin has no preferences |
| | 131 | context.preferences = None |
| | 132 | |
| | 133 | plugin_class = getattr(plugin_module, plugin_name) |
| 277 | | def _insert_preferences(plugin, plugin_config): |
| 278 | | preferences = getattr(plugin_config, "preferences", None) |
| 279 | | if preferences == None: |
| 280 | | # The plugin has no preferences |
| | 241 | |
| | 242 | def _insert_preferences(request, plugin, package_name, plugin_name): |
| | 243 | """ |
| | 244 | insertet the initial values from the newforms preferences class into the |
| | 245 | database. |
| | 246 | """ |
| | 247 | plugin_module = get_plugin_module(request, package_name, plugin_name) |
| | 248 | |
| | 249 | pref_form = getattr(plugin_module, "PreferencesForm", None) |
| | 250 | if pref_form == None: |
| | 251 | # Has no preferences newform class |