Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Latrova committed Nov 7, 2023
1 parent 0657a6d commit e37ba5e
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions pipreqs/pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,15 @@
from yarg import json2package
from yarg.exceptions import HTTPError

try:
PythonExporter = None
ignore_notebooks = False
from nbconvert import PythonExporter
except ImportError:
pass

from pipreqs import __version__

REGEXP = [
re.compile(r"^import (.+)$"),
re.compile(r"^from ((?!\.+).*?) import (?:.*)$"),
]

ignore_notebooks = False


@contextmanager
def _open(filename=None, mode="r"):
Expand Down Expand Up @@ -117,22 +112,20 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links
ignore_dirs_parsed.append(os.path.basename(os.path.realpath(e)))
ignore_dirs.extend(ignore_dirs_parsed)

def get_file_extensions():
return [".py"] if ignore_notebooks else [".py", ".ipynb"]

extensions = get_file_extensions()

walk = os.walk(path, followlinks=follow_links)
for root, dirs, files in walk:
dirs[:] = [d for d in dirs if d not in ignore_dirs]

candidates.append(os.path.basename(root))
if notebooks_are_enabled():
files = [fn for fn in files if file_ext_is_allowed(fn, [".py", ".ipynb"])]
else:
files = [fn for fn in files if file_ext_is_allowed(fn, [".py"])]
py_files = [file for file in files if file_ext_is_allowed(file, [".py"])]
candidates.extend([os.path.splitext(filename)[0] for filename in py_files])

candidates = list(
map(
lambda fn: os.path.splitext(fn)[0],
filter(lambda fn: file_ext_is_allowed(fn, [".py"]), files),
)
)
files = [fn for fn in files if file_ext_is_allowed(fn, extensions)]

for file_name in files:
file_name = os.path.join(root, file_name)
Expand Down Expand Up @@ -178,10 +171,6 @@ def get_all_imports(path, encoding="utf-8", extra_ignore_dirs=None, follow_links
return list(packages - data)


def notebooks_are_enabled():
return PythonExporter and not ignore_notebooks


def file_ext_is_allowed(file_name, acceptable):
return os.path.splitext(file_name)[1] in acceptable

Expand All @@ -197,7 +186,7 @@ def ipynb_2_py(file_name, encoding="utf-8"):
str: parsed string
"""

Check warning on line 189 in pipreqs/pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 blank line contains whitespace Raw Output: ./pipreqs/pipreqs.py:189:1: W293 blank line contains whitespace
exporter = PythonExporter()

Check failure on line 190 in pipreqs/pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 undefined name 'PythonExporter' Raw Output: ./pipreqs/pipreqs.py:190:16: F821 undefined name 'PythonExporter'
(body, _) = exporter.from_filename(file_name)

Expand Down Expand Up @@ -493,7 +482,9 @@ def init(args):
encoding = args.get("--encoding")
extra_ignore_dirs = args.get("--ignore")
follow_links = not args.get("--no-follow-links")
ignore_notebooks = args.get("--ignore-notebooks")

Check warning on line 485 in pipreqs/pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 blank line contains whitespace Raw Output: ./pipreqs/pipreqs.py:485:1: W293 blank line contains whitespace
ignore_notebooks = args.get("--ignore-notebooks", False)

input_path = args["<path>"]

if encoding is None:
Expand Down

0 comments on commit e37ba5e

Please sign in to comment.