File: imports.py

package info (click to toggle)
python-happydoc 2.0-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,176 kB
  • ctags: 3,347
  • sloc: python: 11,321; makefile: 88; sh: 77
file content (144 lines) | stat: -rw-r--r-- 4,688 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
#
# $Id: imports.py,v 1.1 2001/11/11 18:51:37 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
#                         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 that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN 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.
#

"""Collects info about imports for a module.

"""

__rcs_info__ = {
    #
    #  Creation Information
    #
    'module_name'  : '$RCSfile: imports.py,v $',
    'rcs_id'       : '$Id: imports.py,v 1.1 2001/11/11 18:51:37 doughellmann Exp $',
    'creator'      : 'Doug Hellmann <doug@hellfly.net>',
    'project'      : 'UNSPECIFIED',
    'created'      : 'Sun, 11-Nov-2001 10:51:52 EST',

    #
    #  Current Information
    #
    'author'       : '$Author: doughellmann $',
    'version'      : '$Revision: 1.1 $',
    'date'         : '$Date: 2001/11/11 18:51:37 $',
}
try:
    __version__ = __rcs_info__['version'].split(' ')[1]
except:
    __version__ = '0.0'

#
# Import system modules
#


#
# Import Local modules
#


#
# Module
#

class ImportInfo:
    """Collects info about imports for a module.
    """

    def __init__(self):
        self._straight_imports = []
        self._named_imports = {}
        return

    def addImport(self, moduleName, symbolName=None, asName=None):
        """Add information about an import statement to the saved info.

        Parameters

          moduleName -- The name of the module involved in the import.
          For example, in 'from X import Y', X is the moduleName and
          in 'import A.B', A.B is the moduleName.

          symbolName -- The name of the symbol being imported.  For
          example, in 'from X import Y', Y is the symbolName.

          asName -- The name within the module by which symbolName can
          be referenced.  Usually, this is the same as symbolName, but
          by using the 'import X as Y' syntax, the name can be changed.
          
        """
        dbg=0
        
        if symbolName:
            #if dbg: print '\nIMPORT SYMBOL %s from MODULE %s' % (symbolName, moduleName)
            name_list = self._named_imports.get(moduleName, [])
            if symbolName not in name_list:
                #if dbg: print '\t*** added'
                name_list.append(symbolName)
                self._named_imports[moduleName] = name_list
                
        else:
            #if dbg: print '\nIMPORT MODULE: %s' % moduleName
            if moduleName not in self._straight_imports:
                #if dbg: print '\t*** added'
                self._straight_imports.append(moduleName)
        #if dbg:
        #    print 'STRAIGHT: ',
        #    pprint.pprint(self._straight_imports)
        #    print 'NAMED: ',
        #    pprint.pprint(self._named_imports)
        #    print 'CURRENT IMPORTS: ', self.items()
        return
    
    def importedSymbols(self, moduleName):
        if self._named_imports.has_key(moduleName):
            return self._named_imports[moduleName]
        else:
            raise ValueError('No symbols imported for module', moduleName)
        return

    def __str__(self):
        return '(%s)' % string.join( map(str, self.items()),
                                     '\n'
                                     )
    
    def items(self):
        """Returns a sequence of tuples containing module names and the
        symbols imported from them.
        """
        all_names = self._straight_imports[:]
        for name in self._named_imports.keys():
            if name not in all_names:
                all_names.append(name)
        all_names.sort()
        all_items = []
        for name in all_names:
            if name in self._straight_imports:
                all_items.append( (name, None) )
            if self._named_imports.has_key(name):
                all_items.append( (name, self._named_imports[name]) )
        return all_items