Skip to content

Commit

Permalink
fix: fixed windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Feb 9, 2024
1 parent 0e74eb0 commit 3c5985c
Show file tree
Hide file tree
Showing 9 changed files with 1,566 additions and 1,550 deletions.
6 changes: 3 additions & 3 deletions src/lazy_imports_lite/__init__.py
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
84 changes: 43 additions & 41 deletions src/lazy_imports_lite/__main__.py
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()
206 changes: 103 additions & 103 deletions src/lazy_imports_lite/_hooks.py
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
Loading

0 comments on commit 3c5985c

Please sign in to comment.