File: analyse.py

package info (click to toggle)
zope-docfindereverywhere 0.4.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 184 kB
  • ctags: 168
  • sloc: python: 814; sh: 70; makefile: 40
file content (405 lines) | stat: -rw-r--r-- 11,024 bytes parent folder | download | duplicates (2)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Copyright (C) 2001 by Dr. Dieter Maurer <dieter@handshake.de>
# D-66386 St. Ingbert, Eichendorffstr. 23, Germany
#
#			All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice and this permission
# notice appear in all copies, modified copies and in
# supporting documentation.
# 
# Dieter Maurer DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL Dieter Maurer
# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
"""analyse object for methods (names, arguments, documentation) and
permissions."""

# Detail types
DETAIL_PROGRAMMER= 'programmer'
DETAIL_SCRIPTER= 'scripter'
DETAIL_WEB= 'web'

import sys


from funcs import func_prop_tuple
from string import join, replace
import re
from types import IntType, DictType
from AccessControl.SecurityInfo import ClassSecurityInfo, \
     ACCESS_PUBLIC, ACCESS_PRIVATE, ACCESS_NONE
from ExtensionClass import Base


class DocFinder(Base):
  '''determine the documentation of an object.

  Doc is maintained in a two level structure:

   1 classes, the object is build from, in inheritance order

     each class is described by a 'ClassDoc' instance

   2 for each class, the attributes defined by the class
     
     each attribute is described by a 'AttributeDoc' instance.

  The documentation does not include instance level attributes
  (they are too many). However, it does provide
  summary information about access to unprotected attributes
  in the doc for the pseudo class '-- Instance --'.
  This information is not accurate, as the
  '__allow_access_to_unprotected__subobjects__'
  evaluation is not precise.
  '''

  _classDict= None

  _secInfo= ClassSecurityInfo()
  _secInfo.declarePublic('__getitem__','__len__','tpValues', 'tpId')

  def __init__(self, obj, detail_type=DETAIL_SCRIPTER, method_filter= None):
    # encode type
    t= detail_type
    if type(t) == type(''):
      if t == DETAIL_PROGRAMMER: t= 10
      elif t == DETAIL_WEB: t= -10
      else: t= 0

    # print 'DocFinder: ', detail_type, t; sys.stdout.flush()

    # try to get a name
    name= None
    i= getattr(obj,'getId',None) or getattr(obj,'id',None) or getattr(obj,'__name__',None)
    if i is not None:
      if callable(i): i= i()
      name= i
    if name is None: name= '-- Doc --'

    # permanent members
    self._classes= []
    self._type= t
    self._name= name
    self._method_filter=  method_filter and re.compile(method_filter).match

    # temporary members
    self._obj= obj
    self._seenclasses= {}
    self._attributes= {}
    self._check= self._makeUnprotectedChecker()

    c= _getClass(obj)

    ic= ClassDoc('-- Instance --')
    ic._append(AttributeDoc('-- unprotected attributes --',self._attrRoles(),obj= obj))
    self._classes.append(ic)

    self._analyseClassStructure(c)

    # delete temporaries
    del self._obj
    del self._seenclasses
    del self._attributes
    del self._check


  def __getitem__(self,k):
    '''allow access by both integer as well as class names.'''
    if type(k) is IntType: return self._classes[k]
    if self._classDict is None:
      cd= self._classDict= {}
      for c in self._classes: cd[c._name]= c
    return self._classDict[k]

  
  def __len__(self):
    '''the length of the classes.'''
    return len(self._classes)


  def tpValues(self):
    '''tuple of classes for tree display.'''
    return tuple(self._classes)

  def tpId(self):
    return self._name


  def _analyseClassStructure(self, c):
    '''analyse class *c* including base classes.'''

    if self._seenclasses.has_key(c): return
    self._seenclasses[c]= None

    self._analyseClass(c)

    for b in c.__bases__:
      self._analyseClassStructure(b)


  def _analyseClass(self, c,
                    _omit= {'__doc__': None, '__module__': None,
                            '__allow_access_to_unprotected_subobjects__': None,
                            }.has_key,
                    _allow= {'__len__': None,
                            '__str__': None,
                            '__getitem__': None,
                            '__call__': None,
                            }.has_key,
                    ):
    '''analyse *c*.'''
    cd= ClassDoc(c.__name__, getattr(c,'__doc__',None),_getLoc(c))
    attributes= self._attributes; seen= attributes.has_key
    check= self._check; o= self._obj; filter= self._method_filter

    for (k,v) in c.__dict__.items():
      if k[-9:] == '__roles__' or _omit(k): continue
      if seen(k): continue
      attributes[k]= None
      if self._type <= 5:
        if k[0] == '_' and not _allow(k): continue
      if filter and not filter(k): continue
      r= getattr(o,k+'__roles__', check(k))
      if self._type <= 0 and _isPrivat(r): continue
      # something interesting
      a= AttributeDoc(k,r,v,o)
      if self._type <= -5 and not a.Doc(): continue
      cd._append(a)

    if self._type <= 5 and not cd: return
    cd._finish()
    self._classes.append(cd)


  def _makeUnprotectedChecker(self):
    roles= getattr(self._obj,'__roles__', ACCESS_PUBLIC)
    allow= getattr(self._obj,'__allow_access_to_unprotected_subobjects__', 0)
    if type(allow) is IntType:
      if not allow: roles= ACCESS_PRIVATE
      def check(name,roles=roles): return roles
    elif type(allow) is DictType:
      def check(name, check=allow.get, roles=roles, priv= ACCESS_PRIVATE):
        if check(name): return roles
        return priv
    else:
      def check(name, obj= self._obj, allow= allow, roles=roles, priv= ACCESS_PRIVATE):
        v= getattr(obj,name)
        if allow(name,v): return roles
        return priv
    return check


  def _attrRoles(self):
    roles= getattr(self._obj,'__roles__', ACCESS_PUBLIC)
    allow= getattr(self._obj,'__allow_access_to_unprotected_subobjects__', 0)
    if type(allow) is IntType:
      if not allow: roles= ACCESS_PRIVATE
    elif type(allow) is DictType: roles= 'Restricted (Dict)'
    else: roles= 'Restricted (Func)'
    return roles


