Skip to content

Commit

Permalink
Merge pull request #469 from bageljrkhanofemus/master
Browse files Browse the repository at this point in the history
Use multithread executor for local imports, process imports
  • Loading branch information
lieryan authored May 20, 2022
2 parents bae936f + f299abe commit 551438c
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 133 deletions.
9 changes: 9 additions & 0 deletions docs/library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ uses sqlite3 database (rope.contrib.autoimport.sqlite.AutoImport). New and
existing integrations should migrate to the sqlite3 storage as the pickle-based
autoimport will be removed in the future.
`rope.contrib.autoimport.sqlite`
--------------------------------
By default, the sqlite3-based only stores autoimport cache in an in-memory
sqlite3 database, you can make it write the import cache to persistent storage
by passing memory=False to AutoImport constructor.
Expand All @@ -862,6 +866,11 @@ AutoImport can search for a name from both modules and statements you can import
autoimport.generate_modules_cache() # Generates a cache of external modules
print(autoimport.search("Dict"))
autoimport.close()
project.close()
It provides two new search methods:
- search_full() - returns a list of mostly unsorted tuples. This has itemkind and source information.
- search() - simpler wrapper around search_full with a basic sorting algorithm
Cross-Project Refactorings
Expand Down
15 changes: 12 additions & 3 deletions rope/contrib/autoimport/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ModuleInfo(NamedTuple):
filepath: Optional[pathlib.Path]
modname: str
underlined: bool
process_imports: bool = False
process_imports: bool


class ModuleFile(ModuleInfo):
Expand All @@ -32,7 +32,7 @@ class ModuleFile(ModuleInfo):
filepath: pathlib.Path
modname: str
underlined: bool
process_imports: bool = False
process_imports: bool


class ModuleCompiled(ModuleInfo):
Expand All @@ -41,7 +41,7 @@ class ModuleCompiled(ModuleInfo):
filepath = None
modname: str
underlined: bool
process_imports: bool = False
process_imports: bool


class PackageType(Enum):
Expand Down Expand Up @@ -107,3 +107,12 @@ class PartialName(NamedTuple):

name: str
name_type: NameType


class SearchResult(NamedTuple):
"""Search Result."""

import_statement: str
name: str
source: int
itemkind: int
30 changes: 26 additions & 4 deletions rope/contrib/autoimport/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ def get_type_ast(node: ast.AST) -> NameType:
return NameType.Function
if isinstance(node, ast.Assign):
return NameType.Variable
return NameType.Text # default value
return NameType.Variable # default value


def get_names_from_file(
module: pathlib.Path,
package_name: str = "",
underlined: bool = False,
process_imports: bool = False,
) -> Generator[PartialName, None, None]:
"""Get all the names from a given file using ast."""

try:
root_node = ast.parse(module.read_bytes())
except SyntaxError as error:
Expand All @@ -66,6 +67,22 @@ def get_names_from_file(
node.name,
get_type_ast(node),
)
elif process_imports and isinstance(node, ast.ImportFrom):
# When we process imports, we want to include names in it's own package.
if node.level == 0:
continue
if not node.module or package_name is node.module.split(".")[0]:
continue
for name in node.names:
if isinstance(name, ast.alias):
if name.asname:
real_name = name.asname
else:
real_name = name.name
else:
real_name = name
if underlined or not real_name.startswith("_"):
yield PartialName(real_name, get_type_ast(node))


def get_type_object(imported_object) -> NameType:
Expand All @@ -74,7 +91,7 @@ def get_type_object(imported_object) -> NameType:
return NameType.Class
if inspect.isfunction(imported_object) or inspect.isbuiltin(imported_object):
return NameType.Function
return NameType.Constant
return NameType.Variable


def get_names(module: ModuleInfo, package: Package) -> List[Name]:
Expand All @@ -86,7 +103,12 @@ def get_names(module: ModuleInfo, package: Package) -> List[Name]:
if isinstance(module, ModuleFile):
return [
combine(package, module, partial_name)
for partial_name in get_names_from_file(module.filepath, module.underlined)
for partial_name in get_names_from_file(
module.filepath,
package.name,
underlined=module.underlined,
process_imports=module.process_imports,
)
]
return []

Expand Down
Loading

0 comments on commit 551438c

Please sign in to comment.