# Path: pyxc/util.py
# class DirectedGraph:
#     
#     def __init__(self):
#         self._graph = {}
#     
#     def addNode(self, x):
#         if x not in self._graph:
#             self._graph[x] = set()
#     
#     def addArc(self, x, y):
#         self.addNode(x)
#         self.addNode(y)
#         self._graph[x].add(y)
#     
#     @property
#     def topologicalOrdering(self):
#         
#         def topologicalOrderingDestructive(d):
#             
#             if len(d) == 0:
#                 return []
#             
#             possibleInitialNodes = set(d.keys())
#             for k, v in d.items():
#                 if len(v) > 0:
#                     possibleInitialNodes.discard(k)
#             if len(possibleInitialNodes) == 0:
#                 raise CyclicGraphError(repr(d))
#             initialNode = possibleInitialNodes.pop()
#             
#             for k, v in d.items():
#                 v.discard(initialNode)
#             del d[initialNode]
#             
#             return [initialNode] + topologicalOrderingDestructive(d)
#         
#         return topologicalOrderingDestructive(copy.deepcopy(self._graph))


import os, re
from pyxc.util import DirectedGraph

# Python code &rarr; list of imported modules
def parseImports(py):
    imports = []
    for line in py.split('\n'):
        for m in re.finditer(r'^\s*from[ \t]+([^ \t]+)[ \t]+import', line):
            imports.append(m.group(1) or m.group(2))
    return imports


class SourcePath:
    
    def __init__(self, folders):
        self.folders = folders
    
    def pathForModule(self, module, exts=['py', 'pj', 'js']):
        
        # Find all matches
        paths = []
        for folder in self.folders:
            for ext in exts:
                path = '%s/%s.%s' % (
                                        folder.rstrip('/'),
                                        '/'.join(module.split('.')),
                                        ext)
                if os.path.isfile(path):
                    paths.append(path)
        
        # Do we have exactly one match?
        if len(paths) == 1:

# Path: pyxc/util.py
# class DirectedGraph:
#     
#     def __init__(self):
#         self._graph = {}
#     
#     def addNode(self, x):
#         if x not in self._graph:
#             self._graph[x] = set()
#     
#     def addArc(self, x, y):
#         self.addNode(x)
#         self.addNode(y)
#         self._graph[x].add(y)
#     
#     @property
#     def topologicalOrdering(self):
#         
#         def topologicalOrderingDestructive(d):
#             
#             if len(d) == 0:
#                 return []
#             
#             possibleInitialNodes = set(d.keys())
#             for k, v in d.items():
#                 if len(v) > 0:
#                     possibleInitialNodes.discard(k)
#             if len(possibleInitialNodes) == 0:
#                 raise CyclicGraphError(repr(d))
#             initialNode = possibleInitialNodes.pop()
#             
#             for k, v in d.items():
#                 v.discard(initialNode)
#             del d[initialNode]
#             
#             return [initialNode] + topologicalOrderingDestructive(d)
#         
#         return topologicalOrderingDestructive(copy.deepcopy(self._graph))


# Path: pyxc/importing.py
import os, re
from pyxc.util import DirectedGraph


# Python code &rarr; list of imported modules
def parseImports(py):
    imports = []
    for line in py.split('\n'):
        for m in re.finditer(r'^\s*from[ \t]+([^ \t]+)[ \t]+import', line):
            imports.append(m.group(1) or m.group(2))
    return imports


class SourcePath:
    
    def __init__(self, folders):
        self.folders = folders
    
    def pathForModule(self, module, exts=['py', 'pj', 'js']):
        
        # Find all matches
        paths = []
        for folder in self.folders:
            for ext in exts:
                path = '%s/%s.%s' % (
                                        folder.rstrip('/'),
                                        '/'.join(module.split('.')),
                                        ext)
                if os.path.isfile(path):
                    paths.append(path)
        
        # Do we have exactly one match?
        if len(paths) == 1:

            return paths[0]