Parent: [022f09] (diff)

Child: [ca2668] (diff)

Download this file

app_globals.py    107 lines (89 with data), 3.5 kB

  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
# -*- coding: utf-8 -*-
"""The application's Globals object"""
__all__ = ['Globals']
import logging
import socket
import pkg_resources
from tg import config, session
from pylons import c
import paste.deploy.converters
import pysolr
import markdown
from carrot.connection import BrokerConnection
from carrot.messaging import Publisher
from pyforge import model as M
from pyforge.lib.markdown_extensions import ArtifactExtension
log = logging.getLogger(__name__)
class Globals(object):
"""Container for objects available throughout the life of the application.
One instance of Globals is created during application initialization and
is available during requests via the 'app_globals' variable.
"""
def __init__(self):
"""Do nothing, by default."""
self.pyforge_templates = pkg_resources.resource_filename('pyforge', 'templates')
self.solr_server = config.get('solr.server')
if self.solr_server:
self.solr = pysolr.Solr(self.solr_server)
else:
self.solr = None
self.use_queue = paste.deploy.converters.asbool(
config.get('use_queue', False))
self.conn = BrokerConnection(
hostname=config.get('amqp.hostname', 'localhost'),
port=config.get('amqp.port', 5672),
userid=config.get('amqp.userid', 'testuser'),
password=config.get('amqp.password', 'testpw'),
virtual_host=config.get('amqp.vhost', 'testvhost'))
self.publisher = dict(
audit=Publisher(connection=self.conn, exchange='audit', auto_declare=False),
react=Publisher(connection=self.conn, exchange='react', auto_declare=False))
self.markdown = markdown.Markdown(
extensions=['codehilite', ArtifactExtension()],
output_format='html4')
self.oid_store = M.OpenIdStore()
def oid_session(self):
if 'openid_info' in session:
return session['openid_info']
else:
session['openid_info'] = result = {}
session.save()
return result
def app_static(self, resource, app=None):
app = app or c.app
return ''.join(
[ config['static_root'],
app.config.plugin_name,
'/',
resource ])
def set_project(self, pid):
c.project = M.Project.query.get(_id=pid + '/')
def set_app(self, name):
c.app = c.project.app_instance(name)
def publish(self, xn, key, message=None, **kw):
project = getattr(c, 'project', None)
app = getattr(c, 'app', None)
log.info('HI! I am publishing!')
if message is None: message = {}
if project:
message.setdefault('project_id', project._id)
if app:
message.setdefault('mount_point', app.config.options.mount_point)
if hasattr(c, 'queued_messages'):
c.queued_messages.append(dict(
xn=xn,
message=message,
routing_key=key,
**kw))
else:
self._publish(xn, message, routing_key=key, **kw)
def _publish(self, xn, message, routing_key, **kw):
try:
self.publisher[xn].send(message, routing_key=routing_key, **kw)
except socket.error:
return
log.exception('''Failure publishing message:
xn : %r
routing_key: %r
data : %r
''', xn, routing_key, message)