Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[01d9bf]: pyforge / pyforge / lib / app_globals.py Maximize Restore History

Download this file

app_globals.py    205 lines (170 with data), 7.0 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
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
202
# -*- coding: utf-8 -*-
"""The application's Globals object"""
__all__ = ['Globals']
import logging
import socket
import re
from urllib import urlencode
from ConfigParser import RawConfigParser
from collections import defaultdict
import pkg_resources
from tg import config, session
from pylons import c, request
import pysolr
import oembed
import markdown
from carrot.connection import BrokerConnection
from carrot.messaging import Publisher
from pymongo.bson import ObjectId
from paste.deploy.converters import asint, asbool
from pyforge import model as M
from pyforge.lib.markdown_extensions import ForgeExtension
from pyforge.lib import gravatar
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')
# Setup SOLR
self.solr_server = config.get('solr.server')
if self.solr_server:
self.solr = pysolr.Solr(self.solr_server)
else: # pragma no cover
self.solr = None
self.use_queue = asbool(config.get('use_queue', False))
# Setup RabbitMQ
if asbool(config.get('amqp.mock')):
self.mock_amq = MockAMQ()
self._publish = self.mock_amq.publish
else:
self.conn = BrokerConnection(
hostname=config.get('amqp.hostname', 'localhost'),
port=asint(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))
# Setup markdown
self.markdown = markdown.Markdown(
extensions=['codehilite', ForgeExtension(), 'meta'],
output_format='html4')
self.markdown_wiki = markdown.Markdown(
extensions=['codehilite', ForgeExtension(wiki=True),'meta'],
output_format='html4')
# Setup OEmbed
cp = RawConfigParser()
cp.read(config['oembed.config'])
self.oembed_consumer = consumer = oembed.OEmbedConsumer()
for endpoint in cp.sections():
values = [ v for k,v in cp.items(endpoint) ]
consumer.addEndpoint(oembed.OEmbedEndpoint(endpoint, values))
# Setup Gravatar
self.gravatar = gravatar.url
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(shortname=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)
user = getattr(c, 'user', None)
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 user:
if user._id is None:
message.setdefault('user_id', None)
else:
message.setdefault('user_id', user._id)
# Make message safe for serialization
if kw.get('serializer', 'json') in ('json', 'yaml'):
for k, v in message.items():
if isinstance(v, ObjectId):
message[k] = str(v)
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: # pragma no cover
return
log.exception('''Failure publishing message:
xn : %r
routing_key: %r
data : %r
''', xn, routing_key, message)
def url(self, base, **kw):
params = urlencode(kw)
if params:
return '%s://%s%s?%s' % (request.scheme, request.host, base, params)
else:
return '%s://%s%s' % (request.scheme, request.host, base)
class MockAMQ(object):
def __init__(self):
self.exchanges = defaultdict(list)
self.queue_bindings = defaultdict(list)
def publish(self, xn, message, routing_key, **kw):
self.exchanges[xn].append(
dict(routing_key=routing_key, message=message, kw=kw))
def pop(self, xn):
return self.exchanges[xn].pop(0)
def setup_handlers(self):
from pyforge.command.reactor import plugin_consumers, ReactorCommand
self.plugins = [
(ep.name, ep.load()) for ep in pkg_resources.iter_entry_points('pyforge') ]
self.reactor = ReactorCommand()
for name, plugin in self.plugins:
for method, xn, qn, keys in plugin_consumers(name, plugin):
for k in keys:
self.queue_bindings[xn].append(
dict(key=k, plugin_name=name, method=method))
self.setup_plugin(name, plugin)
def handle(self, xn):
msg = self.pop(xn)
for handler in self.queue_bindings[xn]:
if self._route_matches(handler['key'], msg['routing_key']):
self._route(xn, msg, handler['plugin_name'], handler['method'])
def _route(self, xn, msg, plugin_name, method):
if xn == 'audit':
callback = self.reactor.route_audit(plugin_name, method)
else:
callback = self.reactor.route_react(plugin_name, method)
data = msg['message']
msg = mock.Mock()
msg.delivery_info['routing_key'] = msg['routing_key']
return callback(msg, data)
def _route_matches(self, pattern, key):
re_pattern = (pattern
.replace('.', r'\.')
.replace('*', r'(?:\w+)')
.replace('#', r'(?:\w+)(?:\.\w+)*'))
return re.match(re_pattern+'$', key)