org.python.indexer
Class Indexer
- java.lang.Object
-
- org.python.indexer.Indexer
-
public class Indexer extends java.lang.ObjectIndexes a set of Python files and builds a code graph.This class is not thread-safe.
-
-
Field Summary
Fields Modifier and Type Field and Description BuiltinsbuiltinsManages the built-in modules -- that is, modules from the standard Python library that are implemented in C and consequently have no Python source.java.lang.StringcurrentFilejava.util.Set<java.lang.String>failedModulesWhen resolving imports we look in various possible locations.ScopeglobaltableThe top-level (builtin) scope.static IndexeridxThe global indexer instance.ScopemoduleTableA scope containing bindings for all modules currently loaded by the indexer.java.util.Map<java.lang.String,java.util.List<Diagnostic>>parseErrsjava.util.List<java.lang.String>pathjava.util.Map<java.lang.String,java.util.List<Diagnostic>>problemsDiagnostics.java.lang.StringprojDir
-
Constructor Summary
Constructors Constructor and Description Indexer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddPath(java.lang.String p)Adds the specified absolute path to the module search path.voidaddPaths(java.util.List<java.lang.String> p)Adds the specified absolute paths to the module search path.booleanaggressiveAssertionsEnabled()voidclearAstCache()Clears the AST cache (to free up memory).voidclearModuleTable()Clears the module table, discarding all resolved ASTs (modules) and their scope information.voidenableAggressiveAssertions(boolean enable)Configures whether the indexer should abort with an exception when it encounters an internal error or unexpected program state.voidfine(java.lang.String msg)voidfiner(java.lang.String msg)java.util.List<Outliner.Entry>generateOutline(java.lang.String path)Create an outline for a file in the index.NModulegetAstForFile(java.lang.String file)Returns the syntax tree forfile.NModulegetAstForFile(java.lang.String file, java.lang.String contents)Returns the syntax tree forfile.java.util.Map<java.lang.String,NBinding>getBindings()Returns the mutable set of all bindings collected, keyed on their qnames.NModuleTypegetBuiltinModule(java.lang.String qname)java.util.List<Diagnostic>getDiagnosticsForFile(java.lang.String file)Returns the list, possibly empty but nevernull, of errors and warnings generated in the file.java.util.List<java.lang.String>getLoadedFiles()java.util.List<java.lang.String>getLoadPath()Returns the module search path -- the project directory followed by any paths that were added byaddPath(String).java.util.logging.LoggergetLogger()NModuleTypegetModuleForFile(java.lang.String file)Returns (loading/resolving if necessary) the module for a given source path.java.lang.StringgetStatusReport()Report resolution rate and other statistics data.voidhandleException(java.lang.String msg, java.lang.Throwable cause)If aggressive assertions are enabled, propagates the passedThrowable, wrapped in anIndexingException.voidinfo(java.lang.String msg)booleanisLibFile(java.lang.String file)NModuleTypeloadFile(java.lang.String path)Loads a file and all its ancestor packages.NModuleTypeloadFile(java.lang.String path, boolean skipChain)Load, parse and analyze a source file given its absolute path.voidloadFileRecursive(java.lang.String fullname)Load all Python source files recursively if the given fullname is a directory; otherwise just load a file.NModuleTypeloadModule(java.lang.String modname)This method searches the module path for the modulemodname.NModuleTypeloadString(java.lang.String path, java.lang.String contents)Loads a module from a string containing the module contents.voidlog(java.util.logging.Level level, java.lang.String msg)NBindinglookupQname(java.lang.String qname)Return the binding forqname, ornullif not known.NTypelookupQnameType(java.lang.String qname)Return the type forqname, ornullif not known.intnumFilesLoaded()NBindingputBinding(NBinding b)voidputLocation(NNode node, NBinding b)Add a reference to bindingbat AST nodenode.voidputLocation(Ref ref, NBinding b)voidputProblem(NNode loc, java.lang.String msg)voidputProblem(java.lang.String file, int beg, int end, java.lang.String msg)voidready()Performs final indexing-building passes, including marking references to undeclared variables.voidrecordUnresolvedModule(java.lang.String qname, java.lang.String file)Reports a failed module or submodule resolution.voidrelease()Releases all resources for the current indexer.voidremoveBinding(NBinding b)voidreportFailedAssertion(java.lang.String msg)Signals a failed assertion about the state of the indexer or index.voidsetLogger(java.util.logging.Logger logger)voidsetPath(java.util.List<java.lang.String> path)Sets the module search path to the specified list of absolute paths.voidsetProjectDir(java.lang.String cd)voidsevere(java.lang.String msg)java.lang.StringtoString()voidupdateLocation(Ref node, NBinding b)Addnodeas a reference to bindingb, removingnodefrom any other binding ref-lists that it may have occupied.voidwarn(java.lang.String msg)
-
-
-
Field Detail
-
idx
public static Indexer idx
The global indexer instance. Provides convenient access to global resources, as well as easy cleanup of resources after the index is built.
-
moduleTable
public Scope moduleTable
A scope containing bindings for all modules currently loaded by the indexer.
-
globaltable
public Scope globaltable
The top-level (builtin) scope.
-
problems
public java.util.Map<java.lang.String,java.util.List<Diagnostic>> problems
Diagnostics.
-
parseErrs
public java.util.Map<java.lang.String,java.util.List<Diagnostic>> parseErrs
-
currentFile
public java.lang.String currentFile
-
projDir
public java.lang.String projDir
-
path
public java.util.List<java.lang.String> path
-
failedModules
public java.util.Set<java.lang.String> failedModules
When resolving imports we look in various possible locations. This set keeps track of modules we attempted but didn't find.
-
builtins
public Builtins builtins
Manages the built-in modules -- that is, modules from the standard Python library that are implemented in C and consequently have no Python source.
-
-
Method Detail
-
setLogger
public void setLogger(java.util.logging.Logger logger)
-
getLogger
public java.util.logging.Logger getLogger()
-
setProjectDir
public void setProjectDir(java.lang.String cd) throws java.io.IOException- Throws:
java.io.IOException
-
enableAggressiveAssertions
public void enableAggressiveAssertions(boolean enable)
Configures whether the indexer should abort with an exception when it encounters an internal error or unexpected program state. Normally the indexer attempts to continue indexing, on the assumption that having an index with mostly good data is better than having no index at all. Enabling aggressive assertions is useful for debugging the indexer.
-
aggressiveAssertionsEnabled
public boolean aggressiveAssertionsEnabled()
-
handleException
public void handleException(java.lang.String msg, java.lang.Throwable cause)If aggressive assertions are enabled, propagates the passedThrowable, wrapped in anIndexingException.- Parameters:
msg- descriptive message; ok to benull- Throws:
IndexingException
-
reportFailedAssertion
public void reportFailedAssertion(java.lang.String msg)
Signals a failed assertion about the state of the indexer or index. If aggressive assertions are enabled, throws anIndexingException. Otherwise the message is logged as a warning, and indexing continues.- Parameters:
msg- a descriptive message about the problem- Throws:
IndexingException- See Also:
enableAggressiveAssertions(boolean)
-
addPaths
public void addPaths(java.util.List<java.lang.String> p) throws java.io.IOExceptionAdds the specified absolute paths to the module search path.- Throws:
java.io.IOException
-
addPath
public void addPath(java.lang.String p) throws java.io.IOExceptionAdds the specified absolute path to the module search path.- Throws:
java.io.IOException
-
setPath
public void setPath(java.util.List<java.lang.String> path) throws java.io.IOExceptionSets the module search path to the specified list of absolute paths.- Throws:
java.io.IOException
-
getLoadPath
public java.util.List<java.lang.String> getLoadPath()
Returns the module search path -- the project directory followed by any paths that were added byaddPath(String).
-
isLibFile
public boolean isLibFile(java.lang.String file)
-
getBindings
public java.util.Map<java.lang.String,NBinding> getBindings()
Returns the mutable set of all bindings collected, keyed on their qnames.
-
lookupQname
public NBinding lookupQname(java.lang.String qname)
Return the binding forqname, ornullif not known.
-
lookupQnameType
public NType lookupQnameType(java.lang.String qname)
Return the type forqname, ornullif not known.- Throws:
java.lang.IllegalStateException- ifready()has not been called.
-
getModuleForFile
public NModuleType getModuleForFile(java.lang.String file) throws java.lang.Exception
Returns (loading/resolving if necessary) the module for a given source path.- Parameters:
file- absolute file path- Throws:
java.lang.Exception
-
getDiagnosticsForFile
public java.util.List<Diagnostic> getDiagnosticsForFile(java.lang.String file)
Returns the list, possibly empty but nevernull, of errors and warnings generated in the file.
-
generateOutline
public java.util.List<Outliner.Entry> generateOutline(java.lang.String path) throws java.lang.Exception
Create an outline for a file in the index.- Parameters:
path- the file for which to build the outline- Returns:
- a list of entries constituting the file outline. Returns an empty list if the indexer hasn't indexed that path.
- Throws:
java.lang.Exception
-
putLocation
public void putLocation(NNode node, NBinding b)
Add a reference to bindingbat AST nodenode.
-
updateLocation
public void updateLocation(Ref node, NBinding b)
Addnodeas a reference to bindingb, removingnodefrom any other binding ref-lists that it may have occupied. Currently only used in retargeting attribute references from provisional bindings once the actual binding is determined.
-
removeBinding
public void removeBinding(NBinding b)
-
putProblem
public void putProblem(NNode loc, java.lang.String msg)
-
putProblem
public void putProblem(java.lang.String file, int beg, int end, java.lang.String msg)
-
loadFile
public NModuleType loadFile(java.lang.String path) throws java.lang.Exception
Loads a file and all its ancestor packages.- Throws:
java.lang.Exception- See Also:
loadFile(String,boolean)
-
loadString
public NModuleType loadString(java.lang.String path, java.lang.String contents) throws java.lang.Exception
Loads a module from a string containing the module contents. Idempotent: looks in the module cache first. Used for simple unit tests.- Parameters:
path- a path for reporting/caching purposes. The filename component is used to construct the module qualified name.- Throws:
java.lang.Exception
-
loadFile
public NModuleType loadFile(java.lang.String path, boolean skipChain) throws java.lang.Exception
Load, parse and analyze a source file given its absolute path. By default, loads the entire ancestor package chain.- Parameters:
path- the absolute path to the file or directory. If it is a directory, it is suffixed with "__init__.py", and only that file is loaded from the directory.skipChain-trueto skip loading ancestor packages- Returns:
nullifpathcould not be loaded- Throws:
java.lang.Exception
-
getAstForFile
public NModule getAstForFile(java.lang.String file) throws java.lang.Exception
Returns the syntax tree forfile.- Throws:
java.lang.Exception
-
getAstForFile
public NModule getAstForFile(java.lang.String file, java.lang.String contents) throws java.lang.Exception
Returns the syntax tree forfile.- Throws:
java.lang.Exception
-
getBuiltinModule
public NModuleType getBuiltinModule(java.lang.String qname) throws java.lang.Exception
- Throws:
java.lang.Exception
-
loadModule
public NModuleType loadModule(java.lang.String modname) throws java.lang.Exception
This method searches the module path for the modulemodname. If found, it is passed toloadFile(java.lang.String).The mechanisms for importing modules are in general statically undecidable. We make a reasonable effort to follow the most common lookup rules.
- Parameters:
modname- a module name. Can be a relative path to a directory or a file (without the extension) or a possibly-qualified module name. If it is a module name, cannot contain leading dots.- Throws:
java.lang.Exception- See Also:
- The Import Statement
-
loadFileRecursive
public void loadFileRecursive(java.lang.String fullname) throws java.lang.ExceptionLoad all Python source files recursively if the given fullname is a directory; otherwise just load a file. Looks at file extension to determine whether to load a given file.- Throws:
java.lang.Exception
-
ready
public void ready()
Performs final indexing-building passes, including marking references to undeclared variables. Caller should invoke this method after loading all files.
-
clearAstCache
public void clearAstCache()
Clears the AST cache (to free up memory). Subsequent calls togetAstForFile(java.lang.String)will either fetch the serialized AST from a disk cache or re-parse the file from scratch.
-
clearModuleTable
public void clearModuleTable()
Clears the module table, discarding all resolved ASTs (modules) and their scope information.
-
recordUnresolvedModule
public void recordUnresolvedModule(java.lang.String qname, java.lang.String file)Reports a failed module or submodule resolution.- Parameters:
qname- module qname, e.g. "org.foo.bar"file- the file where the unresolved import occurred
-
getStatusReport
public java.lang.String getStatusReport()
Report resolution rate and other statistics data.
-
numFilesLoaded
public int numFilesLoaded()
-
getLoadedFiles
public java.util.List<java.lang.String> getLoadedFiles()
-
log
public void log(java.util.logging.Level level, java.lang.String msg)
-
severe
public void severe(java.lang.String msg)
-
warn
public void warn(java.lang.String msg)
-
info
public void info(java.lang.String msg)
-
fine
public void fine(java.lang.String msg)
-
finer
public void finer(java.lang.String msg)
-
release
public void release()
Releases all resources for the current indexer.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-
DMelt 3.0 © DataMelt by jWork.ORG