"""'A La Mode' is a helper module for web apps. This is version 1.0.16. See alamode.html for more info. LICENSE ------- This work, including the source code, documentation and related data, is placed into the public domain. The original author is Robert Brewer. fumanchu@aminus.org http://projects.amor.org/misc/wiki/Alamode THIS SOFTWARE IS PROVIDED AS-IS, WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR REDISTRIBUTION OF THIS SOFTWARE. """ __all__ = ['Adapter', 'AdapterFromHTML', 'AdapterToHTML', 'checked', 'selected', 'coerce_in', 'coerce_out', 'escape_url', 'escape_spaces', 'unescape_spaces', 'from_html', 'to_html', 'inttuple', 'strtuple', 'parse_date', 'sane_year', 'quote', 'urljoin', 'today', 'now', ] import datetime today = datetime.date.today now = datetime.datetime.now import sys, traceback # Quoting and escaping from xml.sax.saxutils import quoteattr as quote control = "".join(map(chr, xrange(32))) reserved = ";/?:@&=+$," unwise = '<>#%"' def escape_url(url, escape_unwise=True): """Replace excluded URL characters with hex-encoded (%XX).""" chars = control + reserved + " " if escape_unwise: chars += unwise for char in chars: url = url.replace(char, hex(ord(char)).replace("0x", "%")) return url def urljoin(*atoms): """urljoin(*atoms) -> Join path atoms with / to form a complete url.""" return u"/".join([x.strip(r"\/") for x in atoms]) def selected(value): """If value is True, return a valid XHTML 'selected' attribute, else u''.""" if value: return u' selected="selected" ' return u'' def checked(value): """If value is True, return a valid XHTML 'checked' attribute, else u''.""" if value: return u' checked="checked" ' return u'' def escape_spaces(val, repl=' '): """escape_spaces(val, repl=' ') -> val, made safe for HTML forms. Replace leading and trailing spaces with  , to get around browsers like IE, Firefox and Lynx which strip leading and trailing spaces from form element values upon submit. See unescape_spaces() for the reverse transform. IE, Firefox: occurs with option elements (display only; if a separate value argument is given (