From 6513de42cb81ce1de2b51cd2eb56b016f8ef6156 Mon Sep 17 00:00:00 2001 From: Karol Zak Date: Mon, 7 Sep 2020 23:26:33 +0200 Subject: [PATCH] upd imports --- pre_commit_nb/common.py | 73 +++++++++++++++++++++-------------------- setup.py | 2 +- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/pre_commit_nb/common.py b/pre_commit_nb/common.py index f5a3392..778ad71 100644 --- a/pre_commit_nb/common.py +++ b/pre_commit_nb/common.py @@ -4,9 +4,6 @@ import subprocess import uuid -from pre_commit_nb.base64_to_external_storage import base64_to_blob_storage -from pre_commit_nb.base64_to_image_files import base64_to_local_file - def create_nb_cell_output(url: str) -> str: return """"text/html": [ @@ -39,44 +36,50 @@ def process_nb( print("Processing %s" % filename) with open(filename, 'r') as file: org_data = " ".join(file.readlines()) - data = org_data - matches = re.findall( - r"\"image/(?:gif|png|jpeg|bmp|webp)\": \".*[a-zA-Z0-9+/=]\"", - data) + data = org_data + matches = re.findall( + r"\"image/(?:gif|png|jpeg|bmp|webp)\": \".*[a-zA-Z0-9+/=]\"", + data) - new_files = "" + new_files = "" - for match in matches: - ext = "." + re.findall(r"image/[a-zA-Z]*", match)[0].split('/')[1] - image_path = "nb_images" + "/" + str(uuid.uuid4()) + ext + for match in matches: + ext = "." + re.findall(r"image/[a-zA-Z]*", match)[0].split('/')[1] + image_path = "nb_images" + "/" + str(uuid.uuid4()) + ext - full_path = "./" + os.path.dirname(filename) + "/" + image_path + full_path = "./" + os.path.dirname(filename) + "/" + image_path - base64_string = ( - match.split(':')[1] - .replace('"', '') - .replace(' ', '') - .replace('\\n', '') - ) + base64_string = ( + match.split(':')[1] + .replace('"', '') + .replace(' ', '') + .replace('\\n', '') + ) - if az_blob_container_url: - response_status, url_path = base64_to_blob_storage( - base64_string, az_blob_container_url, full_path - ) + if az_blob_container_url: + from pre_commit_nb.base64_to_external_storage import ( + base64_to_blob_storage) - if response_status >= 200 and response_status < 300: - print(f"Successfully uploaded image to blob storage: {url_path}") # NOQA E501 - else: - print(f"Uploading process failed with response code: {response_status}") # NOQA E501 + response_status, url_path = base64_to_blob_storage( + base64_string, az_blob_container_url, full_path + ) + + if response_status >= 200 and response_status < 300: + print(f"Successfully uploaded image to blob storage: {url_path}") # NOQA E501 else: - print("Converting base64 to image file and saving as %s" % full_path) # NOQA E501 - base64_to_local_file( - base64_string, full_path - ) - url_path = "./" + image_path - new_files += " " + full_path + print(f"Uploading process failed with response code: {response_status}") # NOQA E501 + else: + from pre_commit_nb.base64_to_image_files import ( + base64_to_local_file) + + print(f"Converting base64 to image file and saving as {full_path}") + base64_to_local_file( + base64_string, full_path + ) + url_path = "./" + image_path + new_files += " " + full_path - data = data.replace(match, create_nb_cell_output(url_path)) + data = data.replace(match, create_nb_cell_output(url_path)) if org_data != data: with open(filename, 'w') as file: @@ -85,12 +88,12 @@ def process_nb( new_files = new_files.strip() if add_changes_to_staging: - print("'--add_changes_to_staging' flag set to 'True' - adding new and changed files to staging...") # NOQA E501 + print("'--add-changes-to-staging' flag set to 'True' - adding new and changed files to staging...") # NOQA E501 print(new_files) git_add(new_files) if auto_commit_changes: - print("'--auto_commit_changes' flag set to 'True' - git hook set to return exit code 0.") # NOQA E501 + print("'--auto-commit-changes' flag set to 'True' - git hook set to return exit code 0.") # NOQA E501 return 0 return 1 diff --git a/setup.py b/setup.py index e9aed8c..e094683 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name="pre_commit_nb", - version="0.2.2a2", + version="0.2.2a3", description="Set of git pre-commit hooks for Jupyter Notebooks compatible with https://pre-commit.com/ framework", # NOQA E501 long_description=long_description, long_description_content_type="text/markdown", # This is important!