File: enclbr.py

package info (click to toggle)
python-envisageplugins 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,616 kB
  • ctags: 1,970
  • sloc: python: 7,047; makefile: 11; sh: 11; lisp: 1
file content (181 lines) | stat: -rw-r--r-- 4,362 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
""" The Enthought class browser module. """


# Standard library imports.
import cPickle, imp, logging, os, stat, warnings

# Enthought library imports.
from enthought.io.api import File

# Local imports.
from module import ModuleFactory
from package import Package


# Logging.
logger = logging.getLogger(__name__)


# Filter future warnings for maxint, since it causes warnings when compiling
# anything that has RGBA colors defined as hex values.
warnings.filterwarnings(
    "ignore", r'hex/oct constants > sys\.maxint .*', FutureWarning
)


# A cache that contains every module that has been parsed.
MODULES = {}

# Has the cache been modified in this process?
MODULES_CHANGED = False


def save_cache(filename):
    """ Saves the module cache to disk. """

    global MODULES
    global MODULES_CHANGED
    
    if MODULES_CHANGED:
        logger.debug('saving cache...')
        
        f = file(filename, 'wb')
        cPickle.dump(MODULES, f, 1)
        f.close()

        logger.debug('cache saved')

    return


def load_cache(filename):
    """ Loads the module cache from disk. """

    global MODULES

    if os.path.isfile(filename):
        logger.debug('loading cache...')

        f = file(filename, 'rb')
        MODULES = cPickle.load(f)
        f.close()

        logger.debug('cache loaded...')

    else:
        MODULES = {}

    return
    

def read_package(package_name):
    """ Parse every module in the specified package. """

    filename = find_module(package_name)
    if filename is None:
        raise ValueError("no such package '%s'" % package_name)

    package = Package(filename=filename, name=package_name)
    read_directory(filename, package)
    
    return package


def read_directory(filename, package=None):
    """ Parse every module in the specified directory. """

    directory = File(filename)
    if not directory.is_folder:
        raise ValueError("%s is NOT a directory." % filename)

    if package is not None:
        contents = package.contents

    else:
        contents = []

    for child in directory.children:
        if child.ext == '.py':
            contents.append(read_file(child.path, package))

        elif child.is_package:
            if package is not None:
                sub_package_name = '%s.%s' % (package.name, child.name)
                sub_package = Package(
                    filename=child.path, name=sub_package_name, parent=package
                )

            else:
                sub_package = Package(filename=child.path, name=child.name)

            read_directory(child.path, sub_package)
            contents.append(sub_package)

    return contents


def read_file(filename, namespace=None):
    """ Parses a file. """

    global MODULES
    global MODULES_CHANGED
    
    module, mod_time = MODULES.get(filename, (None, None))
    if module is None or mod_time != os.stat(filename)[stat.ST_MTIME]:
        logger.debug('parsing module %s' % filename)
        
        module_factory = ModuleFactory()
        try:
            module = module_factory.from_file(filename, namespace)

            # Add the parsed module to the cache.
            MODULES[filename] = (module, os.stat(filename)[stat.ST_MTIME])
            MODULES_CHANGED = True

        except:
            logger.exception('error parsing file %s' % filename)
        
    return module


def read_module(module_name):
    """ Parses a module. """

    filename = find_module(module_name)
    if filename is not None:
        module = read_file(filename)

    else:
        module = None
        
    return module


def find_module(module_name, path=None):
    """ Returns the filename for the specified module. """

    components = module_name.split('.')

    try:
        # Look up the first component of the module name (of course it could be
        # the *only* component).
        f, filename, description = imp.find_module(components[0], path)
    
        # If the module is in a package then go down each level in the package
        # hierarchy in turn.
        if len(components) > 0:
            for component in components[1:]:
                f,filename,description = imp.find_module(component, [filename])

    except ImportError:
        filename = None
        
    return filename

#### EOF ######################################################################