File: class_utility.py

package info (click to toggle)
zope-groupuserfolder 3.1.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,416 kB
  • ctags: 1,037
  • sloc: python: 6,755; sh: 1,365; makefile: 147
file content (172 lines) | stat: -rw-r--r-- 4,419 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import string
import re
import threading
import string

# Base classes global vars management
_BASECLASSESLOCK = threading.RLock()
_BASECLASSES = {}
_BASEMETALOCK = threading.RLock()
_BASEMETA = {}

def showaq(self, indent=''):
    "showaq"
    rval = ""
    obj = self
    base = getattr(obj, 'aq_base', obj)
    try: id = base.id
    except: id = str(base)
    try: id = id()
    except: pass

    if hasattr(obj, 'aq_self'):
        if hasattr(obj.aq_self, 'aq_self'):
            rval = rval + indent + "(" + id + ")\n"
            rval = rval + indent + "|  \\\n"
            rval = rval + showaq(obj.aq_self, '|   ' + indent)
            rval = rval + indent + "|\n"
        if hasattr(obj, 'aq_parent'):
            rval = rval + indent + id + "\n"
            rval = rval + indent + "|\n"
            rval = rval + showaq(obj.aq_parent, indent)
    else:
        rval = rval + indent + id + "\n"
    return rval


def listBaseMetaTypes(cl, reverse = 0):
    """
    listBaseMetaTypes(cl, reverse = 0) => list of strings

    List all base meta types for this class.
    """
    # Look for the class in _BASEMETA cache
    try:
        return _BASEMETA[cl][reverse]

    except KeyError:
        _populateBaseMetaTypes(cl)
        return listBaseMetaTypes(cl, reverse)

def isBaseMetaType(meta, cl):
    try:
        return _BASEMETA[cl][2].has_key(meta)

    except KeyError:
        _populateBaseMetaTypes(cl)
        return isBaseMetaType(meta, cl)

def _populateBaseMetaTypes(cl):
    """Fill the base classes structure"""
    # Fill the base classes list
    try:
        ret = [cl.meta_type]
    except AttributeError:
        ret = []

    for b in cl.__bases__:
        ret = list(listBaseMetaTypes(b, 1)) + ret

    # Fill the base classes dict
    bases = {}
    for b in ret:
        bases[b] = 1

    _BASEMETALOCK.acquire()
    try:
        rev = ret[:]
        rev.reverse()
        _BASEMETA[cl] = (tuple(rev), tuple(ret), bases)
    finally:
        _BASEMETALOCK.release()

def objectIds(container, meta_types = []):
    """
    """
    return map(lambda x: x[0], objectItems(container, meta_types))

def objectValues(container, meta_types = []):
    """
    """
    return map(lambda x: x[1], objectItems(container, meta_types))

def objectItems(container, meta_types = []):
    """
    objectItems(container, meta_types = [])
    Same as a container's objectItem method, meta_types are scanned in the base classes too.
    Ie. all objects derivated from Folder will be returned by objectItem(x, ['Folder'])
    """
    # Convert input type
    if type(meta_types) not in (type(()), type([])):
        meta_types = [meta_types]

    # Special case where meta_types is empty
    if not meta_types:
        return container.objectItems()

    # Otherwise : check parent for each meta_type
    ret = []
    for (id, obj) in container.objectItems():
        for mt in meta_types:
            if isBaseMetaType(mt, obj.__class__):
                ret.append((id, obj))
                break

    return ret



def listBaseClasses(cl, reverse = 0):
    """
    listBaseClasses(cl, reverse = 0) => list of classes

    List all the base classes of an object.
    When reverse is 0, return the self class first.
    When reverse is 1, return the self class last.

    WARNING : reverse is 0 or 1, it is an integer, NOT A BOOLEAN ! (optim issue)

    CACHE RESULTS

    WARNING : for optimization issues, the ORIGINAL tuple is returned : please do not change it !
    """
    # Look for the class in _BASECLASSES cache
    try:
        return _BASECLASSES[cl][reverse]

    except:
        _populateBaseClasses(cl)
        return listBaseClasses(cl, reverse)


def isBaseClass(base, cl):
    """
    isBaseClass(base, cl) => Boolean
    Return true if base is a base class of cl
    """
    try:
        return _BASECLASSES[cl][2].has_key(base)
    except:
        _populateBaseClasses(cl)
        return isBaseClass(base, cl)


def _populateBaseClasses(cl):
    """Fill the base classes structure"""
    # Fill the base classes list
    ret = [cl]
    for b in cl.__bases__:
        ret = list(listBaseClasses(b, 1)) + ret

    # Fill the base classes dict
    bases = {}
    for b in ret:
        bases[b] = 1

    _BASECLASSESLOCK.acquire()
    try:
        rev = ret[:]
        rev.reverse()
        _BASECLASSES[cl] = (tuple(rev), tuple(ret), bases)
    finally:
        _BASECLASSESLOCK.release()