-
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 2 commits
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
alabaster==0.7.12 | ||
attrs==21.4.0 | ||
Babel==2.9.1 | ||
bleach==4.1.0 | ||
certifi==2021.10.8 | ||
charset-normalizer==2.0.11 | ||
commonmark==0.9.1 | ||
defusedxml==0.7.1 | ||
docutils==0.17.1 | ||
entrypoints==0.3 | ||
gitdb==4.0.9 | ||
GitPython==3.1.26 | ||
idna==3.3 | ||
imagesize==1.3.0 | ||
importlib-metadata==4.10.1 | ||
importlib-resources==5.4.0 | ||
ipython-genutils==0.2.0 | ||
Jinja2==3.0.3 | ||
jsonschema==4.4.0 | ||
jupyter-client==7.1.2 | ||
jupyter-core==4.9.1 | ||
jupyterlab-pygments==0.1.2 | ||
MarkupSafe==2.0.1 | ||
mistune==0.8.4 | ||
nbclient==0.5.10 | ||
nbconvert==6.4.1 | ||
nbformat==5.1.3 | ||
nest-asyncio==1.5.4 | ||
packaging==21.3 | ||
pandocfilters==1.5.0 | ||
pydantic==1.9.0 | ||
Pygments==2.11.2 | ||
pyparsing==3.0.7 | ||
pyrsistent==0.18.1 | ||
python-dateutil==2.8.2 | ||
pytz==2021.3 | ||
pyzmq==22.3.0 | ||
requests==2.27.1 | ||
rich==12.0.1 | ||
six==1.16.0 | ||
smmap==5.0.0 | ||
snowballstemmer==2.2.0 | ||
Sphinx==4.4.0 | ||
sphinx-rtd-theme==1.0.0 | ||
sphinxcontrib-applehelp==1.0.2 | ||
sphinxcontrib-devhelp==1.0.2 | ||
sphinxcontrib-htmlhelp==2.0.0 | ||
sphinxcontrib-jsmath==1.0.1 | ||
sphinxcontrib-qthelp==1.0.3 | ||
sphinxcontrib-serializinghtml==1.1.5 | ||
testpath==0.5.0 | ||
tornado==6.1 | ||
traitlets==5.1.1 | ||
typing_extensions==4.0.1 | ||
urllib3==1.26.8 | ||
webencodings==0.5.1 | ||
zipp==3.7.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,9 @@ class Notebook(RichRenderable): | |
""" | ||
|
||
def __init__(self, path: Path): | ||
self.imported_packages = None | ||
self.path: Path = path | ||
self.missing_requiremet: set | ||
|
||
# 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_packages = 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,9 +1,11 @@ | ||
import os | ||
import re | ||
import sys | ||
import tempfile | ||
import zipfile | ||
from abc import ABC | ||
from pathlib import Path | ||
from typing import List, Optional | ||
from typing import List, Optional, Pattern | ||
|
||
import git | ||
|
||
|
@@ -20,9 +22,12 @@ def __init__(self, path: Path): | |
|
||
# Repository info | ||
self.path = path | ||
self.dependencies_module: set | ||
|
||
# Extracted content | ||
self.notebooks: List[Notebook] = [] # List of Notebook objects | ||
self.dependencies_module = self._get_dependencies() | ||
self.core_library: set = self._get_core_dependecies() | ||
|
||
def retrieve_notebooks(self): | ||
|
||
|
@@ -67,6 +72,41 @@ def large_file_paths(self) -> List[Path]: | |
large_files.append(file_path) | ||
return large_files | ||
|
||
def _get_dependencies(self) -> set: | ||
dependencies = set() | ||
path = os.path.dirname(os.path.abspath(__file__)) | ||
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. We don't need to dynamically fetch this information: the path of a repository is already present as a property in the class |
||
path = os.path.dirname(path) | ||
# siccome il file di requirement si toverà nella root di | ||
# progetto ho creato un file fittizio con le stesse | ||
# informazioni per non spostare quello pre-esistente | ||
file = "kk.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. I don't understand this. 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. the requirement.txt file that is present in the repository is located inside the "docs" folder and not in the project root where it should actually be. I wrote this passage in anticipation of what I believe is the future location of "requirement.txt". If, on the other hand, it should remain there where it is, I can change the route without problems. |
||
final_path = os.path.join(path, file) | ||
with open(final_path, "r") as fi: | ||
file_row = fi.read() | ||
tmp = file_row.split("\n") | ||
for item in tmp: | ||
item.strip() | ||
dependencies.add(tuple(item.split("=="))) | ||
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. Saving dependencies as tuples is unnecessary. You can save them as simple strings, stripping the version info (which we cannot deduce from the notebook). So the 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. I have inserted the step of the split to make it easier to check with the versions of the libraries to look for. Now I fix this step. |
||
|
||
# print(dependencies) | ||
|
||
return dependencies | ||
|
||
# momentaneamente la lascio qui ma quando ma dovo la sposterò | ||
# perchè ovviamente non è una funzione che riguarda il repository | ||
def _get_core_dependecies(self) -> set: | ||
coredependecies = set() | ||
modules = sys.modules | ||
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. Are you sure that this solution is appropriate? From this SO thread I learn that:
The solution proposed in one of the answers to the same SO question seems to be a lot cleaner to me and might be the way to go: i.e., using isort's from isort.stdlibs.py39 import stdlib
for name in sorted(stdlib): print(name) |
||
pattern: Pattern[str] = re.compile(r".*\\\\Python\\\\Python37\\\\lib\\\\.*.py") | ||
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 regex is tied to your installation of Python. It would not work with a different Python version (e.g., Python 3.8) or a different virtual environment name. At least, I would generalize the Python version. 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. Of course, it was an oversight. I correct it immediately. |
||
for i in modules.items(): | ||
# print(" PPOSIZIONE 0:" + i[0]) | ||
# print(" PPOSIZIONE 1:" + str(i)) | ||
if pattern.match(str(i)): | ||
coredependecies.add(i[0]) | ||
print(coredependecies) | ||
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. I guess these are debug print statements that will be removed. |
||
|
||
return coredependecies | ||
|
||
|
||
class LocalRepository(Repository): | ||
""" | ||
|
@@ -114,7 +154,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.
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.