Top Description Fields Constructors Methods
org.python.indexer

public Class Indexer

extends Object
Class Inheritance
Imports
org.python.indexer.ast.NNode, .NModule, .NName, .NStr, .NUrl, org.python.indexer.types.NModuleType, .NType, .NUnknownType, java.io.File, .IOException, java.util.ArrayList, .Collection, .HashMap, .HashSet, .Iterator, .List, .Map, .Set, .TreeSet, .TreeMap, .Map.Entry, java.util.logging.Level, .Logger

Indexes a set of Python files and builds a code graph.

This class is not thread-safe.

Field Summary

Modifier and TypeField and Description
private boolean
private Map<String, NBinding>
allBindings

A map of all bindings created, keyed on their qnames.

private AstCache
astCache

Manages a store of serialized ASTs.

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.

public String
public Set<String>
failedModules

When resolving imports we look in various possible locations.

public Scope
globaltable

The top-level (builtin) scope.

public static Indexer
idx

The global indexer instance.

private int
private Map<Ref, List<NBinding>>
locations

A map of references to their referenced bindings.

private Logger
public Scope
moduleTable

A scope containing bindings for all modules currently loaded by the indexer.

private int
private int
private int
private int
private int
public Map<String, List<Diagnostic>>
public List<String>
public Map<String, List<Diagnostic>>
problems

Diagnostics.

public String
private Map<String, Set<String>>
unresolvedModules

This set tracks module imports that could not be resolved.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv void
addFileErr(String file, int beg, int end, String msg)

public void
addPath(String p)

Adds the specified absolute path to the module search path.

public void
addPaths(List<String> p)

Adds the specified absolute paths to the module search path.

public boolean
public void
clearAstCache()

Clears the AST cache (to free up memory).

public void
clearModuleTable()

Clears the module table, discarding all resolved ASTs (modules) and their scope information.

private void
private int
countDefs(List<NBinding> bindings)

private void
public void
enableAggressiveAssertions(boolean enable)

Configures whether the indexer should abort with an exception when it encounters an internal error or unexpected program state.

public void
fine(String msg)

public void
public List<Outliner.Entry>

Returns:

a list of entries constituting the file outline. Returns an empty list if the indexer hasn't indexed that path.
generateOutline
(String
the file for which to build the outline
path
)

Create an outline for a file in the index.

private AstCache
public NModule
getAstForFile(String file)

Returns the syntax tree for file.

public NModule
getAstForFile(String file, String contents)

Returns the syntax tree for file.

public Map<String, NBinding>
getBindings()

Returns the mutable set of all bindings collected, keyed on their qnames.

public NModuleType
pack-priv NModuleType
public List<Diagnostic>
getDiagnosticsForFile(String file)

Returns the list, possibly empty but never null, of errors and warnings generated in the file.

pack-priv List<Diagnostic>
public List<String>
public List<String>
getLoadPath()

Returns the module search path -- the project directory followed by any paths that were added by addPath(String).

public Logger
public NModuleType
getModuleForFile(String
absolute file path
file
)

Returns (loading/resolving if necessary) the module for a given source path.

pack-priv List<Diagnostic>
public String
getStatusReport()

Report resolution rate and other statistics data.

public void
handleException(String
descriptive message; ok to be null
msg
,
Throwable cause)

If aggressive assertions are enabled, propagates the passed Throwable, wrapped in an IndexingException.

public void
info(String msg)

private boolean
public boolean
public NModuleType
loadFile(String path)

Loads a file and all its ancestor packages.

public NModuleType

Returns:

null if path could not be loaded
loadFile
(String
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.
path
,
boolean
true to skip loading ancestor packages
skipChain
)

Load, parse and analyze a source file given its absolute path.

public void
loadFileRecursive(String fullname)

Load all Python source files recursively if the given fullname is a directory; otherwise just load a file.

public NModuleType
loadModule(String
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.
modname
)

This method searches the module path for the module modname.

private void
loadParentPackage(String file)

When we load a module, load all its parent packages, top-down.

public NModuleType
loadString(String
a path for reporting/caching purposes. The filename component is used to construct the module qualified name.
path
,
String contents)

Loads a module from a string containing the module contents.

public void
log(Level level, String msg)

public NBinding
lookupQname(String qname)

Return the binding for qname, or null if not known.

public NType
lookupQnameType(String qname)

Return the type for qname, or null if not known.

public int
private NModuleType
private NModuleType
parseAndResolve(String
the filename
file
,
String
optional file contents. If null, loads the file contents from disk.
contents
)

Parse a file or string and return its module parse tree.

private String
percent(int num, int total)

private String
public NBinding
public void
putLocation(NNode
a node referring to a name binding. Typically a NName, NStr or NUrl.
node
,
NBinding b)

Add a reference to binding b at AST node node.

public void
public void
public void
putProblem(String file, int beg, int end, String msg)

public void
ready()

Performs final indexing-building passes, including marking references to undeclared variables.

