File: funcs.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 (77 lines) | stat: -rw-r--r-- 2,437 bytes parent folder | download | duplicates (4)
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
# Copyright (C) 1997-2001 by Dr. Dieter Maurer <dieter@hit.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 appear in all copies and
# modified copies and that
# both that copyright notice and this permission notice appear 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.
"""Function auxiliaries"""

import types, string

# copied from Python header file
CO_OPTIMIZED = 0x0001
CO_NEWLOCALS = 0x0002
CO_VARARGS = 0x0004
CO_VARKEYWORDS = 0x0008

def func_props(f,detail='a',name=None):
  """returns a string describing the function, method or class *f*

  *detail* controls how much properties are included:
    ''		only the functions name
    'a'		includes the functions arguments
    'd'	        the functions first line of documentation
    'D'		includes the functions complete documentation
    several options can be concatenated"""
  (n,a,d)= func_prop_tuple(f)
  if name is None: name= n
  args= 'a' in detail and '(' + string.join(a,',') + ')' or ''
  doc= ''
  if d:
    if 'D' in detail: doc= '\n  ' + d
    elif 'd' in detail: doc= ': ' + d
  return name + args + doc


def func_prop_tuple(f):
  name=f.__name__
  doc=f.__doc__
  t=type(f)
  if t is types.MethodType: f= f.im_func
  elif t is types.ClassType:
    try:
      f= f.__init__.im_func
      if f.__doc__: doc=f.__doc__
    except: f= _emptyfunc
  c=f.func_code
  n=c.co_argcount
  a=list(c.co_varnames[:n])
  if f.func_defaults:
    i=n-len(f.func_defaults)
    for d in f.func_defaults:
      a[i]= a[i] + '=' + repr(d)
      i= i+1
  if c.co_flags & CO_VARARGS:
    a.append('*' + c.co_varnames[n])
    n= n+1
  if c.co_flags & CO_VARKEYWORDS:
    a.append('**' + c.co_varnames[n])
    n= n+1
  if t is types.ClassType: del(a[0])
  return (name, a, doc)
    
def _emptyfunc(self): pass