def __init__(self, global_conf, app_conf, paths, **extra): """ Globals acts as a container for objects available throughout the life of the application. One instance of Globals is created by Pylons during application initialization and is available during requests via the 'g' variable. ``global_conf`` The same variable used throughout ``config/middleware.py`` namely, the variables from the ``[DEFAULT]`` section of the configuration file. ``app_conf`` The same ``kw`` dictionary used throughout ``config/middleware.py`` namely, the variables from the section in the config file for your application. ``extra`` The configuration returned from ``load_config`` in ``config/middleware.py`` which may be of use in the setup of your global variables. """ global_conf.setdefault("debug", False) self.config = ConfigValueParser(global_conf) self.config.add_spec(self.spec) self.plugins = PluginLoader(self.config.get("plugins", [])) self.stats = Stats(self.config.get('statsd_addr'), self.config.get('statsd_sample_rate')) self.startup_timer = self.stats.get_timer("app_startup") self.startup_timer.start() self.paths = paths self.running_as_script = global_conf.get('running_as_script', False) # turn on for language support self.lang = getattr(self, 'site_lang', 'en') self.languages, self.lang_name = \ get_active_langs(default_lang=self.lang) all_languages = self.lang_name.keys() all_languages.sort() self.all_languages = all_languages # set default time zone if one is not set tz = global_conf.get('timezone', 'UTC') self.tz = pytz.timezone(tz) dtz = global_conf.get('display_timezone', tz) self.display_tz = pytz.timezone(dtz) self.startup_timer.intermediate("init")
def get_generated_static_files(): """List all static files that are generated by the build process.""" PluginLoader() # ensure all the plugins put their statics in for filename, mangled in g.static_names.iteritems(): yield filename yield mangled _, ext = os.path.splitext(filename) if ext in (".css", ".js"): yield filename + ".gzip" yield mangled + ".gzip"
def load_environment(global_conf={}, app_conf={}, setup_globals=True): # Setup our paths root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = { 'root': root_path, 'controllers': os.path.join(root_path, 'controllers'), 'templates': tmpl_dirs, } if ConfigValue.bool(global_conf.get('uncompressedJS')): paths['static_files'] = os.path.join(root_path, 'public') else: paths['static_files'] = os.path.join(os.path.dirname(root_path), 'build/public') config.init_app(global_conf, app_conf, package='r2', template_engine='mako', paths=paths) g = config['pylons.g'] = Globals(global_conf, app_conf, paths) if setup_globals: g.setup() r2.config.cache = g.cache config['pylons.h'] = r2.lib.helpers g.plugins = config['r2.plugins'] = PluginLoader().load_plugins( g.config.get('plugins', [])) config['routes.map'] = routing.make_map() #override the default response options config['pylons.response_options']['headers'] = {} # The following template options are passed to your template engines #tmpl_options = {} #tmpl_options['myghty.log_errors'] = True #tmpl_options['myghty.escapes'] = dict(l=webhelpers.auto_link, s=webhelpers.simple_format) tmpl_options = config['buffet.template_options'] tmpl_options['mako.filesystem_checks'] = getattr(g, 'reload_templates', False) tmpl_options['mako.default_filters'] = ["mako_websafe"] tmpl_options['mako.imports'] = \ ["from r2.lib.filters import websafe, unsafe, mako_websafe", "from pylons import c, g, request", "from pylons.i18n import _, ungettext"]
def clean_static_files(config_file): bucket, config = read_static_file_config(config_file) ignored_prefixes = tuple(p.strip() for p in config["ignored_prefixes"].split(",")) plugins = PluginLoader() reachable_files = itertools.chain( get_source_static_files(plugins), get_generated_static_files(), ) condemned_files = get_mature_files_on_s3(bucket) for reachable_file in reachable_files: if reachable_file in condemned_files: del condemned_files[reachable_file] for filename, key in condemned_files.iteritems(): if not filename.startswith(ignored_prefixes): key.delete()
def __init__(self, global_conf, app_conf, paths, **extra): """ Globals acts as a container for objects available throughout the life of the application. One instance of Globals is created by Pylons during application initialization and is available during requests via the 'g' variable. ``global_conf`` The same variable used throughout ``config/middleware.py`` namely, the variables from the ``[DEFAULT]`` section of the configuration file. ``app_conf`` The same ``kw`` dictionary used throughout ``config/middleware.py`` namely, the variables from the section in the config file for your application. ``extra`` The configuration returned from ``load_config`` in ``config/middleware.py`` which may be of use in the setup of your global variables. """ global_conf.setdefault("debug", False) # reloading site ensures that we have a fresh sys.path to build our # working set off of. this means that forked worker processes won't get # the sys.path that was current when the master process was spawned # meaning that new plugins will be picked up on regular app reload # rather than having to restart the master process as well. reload(site) self.pkg_resources_working_set = pkg_resources.WorkingSet() self.config = ConfigValueParser(global_conf) self.config.add_spec(self.spec) self.plugins = PluginLoader(self.pkg_resources_working_set, self.config.get("plugins", [])) self.stats = Stats(self.config.get('statsd_addr'), self.config.get('statsd_sample_rate')) self.startup_timer = self.stats.get_timer("app_startup") self.startup_timer.start() self.paths = paths self.running_as_script = global_conf.get('running_as_script', False) # turn on for language support self.lang = getattr(self, 'site_lang', 'en') self.languages, self.lang_name = \ get_active_langs(default_lang=self.lang) all_languages = self.lang_name.keys() all_languages.sort() self.all_languages = all_languages # set default time zone if one is not set tz = global_conf.get('timezone', 'UTC') self.tz = pytz.timezone(tz) dtz = global_conf.get('display_timezone', tz) self.display_tz = pytz.timezone(dtz) self.startup_timer.intermediate("init")
def load_plugin_modules(plugins=None): if not plugins: plugins = PluginLoader() for plugin in plugins: plugin.add_js(module)
from r2.lib.translation import I18N_PATH from r2.lib.plugin import PluginLoader from r2.lib import js print 'I18NPATH := ' + I18N_PATH plugins = list(PluginLoader.available_plugins()) print 'PLUGINS := ' + ' '.join(plugin.name for plugin in plugins) for plugin in plugins: print 'PLUGIN_PATH_%s := %s' % (plugin.name, PluginLoader.plugin_path(plugin)) js.load_plugin_modules() modules = dict((k, m) for k, m in js.module.iteritems() if m.should_compile) print 'JS_MODULES := ' + ' '.join(modules.iterkeys()) outputs = [] for name, module in modules.iteritems(): outputs.extend(module.outputs) print 'JS_MODULE_OUTPUTS_%s := %s' % (name, ' '.join(module.outputs)) print 'JS_MODULE_DEPS_%s := %s' % (name, ' '.join(module.dependencies)) print 'JS_OUTPUTS := ' + ' '.join(outputs)
def load_plugin_modules(): from r2.lib.plugin import PluginLoader for plugin in PluginLoader.available_plugins(): plugin_cls = plugin.load() plugin_cls().add_js(module)
# # The Original Developer is the Initial Developer. The Initial Developer of # the Original Code is reddit Inc. # # All portions of the code written by reddit are Copyright (c) 2006-2015 reddit # Inc. All Rights Reserved. ############################################################################### import os from r2.lib.translation import I18N_PATH from r2.lib.plugin import PluginLoader from r2.lib import js print 'POTFILE := ' + os.path.join(I18N_PATH, 'r2.pot') plugins = PluginLoader() print 'PLUGINS := ' + ' '.join(plugin.name for plugin in plugins if plugin.needs_static_build) print 'PLUGIN_I18N_PATHS := ' + ','.join(os.path.relpath(plugin.path) for plugin in plugins if plugin.needs_translation) import sys for plugin in plugins: print 'PLUGIN_PATH_%s := %s' % (plugin.name, plugin.path) js.load_plugin_modules(plugins) modules = dict((k, m) for k, m in js.module.iteritems()) print 'JS_MODULES := ' + ' '.join(modules.iterkeys()) outputs = []
# # The Original Developer is the Initial Developer. The Initial Developer of # the Original Code is reddit Inc. # # All portions of the code written by reddit are Copyright (c) 2006-2012 reddit # Inc. All Rights Reserved. ############################################################################### import os from r2.lib.translation import I18N_PATH from r2.lib.plugin import PluginLoader from r2.lib import js print 'POTFILE := ' + os.path.join(I18N_PATH, 'r2.pot') plugins = list(PluginLoader.available_plugins()) print 'PLUGINS := ' + ' '.join(plugin.name for plugin in plugins) for plugin in plugins: print 'PLUGIN_PATH_%s := %s' % (plugin.name, PluginLoader.plugin_path(plugin)) js.load_plugin_modules() modules = dict((k, m) for k, m in js.module.iteritems()) print 'JS_MODULES := ' + ' '.join(modules.iterkeys()) outputs = [] for name, module in modules.iteritems(): outputs.extend(module.outputs) print 'JS_MODULE_OUTPUTS_%s := %s' % (name, ' '.join(module.outputs)) print 'JS_MODULE_DEPS_%s := %s' % (name, ' '.join(module.dependencies)) print 'JS_OUTPUTS := ' + ' '.join(outputs)
def load_plugin_modules(): from r2.lib.plugin import PluginLoader for plugin in PluginLoader(): plugin.add_js(module)