public void
recordUnresolvedModule(String
module qname, e.g. "org.foo.bar"
qname
,
String
the file where the unresolved import occurred
file
)

Reports a failed module or submodule resolution.

public void
release()

Releases all resources for the current indexer.

public void
public void
reportFailedAssertion(String
a descriptive message about the problem
msg
)

Signals a failed assertion about the state of the indexer or index.

public void
setLogger(Logger logger)

public void
setPath(List<String> path)

Sets the module search path to the specified list of absolute paths.

public void
public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
updateLocation(Ref node, NBinding b)

Add node as a reference to binding b, removing node from any other binding ref-lists that it may have occupied.

public void
warn(String msg)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

aggressiveAssertionsback to summary
private boolean aggressiveAssertions
allBindingsback to summary
private Map<String, NBinding> allBindings

A map of all bindings created, keyed on their qnames.

astCacheback to summary
private AstCache astCache

Manages a store of serialized ASTs. ANTLR parsing is one of the slower and more expensive phases of indexing; reusing parse trees can help with resource utilization when indexing several projects (or re-indexing one project).

builtinsback to summary
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.

currentFileback to summary
public String currentFile
failedModulesback to summary
public Set<String> failedModules

When resolving imports we look in various possible locations. This set keeps track of modules we attempted but didn't find.

globaltableback to summary
public Scope globaltable

The top-level (builtin) scope.

idxback to summary
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.

loadedFilesback to summary
private int loadedFiles
locationsback to summary
private Map<Ref, List<NBinding>> locations

A map of references to their referenced bindings. Most nodes will refer to a single binding, and few ever refer to more than two. One situation in which a multiple reference can occur is the an attribute of a union type. For instance:

  class A:
    def foo(self): pass
  class B:
    def foo(self): pass
  if some_condition:
    var = A()
  else
    var = B()
  var.foo()  # foo here refers to both A.foo and B.foo

loggerback to summary
private Logger logger
moduleTableback to summary
public Scope moduleTable

A scope containing bindings for all modules currently loaded by the indexer.

nlocback to summary
private int nloc
nparsingback to summary
private int nparsing
nprobback to summary
private int nprob
nunboundback to summary
private int nunbound
nunknownback to summary
private int nunknown
parseErrsback to summary
public Map<String, List<Diagnostic>> parseErrs
pathback to summary
public List<String> path
problemsback to summary
public Map<String, List<Diagnostic>> problems

Diagnostics.

projDirback to summary
public String projDir
unresolvedModulesback to summary
private Map<String, Set<String>> unresolvedModules

This set tracks module imports that could not be resolved.

Constructor Detail

Indexerback to summary
public Indexer()

Method Detail

addFileErrback to summary
pack-priv void addFileErr(String file, int beg, int end, String msg)
addPathback to summary
public void addPath(String p) throws IOException

Adds the specified absolute path to the module search path.

addPathsback to summary
public void addPaths(List<String> p) throws IOException

Adds the specified absolute paths to the module search path.

aggressiveAssertionsEnabledback to summary
public boolean aggressiveAssertionsEnabled()
clearAstCacheback to summary
public void clearAstCache()

Clears the AST cache (to free up memory). Subsequent calls to getAstForFile will either fetch the serialized AST from a disk cache or re-parse the file from scratch.

clearModuleTableback to summary
public void clearModuleTable()

Clears the module table, discarding all resolved ASTs (modules) and their scope information.

convertCallToNewback to summary
private void convertCallToNew(Ref ref, List<NBinding> bindings)
countDefsback to summary
private int countDefs(List<NBinding> bindings)
duplicateBindingFailureback to summary
private void duplicateBindingFailure(NBinding newb, NBinding oldb)
enableAggressiveAssertionsback to summary
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.

fineback to summary
public void fine(String msg)
finerback to summary
public void finer(String msg)
generateOutlineback to summary
public List<Outliner.Entry> generateOutline(String path) throws Exception

Create an outline for a file in the index.

Parameters
path:String

the file for which to build the outline

Returns:List<Outliner.Entry>

a list of entries constituting the file outline. Returns an empty list if the indexer hasn't indexed that path.

getAstCacheback to summary
private AstCache getAstCache() throws Exception
getAstForFileback to summary
public NModule getAstForFile(String file) throws Exception

Returns the syntax tree for file.

getAstForFileback to summary
public NModule getAstForFile(String file, String contents) throws Exception

Returns the syntax tree for file.

getBindingsback to summary
public Map<String, NBinding> getBindings()

Returns the mutable set of all bindings collected, keyed on their qnames.

getBuiltinModuleback to summary
public NModuleType getBuiltinModule(String qname) throws Exception
getCachedModuleback to summary
pack-priv NModuleType getCachedModule(String file)
getDiagnosticsForFileback to summary
public List<Diagnostic> getDiagnosticsForFile(String file)

Returns the list, possibly empty but never null, of errors and warnings generated in the file.

