def load_environment(global_conf, app_conf): """Configure the Pylons environment via the ``pylons.config`` object """ config = PylonsConfig() # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # setup themes template_paths = [os.path.join(root, 'templates')] themesbase = app_conf.get('baruwa.themes.base', None) if themesbase and os.path.isabs(themesbase): templatedir = os.path.join(themesbase, 'templates') if os.path.isdir(templatedir): template_paths.append(templatedir) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=template_paths) # Initialize config with the basic options config.init_app(global_conf, app_conf, package='baruwa', paths=paths) config['routes.map'] = make_map(config) config['pylons.app_globals'] = app_globals.Globals(config) config['pylons.h'] = baruwa.lib.helpers # Setup cache object as early as possible import pylons pylons.cache._push_object(config['pylons.app_globals'].cache) # Create the Mako TemplateLookup, with the default auto-escaping config['pylons.app_globals'].mako_lookup = TemplateLookup( directories=paths['templates'], error_handler=handle_mako_error, module_directory=os.path.join(app_conf['cache_dir'], 'templates'), input_encoding='utf-8', default_filters=['escape'], imports=['from webhelpers.html import escape']) # Setup the SQLAlchemy database engine surl = config['sqlalchemy.url'] if surl.startswith('mysql'): conv = conversions.copy() conv[246] = float engine = create_engine(surl, pool_recycle=1800, connect_args=dict(conv=conv)) else: engine = engine_from_config(config, 'sqlalchemy.', poolclass=NullPool) init_model(engine) # CONFIGURATION OPTIONS HERE (note: all config options will override # any Pylons config options) return config
def write_html_sig(sigfile, sig, is_domain, logger): "write html sig" cleaner = SignatureCleaner(style=True, remove_tags=UNCLEANTAGS, safe_attrs_only=False) html = cleaner.clean_html(sig.signature_content) html = fragments_fromstring(html)[0] # pylint: disable-msg=W0612 for element, attribute, link, pos in iterlinks(html): if link.startswith('/fm/'): logger.info('Found img link, processing') routemap = make_map(config) routeargs = routemap.match(link) if not routeargs: continue if is_domain: model = DomSigImg else: model = UserSigImg img = Session.query(model)\ .filter(model.id == routeargs['imgid']).one() element.attrib['src'] = 'cid:%s' % img.name imgfile = os.path.join(os.path.dirname(sigfile), img.name) with open(imgfile, 'wb') as handle: handle.write(base64.decodestring(img.image)) logger.info('%s: stored to filesystem at: %s.' % (img.name, imgfile)) sig.image.append(img) if 'link' in locals(): Session.add(sig) Session.commit() # Session.close() with open(sigfile, 'w') as handle: if not sig.signature_content.startswith('--'): handle.write('<br/>--<br/>') handle.write(tostring(html)) logger.info('Finished processing HTML signature: %s' % sigfile)