newstuff.py :  » Development » Bicycle-Repair-Man » bicyclerepair-0.9 » bike » parsing » 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 » Development » Bicycle Repair Man 
Bicycle Repair Man » bicyclerepair 0.9 » bike » parsing » newstuff.py
from __future__ import generators
# Holding module for scaffolding needed to transition parsing package
# into stateless design
import os
import re
from bike.parsing.pathutils import getRootDirectory,getPackageBaseDirectory,\
     filenameToModulePath, getPathOfModuleOrPackage, getFilesForName
from bike.parsing.fastparserast import Module,Package,getRoot,getPackage,getModule
import sys
from bike.parsing.load import getSourceNode,CantLocateSourceNodeException


def translateFnameToModuleName(filename_path):
    return filenameToModulePath(filename_path)



# scope is the scope to search from
def getModuleOrPackageUsingFQN(fqn, dirpath=None):
    pythonpath = getPythonPath()
    #print "getModuleOrPackageUsingFQN",pythonpath,fqn
    if dirpath is not None:
        assert os.path.isdir(dirpath)
        pythonpath = [dirpath] + pythonpath
    filename = getPathOfModuleOrPackage(fqn,pythonpath)
    #print "getModuleOrPackageUsingFQN - filename",filename
    if filename is not None:
        if os.path.isdir(filename):
            return getPackage(filename)
        else:
            return getModule(filename)
    else:
        return None

def getPythonPath():
    return getRoot().pythonpath


def generateModuleFilenamesInPythonPath(contextFilename):
    files = []
    rootdir = getRootDirectory(contextFilename)
    if rootdir in getPythonPath():
        # just search the pythonpath
        for path in getPythonPath():
            for file in getFilesForName(path):
                if file not in files:   # check for duplicates
                    files.append(file)
                    yield file
    else:
        # search the package hierarchy containing contextFilename
        # in addition to pythonpath
        basedir = getPackageBaseDirectory(contextFilename)
        for path in [basedir] + getPythonPath():
            for file in getFilesForName(path):
                if file not in files:   # check for duplicates
                    files.append(file)
                    yield file

        # and search the files immediately above the package hierarchy
        for file in getFilesForName(os.path.join(rootdir,"*.py")):
            if file not in files:   # check for duplicates
                files.append(file)
                yield file

def generateModuleFilenamesInPackage(filenameInPackage):
    basedir = getPackageBaseDirectory(filenameInPackage)
    for file in getFilesForName(basedir):
        yield file



# search all sourcenodes globally from the perspective of file 'contextFilename'
def getSourceNodesContainingRegex(regexstr,contextFilename):
    regex = re.compile(regexstr)
    for fname in generateModuleFilenamesInPythonPath(contextFilename):
        try:
            f = file(fname)
            src = f.read()
        finally:
            f.close()
        if regex.search(src) is not None:
            yield getSourceNode(fname)




fromRegex = re.compile("^\s*from\s+(\w+)\s+import")
importregex = re.compile("^\s*import\s+(\w+)")

# fileInPackage is the filename of a file in the package hierarchy
# generates file and directory paths
def generatePackageDependencies(fileInPackage):
    rejectPackagePaths = [getPackageBaseDirectory(fileInPackage)]
    for fname in generateModuleFilenamesInPackage(fileInPackage):

        try:
            f = file(fname)
            src = f.read()
        finally:
            f.close()

        packagepath = None

        for line in src.splitlines():
            match = fromRegex.search(line) or importregex.search(line)
            if match is not None:
                modulepath = match.group(1)
                packagename = modulepath.split('.')[0]
                packagepath = getPathOfModuleOrPackage(packagename,
                                                       getPythonPath())
            if packagepath is not None and \
                   packagepath not in rejectPackagePaths:
                rejectPackagePaths.append(packagepath) # avoid duplicates
                yield packagepath
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.