-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provisional function on imports #89
base: master
Are you sure you want to change the base?
Changes from 1 commit
8b8446a
5930e7b
527c709
a501026
c928c12
88b1a91
c638c58
d6beae7
9cbed73
14f9c86
39ff970
4a94a5f
6163d3b
69cfee8
c7a9359
3c68e02
e3f90a8
4823f1f
c623078
28093f3
2138192
de8a6a1
03f7185
f24380f
4808cd3
3c569d2
2f61f4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,9 @@ class Notebook(RichRenderable): | |
""" | ||
|
||
def __init__(self, path: Path): | ||
self.imported_package = None | ||
self.path: Path = path | ||
self.missing_requiremet: set | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This information is not supposed to be stored here. It will be computed as the result of a linting function dedicated to checking the presence of import statements that do not map to the declared project requirements. |
||
|
||
# Read the notebook with nbformat | ||
with open(self.path) as f: | ||
|
@@ -44,6 +46,10 @@ def __init__(self, path: Path): | |
self.ast = ast.parse(self.script) | ||
except SyntaxError: | ||
self.has_invalid_python_syntax = True | ||
self.imported_package = self._get_import_package_set() | ||
# non posso accedere ai set creati nella classe Repository, | ||
# come dovrei procedere? | ||
# self.missing_requiremet = self.imported_package.difference(Repository.) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check should be performed within a dedicated linting function (inside |
||
|
||
@property | ||
def code_cells(self) -> List[Cell]: | ||
|
@@ -63,6 +69,20 @@ def initial_cells(self) -> List[Cell]: | |
def final_cells(self) -> List[Cell]: | ||
return self.cells[-settings.final_cells :] # noqa: E203 | ||
|
||
def _get_import_package_set(self): | ||
import_set = set() | ||
for node in ast.walk(self.ast): | ||
if isinstance(node, ast.Import): | ||
for name in node.names: | ||
import_set.add(name.name.split(".")[0]) | ||
elif isinstance(node, ast.ImportFrom): | ||
if node.level > 0: | ||
# Relative imports always refer to the current package. | ||
continue | ||
# assert node.module | ||
import_set.add(node.module.split(".")[0]) | ||
return import_set | ||
|
||
def __len__(self) -> int: | ||
return len(self.cells) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import os | ||
import sys | ||
import tempfile | ||
import zipfile | ||
from abc import ABC | ||
|
@@ -23,6 +24,8 @@ def __init__(self, path: Path): | |
|
||
# Extracted content | ||
self.notebooks: List[Notebook] = [] # List of Notebook objects | ||
self.dependencies_module: set = self._get_dependencies() | ||
self.core_library: set = self._get_core_dependecies() | ||
|
||
def retrieve_notebooks(self): | ||
|
||
|
@@ -67,6 +70,50 @@ def large_file_paths(self) -> List[Path]: | |
large_files.append(file_path) | ||
return large_files | ||
|
||
def _get_dependencies(self) -> set: | ||
dependencies = set() | ||
for root, dirs, files in os.walk(self.path): | ||
for f in files: | ||
# check if exist a requiremets.txt file | ||
if f.endswith("requirements.txt"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This recursive search is not necessary. |
||
try: | ||
with open((Path(root) / Path(f)), "r") as fi: | ||
file_row = fi.read() | ||
# print(file_row) | ||
tmp = file_row.split("\n") | ||
for item in tmp: | ||
dependencies.add(item) | ||
# dependencies.add(file_row) | ||
# dependencies.add(file_row) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you parse a package from |
||
except Exception as e: | ||
print(e) | ||
elif f.endswith("poetry.lock"): | ||
continue | ||
elif f.endswith("pyproject.toml"): | ||
continue | ||
elif f.endswith("environment.yml"): | ||
continue | ||
elif f.endswith("setup.py"): | ||
continue | ||
elif f.endswith("Pipfile"): | ||
continue | ||
# print(dependencies) | ||
|
||
return dependencies | ||
|
||
def _get_core_dependecies(self) -> set: | ||
coredependecies = set() | ||
# path = None | ||
for i in sys.path: | ||
if i.endswith("\\lib\\site-packages"): | ||
for root, dirs, files in os.walk(i): | ||
for f in files: | ||
if f.endswith(".py"): | ||
coredependecies.add(f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
break | ||
|
||
return coredependecies | ||
|
||
|
||
class LocalRepository(Repository): | ||
""" | ||
|
@@ -114,7 +161,6 @@ class GitHubRepository(Repository): | |
""" | ||
|
||
def __init__(self, github_url: str): | ||
|
||
self.url = github_url | ||
|
||
# Clone the repo in a temp directory | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.