The unified diff between revisions [92eb8752..] and [14e5fc76..] is displayed below. It can also be downloaded as a raw diff.
This diff has been restricted to the following files: 'viewmtn.py'
#
#
# patch "viewmtn.py"
# from [a3a3446ad7ea43e0ff0ceb6daa04dac0b7719bae]
# to [6629b45724cc639d2555c41248ccadf35838b83e]
#
============================================================
--- viewmtn.py a3a3446ad7ea43e0ff0ceb6daa04dac0b7719bae
+++ viewmtn.py 6629b45724cc639d2555c41248ccadf35838b83e
@@ -11,21 +11,24 @@ from itertools import izip, chain, repea
import os, sys
from itertools import izip, chain, repeat
-# web.py
import web
-# Other bits of ViewMTN
import mtn, handlers
-# The user configuration file
+from urls import common_urls, perdb_urls
import config
-debug = web.debug
-
# purloined from: http://docs.python.org/lib/itertools-recipes.html
def grouper(n, iterable, padvalue=None):
"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
-class OperationsFactory(object):
+class RequestContext(object):
+ def __init__ (self, dbname, ops):
+ self.dbname, self.ops = dbname, ops
+ # make sure that any unread automate output is flushed away
+ if not ops is None:
+ ops.per_request()
+
+class RequestContextFactory(object):
def __init__ (self):
# has the user specified a dbfiles hash? if so, use it
self.ops_instances = {}
@@ -39,64 +42,14 @@ class OperationsFactory(object):
self.ops_instances["legacy"] = mtn.Operations([config.monotone, config.dbfile])
self.default = "legacy"
- def get_ops(self, name):
- ops = None
+ def __getitem__(self, name):
if name is None:
ops = self.ops_instances.get (self.default, None)
else:
name = name.rstrip('/')
ops = self.ops_instances.get (name, None)
- # technically it'd be better to do this before serving the
- # request, however this is about the only per-request
- # spot that runs for every handler..
- if not ops is None:
- ops.per_request()
- return ops
+ return RequestContext(name, ops)
-op_fact = OperationsFactory()
-
-common_urls = (
-# these don't care about multiple databases specified via the URL
- r'about', 'About',
- r'help', 'Help',
- r'robots.txt', 'RobotsTxt', ## FIXME needs o exclude per-db paths
- r'mimeicon/([A-Za-z0-9][a-z0-9\-\+\.]*)/([A-Za-z0-9][a-z0-9\-\+\.]*)', 'MimeIcon',
-)
-
-perdb_urls = (
- r'', 'Index',
- r'tags', 'Tags',
- r'json/([A-Za-z]+)/(.*)', 'Json',
-
- r'([a-zA-Z]/)?revision/browse/('+mtn.revision_re+')/(.*)', 'RevisionBrowse',
- r'revision/browse/('+mtn.revision_re+')()', 'RevisionBrowse',
- r'revision/diff/('+mtn.revision_re+')/with/('+mtn.revision_re+')', 'RevisionDiff',
- r'revision/diff/('+mtn.revision_re+')/with/('+mtn.revision_re+')'+'/(.*)', 'RevisionDiff',
- r'revision/rawdiff/('+mtn.revision_re+')/with/('+mtn.revision_re+')', 'RevisionRawDiff',
- r'revision/rawdiff/('+mtn.revision_re+')/with/('+mtn.revision_re+')'+'/(.*)', 'RevisionRawDiff',
- r'revision/file/('+mtn.revision_re+')/(.*)', 'RevisionFile',
- r'revision/filechanges/()()('+mtn.revision_re+')/(.*)', 'RevisionFileChanges',
- r'revision/filechanges/from/(\d+)/to/(\d+)/('+mtn.revision_re+')/(.*)', 'RevisionFileChanges',
- r'revision/filechanges/rss/()()('+mtn.revision_re+')/(.*)', 'RevisionFileChangesRSS',
- r'revision/filechanges/rss/from/(\d+)/to/(\d+)/('+mtn.revision_re+')/(.*)', 'RevisionFileChangesRSS',
- r'revision/downloadfile/('+mtn.revision_re+')/(.*)', 'RevisionDownloadFile',
- r'revision/info/('+mtn.revision_re+')', 'RevisionInfo',
- r'revision/tar/('+mtn.revision_re+')', 'RevisionTar',
- r'revision/graph/('+mtn.revision_re+')', 'RevisionGraph',
-
- r'branch/changes/(.*)/from/(\d+)/to/(\d+)', 'HTMLBranchChanges',
- r'branch/changes/([^/]+)()()', 'HTMLBranchChanges',
- r'branch/changes/(.*)/from/(\d+)/to/(\d+)/rss', 'RSSBranchChanges',
- r'branch/changes/([^/]+)()()/rss', 'RSSBranchChanges',
- r'branch/tags/([^/]+)', 'Tags',
-
- # let's make it possible to access any function on the head revision
- # through this proxy method; it'll return a redirect to the head revision
- # with the specified function
- r'branch/(head)/([A-Za-z]+)/([^/]+)(.*)', 'BranchHead',
- r'branch/(anyhead)/([A-Za-z]+)/([^/]+)(.*)', 'BranchHead',
-)
-
def runfcgi_apache(func):
web.wsgi.runfcgi(func, None)
@@ -112,7 +65,9 @@ if __name__ == '__main__':
def assemble_urls():
fvars = {}
urls = ()
-
+
+ factory = RequestContextFactory()
+
for url, fn in grouper (2, common_urls):
url = r'^/' + url
if hasattr(handlers, fn):
@@ -125,7 +80,7 @@ if __name__ == '__main__':
class PerDBClosure(object):
def GET (self, *args, **kwargs):
db, other_args = args[0], args[1:]
- ops = op_fact.get_ops (db)
+ ops = factory[db]
if ops is None:
return web.notfound()
return handler.GET (ops, *other_args, **kwargs)