DocFinder._secInfo.apply(DocFinder)


class ClassDoc(Base):
  """the documentation of a class.

  It consists of a 'Name', 'Doc', 'Module' and a list of attributes,
  that can also be accessed via the attribute name.
  """

  _secInfo= ClassSecurityInfo()
  _secInfo.declarePublic('__getitem__','__len__','tpValues', 'tpId', 'Name', 'Doc', 'Module')

  _AttrDict= None

  def __init__(self,name,doc=None,mod=None):
    self._name= name
    self._doc= doc
    self._mod= mod
    self._attrs= []

  def __getitem__(self,k):
    '''allow access by both integer as well as attr names.'''
    if type(k) is IntType: return self._attrs[k]
    if self._AttrDict is None:
      cd= self._AttrDict= {}
      for c in self._attrs: cd[c._name]= c
    return self._AttrDict[k]

  
  def __len__(self):
    '''the length of the classes.'''
    return len(self._attrs)


  def tpValues(self):
    '''tuple of attributes for tree display.'''
    return tuple(self._attrs)

  def tpId(self):
    '''use name as id.'''
    return self._name


  def Name(self):
    '''the class name.'''
    return self._name

  def Doc(self):
    '''the class doc.'''
    return self._doc

  def Module(self):
    '''the module the class is defined in.'''
    return self._mod


  def _append(self,attr):
    '''append *attr*.'''
    self._attrs.append(attr)


  def _finish(self):
    '''finish class definition.'''
    self._attrs.sort(lambda a1,a2, cmp=cmp: cmp(a1._name,a2._name))


ClassDoc._secInfo.apply(ClassDoc)



class AttributeDoc(Base):
  """the documentation of an attribute.

  It consists of a 'Name', 'Roles', 'Args', 'Doc' and 'Type'.
  """

  _secInfo= ClassSecurityInfo()
  _secInfo.declarePublic('tpValues', 'tpId',
                         'Name', 'Roles', 'Args', 'Doc', 'Type', 'DocOrType',
                         'Permission',
                         )

  _knownPermission= 0

  def __init__(self,name,roles,value= None, obj= None):
    if value is not None:
      # determine arguments and documentation, if possible
      arguments= doc= ''
      try:
        (n,a,doc)= func_prop_tuple(value)
        arguments= join(a,', ')
      except: pass
      try: doc= value.__doc__
      except: pass
      type= _getType(value)
    else: doc= arguments= type= ''

    self._name= name
    if roles is ACCESS_PUBLIC: roles= 'public'
    elif roles == ACCESS_PRIVATE: roles= 'private'
    elif roles is ACCESS_NONE: roles= 'none'
    self._roles= roles
    self._arguments= arguments
    self._doc= doc
    self._type= type
    self._obj= obj

  def Name(self):
    '''the attribute name'''
    return self._name

  def Roles(self):
    '''the attribute roles'''
    return self._roles

  def Args(self):
    '''the attribute arguments'''
    return self._arguments

  def Doc(self):
    '''the attribute documentation'''
    return self._doc

  def Type(self):
    '''the attribute type'''
    return self._type

  def tpValues(self):
    return ()

  def tpId(self):
    '''use name as id.'''
    return self._name

  def DocOrType(self):
    '''either the Doc (prefered) or the Type.'''
    return self.Doc() or self.Type()

  def Permission(self):
    '''return the permission protecting the attribute, 'None' if not directly protected.'''
    if self._knownPermission: return self._permission
    p= None
    if self._obj:
      name= self._name
      if name[:3] == '-- ': name= ''
      p= _lookup(self._obj, name+'__roles__')
      if p is not None:
        try:
          p= replace(p[1]._p[1:-11],'_',' ')
        except: p= '-- explicit --'
    self._permission= p; self._knownPermission= 1
    return p



AttributeDoc._secInfo.apply(AttributeDoc)


def _isPrivat(role):
  return role == ACCESS_PRIVATE or role is ACCESS_NONE


def _getLoc(c):
  '''return location (module) of class *c*.'''
  return getattr(c,'__module__',None)


def _getType(v):
  '''return a nice representation of the *v* type.'''
  tn= type(v).__name__
  if tn == 'instance': tn= '%s %s' % (v.__class__.__name__,tn)
  elif tn == 'instance method': tn= '%s %s' % (v.im_class.__name__,tn)
  return tn


def _getClass(obj):
  '''return the class of *obj*.'''
  return hasattr(obj,'_klass') and obj._klass or obj.__class__


def _lookup(obj,key):
  '''emulate Pythons name lookup; return pair (class,attr) or 'None'.'''
  m= {}
  od= getattr(obj,'__dict__',m)
  v= od.get(key,m)
  if v is not m: return (obj,v)
  v= _lookupClassHierarchy(_getClass(obj),key,m)
  return v


def _lookupClassHierarchy(c,k,m):
  v= c.__dict__.get(k,m)
  if v is not m: return (c,v)
  for c in c.__bases__:
    v= _lookupClassHierarchy(c,k,m)
    if v is not None: return v
  return None