From: BenoƮt Pin Date: Sun, 7 Nov 2010 02:14:06 +0000 (+0100) Subject: Importation initiale. X-Git-Url: https://scm.cri.ensmp.fr/git/ZTUtils_hotfix.git/commitdiff_plain/65a58efbf6098c007eac625011cef38abeb2a9c0?ds=inline Importation initiale. --- 65a58efbf6098c007eac625011cef38abeb2a9c0 diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..04c657e --- /dev/null +++ b/README.txt @@ -0,0 +1,26 @@ +Overview + Marshalling hotfix for ZTUtils + +Installation + + To install the hotfix, unpack the tarball file into the + 'Products' directory of your site's INSTANCE_HOME, and then restart + your Zope application server. + + E.g., if on your system, the Zope software is installed in + '/opt/lib/zope2.7', and your instance is in '/var/lib/zope':: + + # cd /var/lib/zope/Products + # tar xzf /tmp/ZTUtils_hotfix.tar.gz + # ../bin/zopectl restart + +Removeal + + To remove the hotfix, simply remove the product folder + and restart the application server. + + E.g., for the same setup:: + + # cd /var/lib/zope/Products + # rm -r ZTUtils_hotfix + # ../bin/zopectl restart diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..b9cbc1b --- /dev/null +++ b/__init__.py @@ -0,0 +1,139 @@ +""" +This hotfix patch ZTUtils.Zope.make_query and make_hidden_input +to make a recursive marshal from a REQUEST.form data. + +pinbe 2004/09/23 +""" + +import urllib, cgi +from DateTime import DateTime + +def make_query(*args, **kwargs): + '''Construct a URL query string, with marshalling markup. + + If there are positional arguments, they must be dictionaries. + They are combined with the dictionary of keyword arguments to form + a dictionary of query names and values. + + Query names (the keys) must be strings. Values may be strings, + integers, floats, or DateTimes, and they may also be lists or + namespaces containing these types. Names and string values + should not be URL-quoted. All arguments are marshalled with + complex_marshal(). + ''' + d = {} + for arg in args: + d.update(arg) + d.update(kwargs) + + qlist = [] + uq = urllib.quote + for k, v in d.items() : + for field in recurse_marshal(k, v) : + qlist.append( '%s%s=%s' % (uq(field['k']), field['m'], uq(str(field['v']))) ) + + return '&'.join(qlist) + +def make_hidden_input(*args, **kwargs): + '''Construct a set of hidden input elements, with marshalling markup. + + If there are positional arguments, they must be dictionaries. + They are combined with the dictionary of keyword arguments to form + a dictionary of query names and values. + + Query names (the keys) must be strings. Values may be strings, + integers, floats, or DateTimes, and they may also be lists or + namespaces containing these types. All arguments are marshalled with + complex_marshal(). + ''' + + d = {} + for arg in args: + d.update(arg) + d.update(kwargs) + + hq = cgi.escape + qlist = [] + for k, v in d.items() : + for field in recurse_marshal(k, v) : + qlist.append( ('' + % (hq(field['k']), field['m'], hq(str(field['v'])))) ) + + return '\n'.join(qlist) + + +def _simple_marshal(v) : # ! It's not a simple_marshal patch, the returned value is not the same. + + if isinstance(v, bool) : + m = ':boolean' + elif isinstance(v, int) : + m = ':int' + elif isinstance(v, float) : + m = ':float' + elif isinstance(v, long) : + m = ':long' + elif isinstance(v, DateTime) : + m = ':date' + elif isinstance(v, unicode) : + m = ':utf8:ustring' + v = v.encode('utf8') + elif hasattr(v, 'items') : + m = ':record' + elif isinstance(v, list) : + if len(v) > 0 and hasattr(v[0], 'items') : + m = ':records' + else : + m = ':list' + else : + m = '' + + return m, v + + +def recurse_marshal(k, v, nested='') : + + marshalList = [] + m, v = _simple_marshal(v) + n = nested + if m == ':list' : + n = ':list' + n + for sv in v : + if isinstance(sv, list) : # only strings into sv + white_space_found = False + for ssv in sv : + if ssv.find(' ') >=0 : white_space_found = True + if white_space_found : + sv = '\n'.join(sv) + insertM = ':lines' + else : + sv = ' '.join(sv) + insertM = ':tokens' + + if isinstance(sv, unicode) : + sv = sv.encode('utf8') + insertM = ':utf8:u' + insertM[1:] + marshalList.extend(recurse_marshal(k, sv, nested=insertM + n)) + else : + marshalList.extend(recurse_marshal(k, sv, n)) + elif m == ':record' : + if n.find(':records') < 0 : + n = ':record' + n + for sk, sv in v.items() : + marshalList.extend(recurse_marshal('%s.%s' % (k, sk), sv, nested=n)) + elif m == ':records' : + n = ':records' + n + for sv in v : + for ssk, ssv in sv.items() : + marshalList.extend(recurse_marshal('%s.%s' % (k, ssk), ssv, nested=n)) + else : + marshalList.append({'k' : k, + 'm' : m + nested, + 'v' : v}) + + return marshalList + + +def initialize(context) : + import ZTUtils as ztu + ztu.make_query = ztu.Zope.make_query = make_query + ztu.make_hidden_input = ztu.Zope.make_hidden_input = make_hidden_input \ No newline at end of file