File: cgiapp.py

package info (click to toggle)
htmlgen 2.2.2-12.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,840 kB
  • ctags: 1,480
  • sloc: python: 4,518; makefile: 93
file content (126 lines) | stat: -rw-r--r-- 3,757 bytes parent folder | download | duplicates (8)
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
__version__ = '$Id: cgiapp.py,v 1.1 1998/03/19 18:57:03 friedric Exp $'
from HTMLgen import *
from HTMLcolors import *
from HTMLutil import *
from types import ListType
import cgi, sys, tempfile, traceback

def _fstodict(fs):
    d = {}
    for tok in fs.keys():
        alist = []
        if type(fs[tok]) == ListType:
            for val in fs[tok]:
                if val.value: alist.append(val.value)
        elif fs[tok].file:
            alist = (fs[tok].filename, fs[tok].file)
        else:
            if fs[tok].value:
                alist = fs[tok].value
        if alist: d[tok] = alist
    return d

class CGI(Document):
    def __init__(self, **kw):
        Document.__init__(self)
        self._tempfile = tempfile.mktemp()

        self.active = 1
        self.logfile = '/home/httpd/logs/cgi_log'
        self.script_url = ''
        self.source = ''

        self.valid = {'_no_function': _no_function}
        self.function = '_no_function'

        self.form = {}
        self.form_ok = 1
        self.form_defaults = {}
        self.form_errors = {}
        self.form_messages = []

        self.http = { 'Content-type': 'text/html' }
        for item in kw.keys():
            if self.__dict__.has_key(item):
                self.__dict__[item] = kw[item]
            else:
                raise KeyError,\
                        `item`+' not a valid parameter of the CGI class'

    def get_form(self):
        self.form = _fstodict(cgi.FieldStorage())
        if self.form.has_key('function'):
            f = self.form['function']
            if f in self.valid.keys():
                self.function = f

    def verify(self):
        for tok in self.form_defaults.keys():
            if not self.form.has_key(tok):
                self.form[tok] = self.form_defaults[tok]
        for tok in self.form_errors.keys():
            if not form.has_key(tok):
                self.form_messages.append(self.form_errors[tok])
                self.form_ok = 0

    def run(self, cont=0):
        # Print HTTP messages
        for key in self.http.keys():
            print '%s: %s' % (key, self.http[key])
        print

        # Redirect stderr
        sys.stderr = open(self._tempfile, 'w')

        # Grab query string
        self.get_form()

        # Function handling
        if not self.active:
            ret = _not_active(self)
            print self
            sys.exit(0)
        elif not '_no_function' in self.valid.keys():
            self.valid['_no_function'] = _no_function
        if not self.function or self.function not in self.valid.keys():
            self.function = '_no_function'
        try:
            ret = self.valid[self.function](self)
        except:
            traceback.print_exc()
            sys.stderr.flush()
            f = open(self._tempfile, 'r')
            self.title = 'CGI Error Occured'
            self.append(Pre(f.read()))
            f.close()

        # Print Document object
        print self
        if not cont:
            sys.exit(0) # Provide a speedy exit

class MinimalCGI(MinimalDocument, CGI):
    __init__ = CGI.__init__

# For backward compatibility
CGIApp = CGI

def _not_active(cgi):
    cgi.title = 'Script Inactive!'
    cgi.subtitle = 'Please Try Later'
    cgi.append(Paragraph("""
            This script is being service or is otherwise unavailable.
            Please try again later or contact the author with any
            questions or comments.
            """))
    return 0

def _no_function(cgi):
    cgi.title = 'Error!'
    cgi.subtitle = 'No Action'
    cgi.append(Paragraph("""
            No action was defined or the action was not
            in the list of valid actions. Please send
            feedback to the author."""))
    return 0