Skip to content

Commit

Permalink
style: remove crlf
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Feb 12, 2024
1 parent 6eb9da9 commit 6bf4988
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 484 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ repos:
rev: v1.7.5
hooks:
- id: docformatter

- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.4
hooks:
- id: remove-crlf
86 changes: 43 additions & 43 deletions src/lazy_imports_lite/__main__.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
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()
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__ = ("_lazy_value",)


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", "_lazy_value")

def __init__(self, package, module, name):
self.package = package
self.module = module
self.name = name

def __getattr__(self, name):
if name == "_lazy_value":
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._lazy_value = 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", "_lazy_value")

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 == "_lazy_value":
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._lazy_value = result
return result
else:
assert False


class ImportAs(LazyObject):
__slots__ = ("module", "_lazy_value")

def __init__(self, module):
self.module = module

def __getattr__(self, name):
if name == "_lazy_value":
module = safe_import(self.module)
self._lazy_value = module
return module
else:
assert False


def make_globals(global_provider):
def g():
return {
key: value._lazy_value if isinstance(value, LazyObject) else value
for key, value in dict(global_provider()).items()
if key not in ("globals", "__lazy_imports_lite__")
}

return g
import importlib
from collections import defaultdict


class LazyObject:
__slots__ = ("_lazy_value",)


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", "_lazy_value")

def __init__(self, package, module, name):
self.package = package
self.module = module
self.name = name

def __getattr__(self, name):
if name == "_lazy_value":
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._lazy_value = 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", "_lazy_value")

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 == "_lazy_value":
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._lazy_value = result
return result
else:
assert False


class ImportAs(LazyObject):
__slots__ = ("module", "_lazy_value")

def __init__(self, module):
self.module = module

def __getattr__(self, name):
if name == "_lazy_value":
module = safe_import(self.module)
self._lazy_value = module
return module
else:
assert False


def make_globals(global_provider):
def g():
return {
key: value._lazy_value if isinstance(value, LazyObject) else value
for key, value in dict(global_provider()).items()
if key not in ("globals", "__lazy_imports_lite__")
}

return g
Loading

0 comments on commit 6bf4988

Please sign in to comment.