getFileErrsback to summary
pack-priv List<Diagnostic> getFileErrs(String file, Map<String, List<Diagnostic>> map)
getLoadedFilesback to summary
public List<String> getLoadedFiles()
getLoadPathback to summary
public List<String> getLoadPath()

Returns the module search path -- the project directory followed by any paths that were added by addPath(String).

getLoggerback to summary
public Logger getLogger()
getModuleForFileback to summary
public NModuleType getModuleForFile(String file) throws Exception

Returns (loading/resolving if necessary) the module for a given source path.

Parameters
file:String

absolute file path

getParseErrsback to summary
pack-priv List<Diagnostic> getParseErrs(String file)
getStatusReportback to summary
public String getStatusReport()

Report resolution rate and other statistics data.

handleExceptionback to summary
public void handleException(String msg, Throwable cause)

If aggressive assertions are enabled, propagates the passed Throwable, wrapped in an IndexingException.

Parameters
msg:String

descriptive message; ok to be null

infoback to summary
public void info(String msg)
isInLoadPathback to summary
private boolean isInLoadPath(File dir)
isLibFileback to summary
public boolean isLibFile(String file)
loadFileback to summary
public NModuleType loadFile(String path) throws Exception

Loads a file and all its ancestor packages.

See Also
loadFile(String, boolean)
loadFileback to summary
public NModuleType loadFile(String path, boolean skipChain) throws Exception

Load, parse and analyze a source file given its absolute path. By default, loads the entire ancestor package chain.

Parameters
path:String

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:boolean

true to skip loading ancestor packages

Returns:NModuleType

null if path could not be loaded

loadFileRecursiveback to summary
public void loadFileRecursive(String fullname) throws Exception

Load 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.

loadModuleback to summary
public NModuleType loadModule(String modname) throws Exception

This method searches the module path for the module modname. If found, it is passed to loadFile.

The mechanisms for importing modules are in general statically undecidable. We make a reasonable effort to follow the most common lookup rules.

Parameters
modname:String

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.

See Also
The Import Statement
loadParentPackageback to summary
private void loadParentPackage(String file) throws Exception

When we load a module, load all its parent packages, top-down. This is in part because Python does it anyway, and in part so that you can click on all parent package components in import statements. We load whole ancestor chain top-down, as does Python.

loadStringback to summary
public NModuleType loadString(String path, String contents) throws 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:String

a path for reporting/caching purposes. The filename component is used to construct the module qualified name.

logback to summary
public void log(Level level, String msg)
lookupQnameback to summary
public NBinding lookupQname(String qname)

Return the binding for qname, or null if not known.

lookupQnameTypeback to summary
public NType lookupQnameType(String qname)

Return the type for qname, or null if not known.

Exceptions
IllegalStateException:
if ready has not been called.
numFilesLoadedback to summary
public int numFilesLoaded()
parseAndResolveback to summary
private NModuleType parseAndResolve(String file) throws Exception
parseAndResolveback to summary
private NModuleType parseAndResolve(String file, String contents) throws Exception

Parse a file or string and return its module parse tree.

Parameters
file:String

the filename

contents:String

optional file contents. If null, loads the file contents from disk.

Annotations
@SuppressWarnings:unchecked
percentback to summary
private String percent(int num, int total)
printBindingsback to summary
private String printBindings()
putBindingback to summary
public NBinding putBinding(NBinding b)
putLocationback to summary
public void putLocation(NNode node, NBinding b)

Add a reference to binding b at AST node node.

Parameters
node:NNode

a node referring to a name binding. Typically a NName, NStr or NUrl.

putLocationback to summary
public void putLocation(Ref ref, NBinding b)
putProblemback to summary
public void putProblem(NNode loc, String msg)
putProblemback to summary
public void putProblem(String file, int beg, int end, String msg)
readyback to summary
public void ready()

Performs final indexing-building passes, including marking references to undeclared variables. Caller should invoke this method after loading all files.

recordUnresolvedModuleback to summary
public void recordUnresolvedModule(String qname, String file)

Reports a failed module or submodule resolution.

Parameters
qname:String

module qname, e.g. "org.foo.bar"

file:String

the file where the unresolved import occurred

releaseback to summary
public void release()

Releases all resources for the current indexer.

removeBindingback to summary
public void removeBinding(NBinding b)
reportFailedAssertionback to summary
public void reportFailedAssertion(String msg)

Signals a failed assertion about the state of the indexer or index. If aggressive assertions are enabled, throws an IndexingException. Otherwise the message is logged as a warning, and indexing continues.

Parameters
msg:String

a descriptive message about the problem

See Also
enableAggressiveAssertions(boolean)
setLoggerback to summary
public void setLogger(Logger logger)
setPathback to summary
public void setPath(List<String> path) throws IOException

Sets the module search path to the specified list of absolute paths.

setProjectDirback to summary
public void setProjectDir(String cd) throws IOException
severeback to summary
public void severe(String msg)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
updateLocationback to summary
public void updateLocation(Ref node, NBinding b)

Add node as a reference to binding b, removing node from 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.

warnback to summary
public void warn(String msg)