Changeset 3295

Show
Ignore:
Timestamp:
01/21/08 21:00:58 (16 years ago)
Author:
jerome
Message:

Made the CGI scripts work again.
Moved even more functions to the utils module.
Removed the cgifuncs module, moved (and changed) content into utils.
If no output encoding defined, use UTF-8 : when wget is used to try
the CGI scripts, it doesn't set by default the accepted charset and
language headers.

Location:
pykota/trunk
Files:
1 removed
23 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/autopykota

    r3294 r3295  
    3030 
    3131from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    32 from pykota.tool import PyKotaTool, crashed, N_ 
     32from pykota.tool import PyKotaTool 
    3333 
    3434__doc__ = N_("""autopykota v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/bin/cupspykota

    r3294 r3295  
    4848 
    4949from pykota.errors import PyKotaToolError 
    50 from pykota.tool import PyKotaTool, crashed 
     50from pykota.tool import PyKotaTool 
    5151from pykota.accounter import openAccounter 
    5252from pykota import cups 
  • pykota/trunk/bin/dumpykota

    r3294 r3295  
    2828 
    2929from pykota.errors import PyKotaCommandLineError 
    30 from pykota.tool import crashed, N_ 
    3130from pykota.dumper import DumPyKota 
    3231 
  • pykota/trunk/bin/edpykota

    r3294 r3295  
    2828 
    2929from pykota.errors import PyKotaCommandLineError 
    30 from pykota.tool import Percent, PyKotaTool, crashed, N_ 
     30from pykota.tool import Percent, PyKotaTool 
    3131from pykota.storage import StorageUserPQuota, StorageGroupPQuota 
    3232 
  • pykota/trunk/bin/pkbanner

    r3294 r3295  
    4848 
    4949from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    50 from pykota.tool import Tool, crashed, N_ 
     50from pykota.tool import Tool 
    5151 
    5252__doc__ = N_("""pkbanner v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/bin/pkbcodes

    r3294 r3295  
    3030 
    3131from pykota.errors import PyKotaCommandLineError 
    32 from pykota.tool import Percent, PyKotaTool, crashed, N_ 
     32from pykota.tool import Percent, PyKotaTool 
    3333from pykota.storage import StorageBillingCode 
    3434 
  • pykota/trunk/bin/pkinvoice

    r3294 r3295  
    4848 
    4949from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    50 from pykota.tool import Percent, PyKotaTool, crashed, N_ 
     50from pykota.tool import Percent, PyKotaTool 
    5151 
    5252__doc__ = N_("""pkinvoice v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/bin/pkmail

    r3294 r3295  
    3535 
    3636from pykota.errors import PyKotaCommandLineError 
    37 from pykota.tool import PyKotaTool, crashed, N_ 
     37from pykota.tool import PyKotaTool 
    3838     
    3939__doc__ = N_("""pkmail v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/bin/pknotify

    r3294 r3295  
    3939 
    4040from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    41 from pykota.tool import Tool, crashed, N_ 
     41from pykota.tool import Tool 
    4242 
    4343__doc__ = N_("""pknotify v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/bin/pkprinters

    r3294 r3295  
    3030 
    3131from pykota.errors import PyKotaCommandLineError 
    32 from pykota.tool import Percent, PyKotaTool, crashed, N_ 
     32from pykota.tool import Percent, PyKotaTool 
    3333from pykota.storage import StoragePrinter 
    3434 
  • pykota/trunk/bin/pkrefund

    r3294 r3295  
    4848 
    4949from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    50 from pykota.tool import Percent, PyKotaTool, crashed, N_ 
     50from pykota.tool import Percent, PyKotaTool 
    5151 
    5252__doc__ = N_("""pkrefund v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/bin/pkturnkey

    r3294 r3295  
    3333 
    3434from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    35 from pykota.tool import Tool, crashed, N_ 
     35from pykota.tool import Tool 
    3636 
    3737__doc__ = N_("""pkturnkey v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/bin/pkusers

    r3294 r3295  
    3030 
    3131from pykota.errors import PyKotaCommandLineError 
    32 from pykota.tool import Percent, PyKotaTool, crashed, N_ 
     32from pykota.tool import Percent, PyKotaTool 
    3333from pykota.storage import StorageUser, StorageGroup 
    3434 
  • pykota/trunk/bin/pykosd

    r3294 r3295  
    3737 
    3838from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    39 from pykota.tool import PyKotaTool, crashed, N_ 
     39from pykota.tool import PyKotaTool 
    4040 
    4141__doc__ = N_("""pykosd v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/bin/pykotme

    r3294 r3295  
    3030 
    3131from pykota.errors import PyKotaCommandLineError 
    32 from pykota.tool import PyKotaTool, crashed, N_ 
     32from pykota.tool import PyKotaTool 
    3333from pykota.accounter import openAccounter 
    3434     
  • pykota/trunk/bin/repykota

    r3294 r3295  
    3232 
    3333from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    34 from pykota.tool import PyKotaTool, crashed, N_ 
     34from pykota.tool import PyKotaTool 
    3535from pykota import reporter 
    3636 
  • pykota/trunk/bin/warnpykota

    r3294 r3295  
    3030 
    3131from pykota.errors import PyKotaCommandLineError 
    32 from pykota.tool import PyKotaTool, crashed, N_ 
     32from pykota.tool import PyKotaTool 
    3333 
    3434__doc__ = N_("""warnpykota v%(__version__)s (c) %(__years__)s %(__author__)s 
  • pykota/trunk/cgi-bin/dumpykota.cgi

    r3275 r3295  
    2929import urllib 
    3030 
    31 from pykota import version 
    32 from pykota.tool import PyKotaToolError 
     31import pykota.appinit 
     32from pykota import version, utils 
    3333from pykota.dumper import DumPyKota 
    34 from pykota.cgifuncs import getLanguagePreference, getCharsetPreference 
    3534 
    3635header = """Content-type: text/html;charset=%s 
     
    6867  </head> 
    6968  <body> 
    70     <!-- %s %s --> 
    7169    <p> 
    7270      <form action="dumpykota.cgi" method="GET" name="mainform" onsubmit="return checkvalues()"> 
     
    123121        """Displays the dumper interface.""" 
    124122        global header, footer 
    125         print header % (self.charset, _("PyKota Data Dumper"), \ 
    126                         self.language, self.charset, \ 
     123        content = [ header % (self.charset, _("PyKota Data Dumper"), \ 
    127124                        self.config.getLogoLink(), \ 
    128125                        self.config.getLogoURL(), version.__version__, \ 
    129126                        self.config.getLogoLink(), \ 
    130127                        version.__version__, _("PyKota Data Dumper"), \ 
    131                         _("Dump"), _("Please click on the above button")) 
    132         print self.htmlListDataTypes(self.options.get("data", ""))  
    133         print "<br />" 
    134         print self.htmlListFormats(self.options.get("format", "")) 
    135         print "<br />" 
    136         print self.htmlFilterInput(" ".join(self.arguments)) 
    137         print "<br />" 
    138         print self.htmlOrderbyInput(self.options.get("orderby", "")) 
    139         print "<br />" 
    140         print self.htmlSumCheckbox(self.options.get("sum", "")) 
    141         print footer % (_("Dump"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__) 
     128                        _("Dump"), _("Please click on the above button")) ] 
     129        content.append(self.htmlListDataTypes(self.options.get("data", ""))) 
     130        content.append(u"<br />") 
     131        content.append(self.htmlListFormats(self.options.get("format", ""))) 
     132        content.append(u"<br />") 
     133        content.append(self.htmlFilterInput(" ".join(self.arguments))) 
     134        content.append(u"<br />") 
     135        content.append(self.htmlOrderbyInput(self.options.get("orderby", ""))) 
     136        content.append(u"<br />") 
     137        content.append(self.htmlSumCheckbox(self.options.get("sum", ""))) 
     138        content.append(footer % (_("Dump"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__)) 
     139        for c in content : 
     140            sys.stdout.write(c.encode(self.charset, "replace")) 
     141        sys.stdout.flush() 
    142142         
    143143    def htmlListDataTypes(self, selected="") :     
     
    236236             
    237237if __name__ == "__main__" : 
    238     admin = PyKotaDumperGUI(lang=getLanguagePreference(), charset=getCharsetPreference()) 
     238    utils.reinitcgilocale() 
     239    admin = PyKotaDumperGUI() 
    239240    admin.deferredInit() 
    240241    admin.form = cgi.FieldStorage() 
  • pykota/trunk/cgi-bin/printquota.cgi

    r3275 r3295  
    3131from mx import DateTime 
    3232 
    33 from pykota import version 
    34 from pykota.tool import PyKotaTool, PyKotaToolError 
    35 from pykota.reporter import PyKotaReporterError, openReporter 
    36 from pykota.cgifuncs import getLanguagePreference, getCharsetPreference 
     33import pykota.appinit 
     34 
     35from pykota import version, utils 
     36from pykota.tool import PyKotaTool 
     37from pykota.errors import PyKotaToolError, PyKotaReporterError 
     38from pykota.reporter import openReporter 
    3739 
    3840header = """Content-type: text/html;charset=%s 
     
    4446  </head> 
    4547  <body> 
    46     <!-- %s %s --> 
    4748    <p> 
    4849      <form action="printquota.cgi" method="POST"> 
     
    9697        """Displays the administrative interface.""" 
    9798        global header, footer 
    98         print header % (self.charset, _("PyKota Reports"), \ 
    99                         self.language, self.charset, \ 
    100                         self.config.getLogoLink(), \ 
    101                         self.config.getLogoURL(), version.__version__, \ 
    102                         self.config.getLogoLink(), \ 
    103                         version.__version__, _("PyKota Reports"), \ 
    104                         _("Report")) 
    105         print self.body 
    106         print footer % (_("Report"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__) 
     99        content = [ header % (self.charset, _("PyKota Reports"), \ 
     100                              self.config.getLogoLink(), \ 
     101                              self.config.getLogoURL(), version.__version__, \ 
     102                              self.config.getLogoLink(), \ 
     103                              version.__version__, _("PyKota Reports"), \ 
     104                              _("Report")) ] 
     105        content.append(self.body)                       
     106        content.append(footer % (_("Report"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__)) 
     107        for c in content : 
     108            sys.stdout.write(c.encode(self.charset, "replace")) 
     109        sys.stdout.flush() 
    107110         
    108111    def error(self, message) : 
     
    287290             
    288291if __name__ == "__main__" : 
    289     admin = PyKotaReportGUI(lang=getLanguagePreference(), charset=getCharsetPreference()) 
     292    utils.reinitcgilocale() 
     293    admin = PyKotaReportGUI() 
    290294    admin.deferredInit() 
    291295    admin.form = cgi.FieldStorage() 
  • pykota/trunk/cgi-bin/pykotme.cgi

    r3275 r3295  
    3030import cStringIO 
    3131 
    32 from pykota import version 
    33 from pykota.tool import PyKotaTool, PyKotaToolError 
    34 from pykota.cgifuncs import getLanguagePreference, getCharsetPreference 
     32import pykota.appinit 
     33 
     34from pykota import version, utils 
     35from pykota.tool import PyKotaTool 
     36from pykota.errors import PyKotaToolError 
     37 
    3538from pkpgpdls import analyzer, pdlparser 
    3639     
     
    4447  </head> 
    4548  <body> 
    46     <!-- %s %s --> 
    4749    <p> 
    4850      <form action="pykotme.cgi" method="POST" enctype="multipart/form-data"> 
     
    9698        """Displays the administrative interface.""" 
    9799        global header, footer 
    98         print header % (self.charset, _("PyKota Quotes"), \ 
    99                         self.language, self.charset, \ 
     100        content = [ header % (self.charset, _("PyKota Quotes"), \ 
    100101                        self.config.getLogoLink(), \ 
    101102                        self.config.getLogoURL(), version.__version__, \ 
    102103                        self.config.getLogoLink(), \ 
    103104                        version.__version__, _("PyKota Quotes"), \ 
    104                         _("Quote")) 
    105         print self.body 
    106         print footer % (_("Quote"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__) 
     105                        _("Quote")) ] 
     106        content.append(self.body) 
     107        content.append(footer % (_("Quote"), version.__doc__, version.__years__, version.__author__, version.__gplblurb__)) 
     108        for c in content : 
     109            sys.stdout.write(c.encode(self.charset, "replace")) 
     110        sys.stdout.flush() 
    107111         
    108112    def error(self, message) : 
     
    187191             
    188192if __name__ == "__main__" : 
    189     admin = PyKotMeGUI(lang=getLanguagePreference(), charset=getCharsetPreference()) 
     193    utils.reinitcgilocale() 
     194    admin = PyKotMeGUI() 
    190195    admin.deferredInit() 
    191196    admin.form = cgi.FieldStorage() 
  • pykota/trunk/pykota/dumper.py

    r3291 r3295  
    3939    hasJAXML = True 
    4040 
     41from pykota.utils import * 
     42 
    4143from pykota import version 
    42 from pykota.tool import PyKotaTool, N_ 
     44from pykota.tool import PyKotaTool 
    4345from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    4446 
  • pykota/trunk/pykota/tool.py

    r3294 r3295  
    3838from mx import DateTime 
    3939 
     40from pykota import utils 
    4041from pykota.errors import PyKotaCommandLineError 
    4142from pykota import config, storage, logger 
    4243from pykota.version import __version__, __author__, __years__, __gplblurb__ 
    43  
    44 def N_(message) : 
    45     """Fake translation marker for translatable strings extraction.""" 
    46     return message 
    47  
    48 def crashed(message="Bug in PyKota") :     
    49     """Minimal crash method.""" 
    50     import traceback 
    51     lines = [] 
    52     for line in traceback.format_exception(*sys.exc_info()) : 
    53         lines.extend([l for l in line.split("\n") if l]) 
    54     msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)]) 
    55     sys.stderr.write(msg) 
    56     sys.stderr.flush() 
    57     return msg 
    5844 
    5945class Percent : 
     
    10389class Tool : 
    10490    """Base class for tools with no database access.""" 
    105     def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : 
     91    def __init__(self, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : 
    10692        """Initializes the command line tool.""" 
    10793        self.debug = True # in case of early failure 
     
    11096        # Saves a copy of the locale settings 
    11197        (self.language, self.charset) = locale.getlocale() 
     98        if not self.charset : 
     99            self.charset = "UTF-8" 
    112100         
    113101        # pykota specific stuff 
     
    247235    def crashed(self, message="Bug in PyKota") :     
    248236        """Outputs a crash message, and optionally sends it to software author.""" 
    249         msg = crashed(message) 
     237        msg = utils.crashed(message) 
    250238        fullmessage = "========== Traceback :\n\n%s\n\n========== sys.argv :\n\n%s\n\n========== Environment :\n\n%s\n" % \ 
    251239                        (msg, \ 
     
    347335class PyKotaTool(Tool) :     
    348336    """Base class for all PyKota command line tools.""" 
    349     def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : 
    350         """Initializes the command line tool and opens the database.""" 
    351         Tool.__init__(self, lang, charset, doc) 
    352          
    353337    def deferredInit(self) :     
    354338        """Deferred initialization.""" 
  • pykota/trunk/pykota/utils.py

    r3294 r3295  
    2323 
    2424import sys 
     25import os 
    2526import locale 
    2627import gettext 
     
    6364    except : 
    6465        gettext.NullTranslations().install(unicode=True) 
     66         
     67def getpreferredlanguage() : 
     68    """Returns the preferred language.""" 
     69    languages = os.environ.get("HTTP_ACCEPT_LANGUAGE", "") 
     70    langs = [l.strip().split(';')[0] for l in languages.split(",")] 
     71    return langs[0].replace("-", "_") 
     72     
     73def getpreferredcharset() : 
     74    """Returns the preferred charset.""" 
     75    charsets = os.environ.get("HTTP_ACCEPT_CHARSET", "UTF-8") 
     76    charsets = [l.strip().split(';')[0] for l in charsets.split(",")] 
     77    return charsets[0] 
     78 
     79def reinitcgilocale() :         
     80    """Reinitializes the locale and gettext translations for CGI scripts, according to browser's preferences.""" 
     81    initgettext(*initlocale(getpreferredlanguage(), getpreferredcharset())) 
     82     
     83def N_(message) : 
     84    """Fake translation marker for translatable strings extraction.""" 
     85    return message 
    6586 
    6687def databaseToUnicode(text) : 
     
    80101def logerr(text) : 
    81102    """Logs an unicode text to stderr.""" 
    82     sys.stderr.write(text.encode(sys.stdout.encoding, "replace")) 
     103    sys.stderr.write(text.encode(sys.stdout.encoding or locale.getlocale()[1], \ 
     104                                 "replace")) 
     105    sys.stderr.flush() 
    83106             
     107def crashed(message="Bug in PyKota") :     
     108    """Minimal crash method.""" 
     109    import traceback 
     110    from pykota.version import __version__ 
     111    lines = [] 
     112    for line in traceback.format_exception(*sys.exc_info()) : 
     113        lines.extend([l for l in line.split("\n") if l]) 
     114    msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)]) 
     115    logerr(msg) 
     116    return msg