Skip to content

Commit

Permalink
upd imports
Browse files Browse the repository at this point in the history
  • Loading branch information
karolzak committed Sep 7, 2020
1 parent 0460698 commit 6513de4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
73 changes: 38 additions & 35 deletions pre_commit_nb/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit 6513de4

Please sign in to comment.