source: python/aubio/web/browser.py @ e47ade3

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since e47ade3 was 5140276, checked in by Paul Brossier <piem@altern.org>, 19 years ago

added aubioweb
added aubioweb

  • Property mode set to 100644
File size: 5.7 KB
Line 
1 #
2 # Copyright 2004 Apache Software Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you
5 # may not use this file except in compliance with the License.  You
6 # may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.  See the License for the specific language governing
14 # permissions and limitations under the License.
15 #
16 # Originally developed by Gregory Trubetskoy.
17 #
18 # $Id: publisher.py,v 1.36 2004/02/16 19:47:27 grisha Exp $
19
20"""
21  This handler is conceputally similar to Zope's ZPublisher, except
22  that it:
23
24  1. Is written specifically for mod_python and is therefore much faster
25  2. Does not require objects to have a documentation string
26  3. Passes all arguments as simply string
27  4. Does not try to match Python errors to HTTP errors
28  5. Does not give special meaning to '.' and '..'.
29
30  This is a modified version of mod_python.publisher.handler Only the first
31  directory argument is matched, the rest is left for path_info. A default
32  one must be provided.
33
34"""
35
36from mod_python import apache
37from mod_python import util
38from mod_python.publisher import resolve_object,process_auth,imp_suffixes
39
40import sys
41import os
42import re
43
44from types import *
45
46def configure_handler(req,default):
47
48    req.allow_methods(["GET", "POST"])
49    if req.method not in ["GET", "POST"]:
50        raise apache.SERVER_RETURN, apache.HTTP_METHOD_NOT_ALLOWED
51
52    func_path = ""
53    if req.path_info:
54        func_path = req.path_info[1:] # skip first /
55        #func_path = func_path.replace("/", ".")
56        #if func_path[-1:] == ".":
57        #    func_path = func_path[:-1]
58        # changed: only keep the first directory
59        func_path = re.sub('/.*','',func_path)
60
61    # default to 'index' if no path_info was given
62    if not func_path:
63        func_path = "index"
64
65    # if any part of the path begins with "_", abort
66    if func_path[0] == '_' or func_path.count("._"):
67        raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
68
69    ## import the script
70    path, module_name =  os.path.split(req.filename)
71    if not module_name:
72        module_name = "index"
73
74    # get rid of the suffix
75    #   explanation: Suffixes that will get stripped off
76    #   are those that were specified as an argument to the
77    #   AddHandler directive. Everything else will be considered
78    #   a package.module rather than module.suffix
79    exts = req.get_addhandler_exts()
80    if not exts:
81        # this is SetHandler, make an exception for Python suffixes
82        exts = imp_suffixes
83    if req.extension:  # this exists if we're running in a | .ext handler
84        exts += req.extension[1:] 
85    if exts:
86        suffixes = exts.strip().split()
87        exp = "\\." + "$|\\.".join(suffixes)
88        suff_matcher = re.compile(exp) # python caches these, so its fast
89        module_name = suff_matcher.sub("", module_name)
90
91    # import module (or reload if needed)
92    # the [path] argument tells import_module not to allow modules whose
93    # full path is not in [path] or below.
94    config = req.get_config()
95    autoreload=int(config.get("PythonAutoReload", 1))
96    log=int(config.get("PythonDebug", 0))
97    try:
98        module = apache.import_module(module_name,
99                                      autoreload=autoreload,
100                                      log=log,
101                                      path=[path])
102    except ImportError:
103        et, ev, etb = sys.exc_info()
104        # try again, using default module, perhaps this is a
105        # /directory/function (as opposed to /directory/module/function)
106        func_path = module_name
107        module_name = "index"
108        try:
109            module = apache.import_module(module_name,
110                                          autoreload=autoreload,
111                                          log=log,
112                                          path=[path])
113        except ImportError:
114            # raise the original exception
115            raise et, ev, etb
116       
117    # does it have an __auth__?
118    realm, user, passwd = process_auth(req, module)
119
120    # resolve the object ('traverse')
121    try:
122        object = resolve_object(req, module, func_path, realm, user, passwd)
123    except AttributeError:
124        # changed, return the default path instead
125        #raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
126        object = default
127    # not callable, a class or an unbound method
128    if (not callable(object) or 
129        type(object) is ClassType or
130        (hasattr(object, 'im_self') and not object.im_self)):
131
132        result = str(object)
133       
134    else:
135        # callable, (but not a class or unbound method)
136       
137        # process input, if any
138        req.form = util.FieldStorage(req, keep_blank_values=1)
139       
140        result = util.apply_fs_data(object, req.form, req=req)
141
142    if result or req.bytes_sent > 0 or req.next:
143       
144        if result is None:
145            result = ""
146        else:
147            result = str(result)
148
149        # unless content_type was manually set, we will attempt
150        # to guess it
151        if not req._content_type_set:
152            # make an attempt to guess content-type
153            if result[:100].strip()[:6].lower() == '<html>' \
154               or result.find('</') > 0:
155                req.content_type = 'text/html'
156            else:
157                req.content_type = 'text/plain'
158
159        if req.method != "HEAD":
160            req.write(result)
161        else:
162            req.write("")
163        return apache.OK
164    else:
165        req.log_error("mod_python.publisher: %s returned nothing." % `object`)
166        return apache.HTTP_INTERNAL_SERVER_ERROR
167
Note: See TracBrowser for help on using the repository browser.