File: util.py

package info (click to toggle)
zope-atseng 0.3.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 312 kB
  • ctags: 223
  • sloc: python: 1,301; makefile: 32; sh: 5
file content (83 lines) | stat: -rw-r--r-- 2,297 bytes parent folder | download
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
# -*- coding: iso-8859-1 -*-

"""
ATSchemaEditorNG

(C) 2003,2004, Andreas Jung, ZOPYX Software Development and Consulting
and Contributors
D-72070 T´┐Żbingen, Germany

Contact: andreas@andreas-jung.com

License: see LICENSE.txt

$Id: util.py 12085 2005-09-22 08:57:47Z rafrombrc $
"""

import urllib
import types
import inspect
from md5 import md5
from ExtensionClass import ExtensionClass

def redirect(RESPONSE, dest, msg=None,**kw):
    """ redirect() helper method """
    
    if RESPONSE is not None:    
        url = dest + "?"
        if msg:
            url += "portal_status_message=%s&" % urllib.quote(msg)
        if kw:
            url += '&'.join(['%s=%s' % (k, urllib.quote(v)) for k,v in kw.items()])
        RESPONSE.redirect(url) 

def create_signature(schema):
    """ Replacement for buggy signature impl in AT Schema """

    s = 'Schema: {'
    for f in schema.fields():

        s += '%s:%s.%s-%s.%s: {' % \
             (f.__name__,
              inspect.getmodule(f.__class__).__name__,
              f.__class__.__name__,
              inspect.getmodule(f.widget.__class__).__name__,
              f.widget.__class__.__name__)

        s += _property_extraction(f._properties)
        s += _property_extraction(f.widget.__dict__)
        s += '}'

    s = s + '}'
    return md5(s).digest()

def _property_extraction(properties):

    s = ''
    disallowed = [types.ClassType, types.MethodType, types.ModuleType, type(ExtensionClass)]

    sorted_keys = properties.keys()
    sorted_keys.sort()
            
    for k in sorted_keys:
        if (type(k) not in disallowed):
            if (type(properties[k]) not in disallowed):
                s = s + '%s:%s,' % (k, properties[k])

    return s


def _getBaseAttr(instance, klass, attr_name):
    """ traverses the base classes of the 'instance' object; returns the
        first class attribute 'attr_name' in a base class that is AFTER
        'klass' in the base class hierarchy.  useful for determining
        which superclass method to call from a mix-in class. """
    bases = instance.__class__.__bases__
    past = 0
    for base in bases:
        if past:
            if hasattr(base, attr_name):
                return getattr(base, attr_name)
        if base == klass:
            past = 1
    return None