Util.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » Ft » Lib » DistExt » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » XML » 4Suite 
4Suite » 4Suite XML 1.0.2 » Ft » Lib » DistExt » Util.py
from __future__ import generators
import os
import re
import sys
from distutils import sysconfig,util
from distutils.errors import DistutilsPlatformError
from xml.dom import pulldom
from xml.sax import make_parser

__all__ = ['GetConfigVars', 'NormalizePath', 'IterXml', 'FindIncludes']

_config_vars = None

def GetConfigVars(*args):
    """Parse the installed pyconfig.h file.

    A dictionary containing name/value pairs is returned.  If an
    optional dictionary is passed in as the second argument, it is
    used instead of a new dictionary.
    """
    global _config_vars
    if _config_vars is None:
        _config_vars = {}
        filename = sysconfig.get_config_h_filename()
        try:
            fp = open(filename)
        except IOError, err:
            msg = "invalid Python installation: unable to open %s" % filename
            if err.strerror:
                msg += " (%s)" % msg.strerror
            raise DistutilsPlatformError(msg)
        try:
            if sys.version < '2.5':
                define_rx = re.compile("#define ([A-Z][a-zA-Z0-9_]+) (.*)$")
                undef_rx = re.compile("/[*] #undef ([A-Z][a-zA-Z0-9_]+) [*]/$")
                line = fp.readline()
                while line:
                    m = define_rx.match(line)
                    if m:
                        n, v = m.group(1, 2)
                        try: v = int(v)
                        except ValueError: pass
                        _config_vars[n] = v
                    else:
                        m = undef_rx.match(line)
                        if m:
                            _config_vars[m.group(1)] = 0
                    line = fp.readline()
            else:
                sysconfig.parse_config_h(fp, _config_vars)
        finally:
            fp.close()

    if args:
        return [ _config_vars.get(name) for name in args ]
    return _config_vars


def NormalizePath(path):
    """Normalize a file/dir name for comparison purposes"""
    return os.path.normcase(os.path.realpath(path))


def IterXml(stream_or_string):
    if isinstance(stream_or_string, (str, unicode)):
        stream = open(stream_or_string)
    else:
        stream = stream_or_string
    parser = make_parser()
    stream = pulldom.DOMEventStream(stream, parser, pulldom.default_bufsize)
    event = stream.getEvent()
    while event:
        yield event
        event = stream.getEvent()
    return

_include_re = re.compile(r'\s*#\s*include\s*("|<)(?P<include>[^\1]+)(>|")')

def FindIncludes(source, include_dirs=None, includes=None):
    search_path = [os.path.dirname(source)]
    if include_dirs is not None:
        search_path.extend(include_dirs)
    if includes is None:
        includes = []
    f = open(util.convert_path(source))
    if sys.version < '2.3':
        iter_lines = f.readlines()
    else:
        iter_lines = f
    for line in iter_lines:
        match = _include_re.match(line)
        if match is not None:
            include = util.convert_path(match.group('include'))
            for path in search_path:
                filename = os.path.normpath(os.path.join(path, include))
                if os.path.isfile(filename) and filename not in includes:
                    includes.append(filename)
                    FindIncludes(filename, include_dirs, includes)
                    break
    f.close()
    return includes
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.