]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/common.py
8f97dde50a16b42b31c86a9ec94c6747dfa44295
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / common.py
1
2 from zope.interface import Interface
3 from nevow import loaders
4 from nevow.util import resource_filename
5
6 class IClient(Interface):
7     pass
8
9
10 def getxmlfile(name):
11     return loaders.xmlfile(resource_filename('allmydata.web', '%s' % name))
12
13 def boolean_of_arg(arg):
14     assert arg.lower() in ("true", "t", "1", "false", "f", "0", "on", "off")
15     return arg.lower() in ("true", "t", "1", "on")
16
17 def get_arg(req, argname, default=None, multiple=False):
18     """Extract an argument from either the query args (req.args) or the form
19     body fields (req.fields). If multiple=False, this returns a single value
20     (or the default, which defaults to None), and the query args take
21     precedence. If multiple=True, this returns a tuple of arguments (possibly
22     empty), starting with all those in the query args.
23     """
24     results = []
25     if argname in req.args:
26         results.extend(req.args[argname])
27     if req.fields and argname in req.fields:
28         results.append(req.fields[argname].value)
29     if multiple:
30         return tuple(results)
31     if results:
32         return results[0]
33     return default