-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,524 additions
and
1,508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from contextlib import nullcontext as eager_imports | ||
|
||
from ._hooks import LazyImportError | ||
from contextlib import nullcontext as eager_imports | ||
|
||
from ._hooks import LazyImportError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,43 @@ | ||
import argparse | ||
import ast | ||
import pathlib | ||
|
||
from lazy_imports_lite._transformer import TransformModuleImports | ||
from lazy_imports_lite._utils import unparse | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
prog="lazy-imports-lite", description="Tool for various file operations." | ||
) | ||
subparsers = parser.add_subparsers( | ||
title="subcommands", dest="subcommand", help="Available subcommands" | ||
) | ||
|
||
# Subcommand for preview | ||
preview_parser = subparsers.add_parser( | ||
"preview", help="Preview the contents of a file" | ||
) | ||
preview_parser.add_argument("filename", help="Name of the file to preview") | ||
|
||
args = parser.parse_args() | ||
|
||
if args.subcommand == "preview": | ||
transformer = TransformModuleImports() | ||
code = pathlib.Path(args.filename).read_text() | ||
tree = ast.parse(code) | ||
new_tree = ast.fix_missing_locations(transformer.visit(tree)) | ||
new_code = unparse(new_tree) | ||
print(new_code) | ||
|
||
else: | ||
print( | ||
"Error: Please specify a valid subcommand. Use 'preview --help' for more information." | ||
) | ||
exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
import argparse | ||
import ast | ||
import pathlib | ||
import sys | ||
|
||
from lazy_imports_lite._transformer import TransformModuleImports | ||
from lazy_imports_lite._utils import unparse | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
prog="lazy-imports-lite", description="Tool for various file operations." | ||
) | ||
subparsers = parser.add_subparsers( | ||
title="subcommands", dest="subcommand", help="Available subcommands" | ||
) | ||
|
||
# Subcommand for preview | ||
preview_parser = subparsers.add_parser( | ||
"preview", help="Preview the contents of a file" | ||
) | ||
preview_parser.add_argument("filename", help="Name of the file to preview") | ||
|
||
args = parser.parse_args() | ||
|
||
if args.subcommand == "preview": | ||
transformer = TransformModuleImports() | ||
code = pathlib.Path(args.filename).read_text() | ||
tree = ast.parse(code) | ||
new_tree = ast.fix_missing_locations(transformer.visit(tree)) | ||
new_code = unparse(new_tree) | ||
print(new_code) | ||
|
||
else: | ||
print( | ||
"Error: Please specify a valid subcommand. Use 'preview --help' for more information.", | ||
file=sys.stderr, | ||
) | ||
exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,103 @@ | ||
import importlib | ||
from collections import defaultdict | ||
|
||
|
||
class LazyObject: | ||
__slots__ = ("v",) | ||
|
||
|
||
class LazyImportError(BaseException): | ||
def __init__(self, module, package): | ||
self.module = module | ||
self.package = package | ||
|
||
def __str__(self): | ||
if self.package is None: | ||
return f"Deferred importing of module '{self.module}' caused an error" | ||
else: | ||
return f"Deferred importing of module '{self.module}' in '{self.package}' caused an error" | ||
|
||
|
||
class ImportFrom(LazyObject): | ||
__slots__ = ("package", "module", "name", "v") | ||
|
||
def __init__(self, package, module, name): | ||
self.package = package | ||
self.module = module | ||
self.name = name | ||
|
||
def __getattr__(self, name): | ||
if name == "v": | ||
module = safe_import(self.module, self.package) | ||
try: | ||
attr = getattr(module, self.name) | ||
except AttributeError: | ||
attr = safe_import(self.module + "." + self.name, self.package) | ||
self.v = attr | ||
return attr | ||
else: | ||
assert False | ||
|
||
|
||
pending_imports = defaultdict(list) | ||
imported_modules = set() | ||
|
||
|
||
def safe_import(module, package=None): | ||
try: | ||
return importlib.import_module(module, package) | ||
except LazyImportError: | ||
raise | ||
except: | ||
raise LazyImportError(module, package) | ||
|
||
|
||
class Import(LazyObject): | ||
__slots__ = ("module", "v") | ||
|
||
def __init__(self, module): | ||
self.module = module | ||
m = self.module.split(".")[0] | ||
|
||
if m in imported_modules: | ||
safe_import(self.module) | ||
else: | ||
pending_imports[m].append(module) | ||
|
||
def __getattr__(self, name): | ||
if name == "v": | ||
m = self.module.split(".")[0] | ||
for pending in pending_imports[m]: | ||
safe_import(pending) | ||
result = safe_import(self.module.split(".")[0]) | ||
imported_modules.add(m) | ||
self.v = result | ||
return result | ||
else: | ||
assert False | ||
|
||
|
||
class ImportAs(LazyObject): | ||
__slots__ = ("module", "v") | ||
|
||
def __init__(self, module): | ||
self.module = module | ||
|
||
def __getattr__(self, name): | ||
if name == "v": | ||
module = safe_import(self.module) | ||
self.v = module | ||
return module | ||
else: | ||
assert False | ||
|
||
|
||
def make_globals(global_provider): | ||
def g(): | ||
return { | ||
k: v.v if isinstance(v, LazyObject) else v | ||
for k, v in dict(global_provider()).items() | ||
if k not in ("globals", "__lazy_imports_lite__") | ||
} | ||
|
||
return g | ||
import importlib | ||
from collections import defaultdict | ||
|
||
|
||
class LazyObject: | ||
__slots__ = ("v",) | ||
|
||
|
||
class LazyImportError(BaseException): | ||
def __init__(self, module, package): | ||
self.module = module | ||
self.package = package | ||
|
||
def __str__(self): | ||
if self.package is None: | ||
return f"Deferred importing of module '{self.module}' caused an error" | ||
else: | ||
return f"Deferred importing of module '{self.module}' in '{self.package}' caused an error" | ||
|
||
|
||
class ImportFrom(LazyObject): | ||
__slots__ = ("package", "module", "name", "v") | ||
|
||
def __init__(self, package, module, name): | ||
self.package = package | ||
self.module = module | ||
self.name = name | ||
|
||
def __getattr__(self, name): | ||
if name == "v": | ||
module = safe_import(self.module, self.package) | ||
try: | ||
attr = getattr(module, self.name) | ||
except AttributeError: | ||
attr = safe_import(self.module + "." + self.name, self.package) | ||
self.v = attr | ||
return attr | ||
else: | ||
assert False | ||
|
||
|
||
pending_imports = defaultdict(list) | ||
imported_modules = set() | ||
|
||
|
||
def safe_import(module, package=None): | ||
try: | ||
return importlib.import_module(module, package) | ||
except LazyImportError: | ||
raise | ||
except: | ||
raise LazyImportError(module, package) | ||
|
||
|
||
class Import(LazyObject): | ||
__slots__ = ("module", "v") | ||
|
||
def __init__(self, module): | ||
self.module = module | ||
m = self.module.split(".")[0] | ||
|
||
if m in imported_modules: | ||
safe_import(self.module) | ||
else: | ||
pending_imports[m].append(module) | ||
|
||
def __getattr__(self, name): | ||
if name == "v": | ||
m = self.module.split(".")[0] | ||
for pending in pending_imports[m]: | ||
safe_import(pending) | ||
result = safe_import(self.module.split(".")[0]) | ||
imported_modules.add(m) | ||
self.v = result | ||
return result | ||
else: | ||
assert False | ||
|
||
|
||
class ImportAs(LazyObject): | ||
__slots__ = ("module", "v") | ||
|
||
def __init__(self, module): | ||
self.module = module | ||
|
||
def __getattr__(self, name): | ||
if name == "v": | ||
module = safe_import(self.module) | ||
self.v = module | ||
return module | ||
else: | ||
assert False | ||
|
||
|
||
def make_globals(global_provider): | ||
def g(): | ||
return { | ||
k: v.v if isinstance(v, LazyObject) else v | ||
for k, v in dict(global_provider()).items() | ||
if k not in ("globals", "__lazy_imports_lite__") | ||
} | ||
|
||
return g |
Oops, something went wrong.