imip-agent

Annotated imipweb/env.py

1053:068aa85f0c45
2016-02-08 Paul Boddie Made the retraction operation a complete transaction. Tidied up the locking and unlocking function application.
paul@440 1
#!/usr/bin/env python
paul@440 2
paul@440 3
"""
paul@440 4
Web interface utilities.
paul@440 5
paul@440 6
Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
paul@440 7
paul@440 8
This program is free software; you can redistribute it and/or modify it under
paul@440 9
the terms of the GNU General Public License as published by the Free Software
paul@440 10
Foundation; either version 3 of the License, or (at your option) any later
paul@440 11
version.
paul@440 12
paul@440 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@440 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@440 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@440 16
details.
paul@440 17
paul@440 18
You should have received a copy of the GNU General Public License along with
paul@440 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@440 20
"""
paul@440 21
paul@777 22
import cgi, os, sys, urlparse
paul@440 23
paul@440 24
getenv = os.environ.get
paul@440 25
setenv = os.environ.__setitem__
paul@440 26
paul@440 27
class CGIEnvironment:
paul@440 28
paul@440 29
    "A CGI-compatible environment."
paul@440 30
paul@440 31
    def __init__(self, charset=None):
paul@440 32
        self.charset = charset
paul@440 33
        self.args = None
paul@440 34
        self.method = None
paul@440 35
        self.path = None
paul@440 36
        self.path_info = None
paul@440 37
        self.user = None
paul@777 38
        self.query_string = None
paul@440 39
paul@440 40
    def get_args(self):
paul@440 41
        if self.args is None:
paul@440 42
            if self.get_method() != "POST":
paul@777 43
                if not self.query_string:
paul@777 44
                    self.query_string = getenv("QUERY_STRING")
paul@440 45
                setenv("QUERY_STRING", "")
paul@440 46
            args = cgi.parse(keep_blank_values=True)
paul@440 47
paul@440 48
            if not self.charset:
paul@440 49
                self.args = args
paul@440 50
            else:
paul@440 51
                self.args = {}
paul@440 52
                for key, values in args.items():
paul@440 53
                    self.args[key] = [unicode(value, self.charset) for value in values]
paul@440 54
paul@440 55
        return self.args
paul@440 56
paul@777 57
    def get_query(self):
paul@777 58
        if not self.query_string:
paul@777 59
            self.query_string = getenv("QUERY_STRING")
paul@777 60
        return urlparse.parse_qs(self.query_string or "", keep_blank_values=True)
paul@777 61
paul@440 62
    def get_method(self):
paul@440 63
        if self.method is None:
paul@440 64
            self.method = getenv("REQUEST_METHOD") or "GET"
paul@440 65
        return self.method
paul@440 66
paul@440 67
    def get_path(self):
paul@440 68
        if self.path is None:
paul@440 69
            self.path = getenv("SCRIPT_NAME") or ""
paul@440 70
        return self.path
paul@440 71
paul@440 72
    def get_path_info(self):
paul@440 73
        if self.path_info is None:
paul@440 74
            self.path_info = getenv("PATH_INFO") or ""
paul@440 75
        return self.path_info
paul@440 76
paul@440 77
    def get_user(self):
paul@440 78
        if self.user is None:
paul@440 79
            self.user = getenv("REMOTE_USER") or ""
paul@440 80
        return self.user
paul@440 81
paul@440 82
    def get_output(self):
paul@440 83
        return sys.stdout
paul@440 84
paul@440 85
    def get_url(self):
paul@440 86
        path = self.get_path()
paul@440 87
        path_info = self.get_path_info()
paul@440 88
        return "%s%s" % (path.rstrip("/"), path_info)
paul@440 89
paul@440 90
    def new_url(self, path_info):
paul@440 91
        path = self.get_path()
paul@440 92
        return "%s/%s" % (path.rstrip("/"), path_info.lstrip("/"))
paul@440 93
paul@440 94
# vim: tabstop=4 expandtab shiftwidth=4