Skip to content

Commit

Permalink
Merge pull request #99 from timkpaine/tkp/lint
Browse files Browse the repository at this point in the history
Move from black to ruff for autoformatting
  • Loading branch information
timkpaine authored Feb 6, 2024
2 parents e11f02f + cd51c26 commit 1f836ac
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 93 deletions.
12 changes: 5 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ install: ## install to site-packages
###########
# Testing #
###########
testpy: ## Clean and Make unit tests
testpy: ## clean and Make unit tests
python -m pytest -v jupyterlab_email/tests --junitxml=junit.xml --cov=jupyterlab_email --cov-report=xml:.coverage.xml --cov-branch --cov-fail-under=15 --cov-report term-missing

testjs: ## Clean and Make js tests
testjs: ## clean and Make js tests
cd js; yarn test

test: tests
Expand All @@ -27,18 +27,16 @@ tests: testpy testjs ## run the tests
###########
# Linting #
###########
lintpy: ## Black/flake8 python
lintpy: ## lint python with ruff
python -m ruff jupyterlab_email setup.py
python -m black --check jupyterlab_email setup.py

lintjs: ## ESlint javascript
cd js; yarn lint

lint: lintpy lintjs ## run linter

fixpy: ## Black python
python -m ruff jupyterlab_email setup.py --fix
python -m black jupyterlab_email/ setup.py
fixpy: ## autoformat python with ruff
python -m ruff format jupyterlab_email setup.py

fixjs: ## ESlint Autofix JS
cd js; yarn fix
Expand Down
20 changes: 5 additions & 15 deletions jupyterlab_email/_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ def make_email(
nb, error = run(to=type_to, name=name, in_=model, template=template, logger=logger)

if error:
message = emails.html(
charset="utf-8", subject=subject, html=nb, mail_from=from_
)
message = emails.html(charset="utf-8", subject=subject, html=nb, mail_from=from_)
return message, error

if not nb:
Expand All @@ -75,9 +73,7 @@ def make_email(

if error:
# from pdf or html conversion specifically
message = emails.html(
charset="utf-8", subject=subject, html=nb, mail_from=from_
)
message = emails.html(charset="utf-8", subject=subject, html=nb, mail_from=from_)
return message, error

if type == "email":
Expand All @@ -88,9 +84,7 @@ def make_email(
item.decompose()

# strip matplotlib base outs
for item in soup.find_all(
"div", class_="output_text output_subarea output_execute_result"
):
for item in soup.find_all("div", class_="output_text output_subarea output_execute_result"):
for c in item.contents:
if "<matplotlib" in str(c):
item.decompose()
Expand All @@ -109,9 +103,7 @@ def make_email(
for i, img in enumerate(imgs):
if not img.get("localdata"):
continue
imgs_to_attach[
img.get("cell_id") + "_" + str(i) + ".png"
] = base64.b64decode(img.get("localdata"))
imgs_to_attach[img.get("cell_id") + "_" + str(i) + ".png"] = base64.b64decode(img.get("localdata"))
img["src"] = "cid:" + img.get("cell_id") + "_" + str(i) + ".png"
# encoders.encode_base64(part)
del img["localdata"]
Expand Down Expand Up @@ -152,9 +144,7 @@ def make_email(

# assemble email soup
soup = str(soup)
message = emails.html(
charset="utf-8", subject=subject, html=soup, mail_from=from_
)
message = emails.html(charset="utf-8", subject=subject, html=soup, mail_from=from_)

for img, data in iteritems(imgs_to_attach):
message.attach(filename=img, content_disposition="inline", data=data)
Expand Down
45 changes: 11 additions & 34 deletions jupyterlab_email/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,26 @@ def load_jupyter_server_extension(nb_server_app):
emails = nb_server_app.config.get("JupyterLabEmail", {}).get("smtp_servers", {})

# should be a list of template paths
user_templates = nb_server_app.config.get("JupyterLabEmail", {}).get(
"templates", {}
)
user_templates = nb_server_app.config.get("JupyterLabEmail", {}).get("templates", {})

headers = nb_server_app.config.get("JupyterLabEmail", {}).get("headers", {})
footers = nb_server_app.config.get("JupyterLabEmail", {}).get("footers", {})
signatures = nb_server_app.config.get("JupyterLabEmail", {}).get("signatures", {})
postprocessors = nb_server_app.config.get("JupyterLabEmail", {}).get(
"postprocessors", {}
)
postprocessors = nb_server_app.config.get("JupyterLabEmail", {}).get("postprocessors", {})

base_url = web_app.settings["base_url"]

host_pattern = ".*$"
nb_server_app.log.info(
"Installing jupyterlab_email handler on path %s"
% url_path_join(base_url, "emails")
)
nb_server_app.log.info(
"Available email servers: %s" % ",".join(k["name"] for k in emails)
)
nb_server_app.log.info("Installing jupyterlab_email handler on path %s" % url_path_join(base_url, "emails"))
nb_server_app.log.info("Available email servers: %s" % ",".join(k["name"] for k in emails))

for k in emails:
if "password" in k:
nb_server_app.log.info(
"WARNING!!! You should not store your password in jupyter_notebook_config.py!!!"
)
nb_server_app.log.info("WARNING!!! You should not store your password in jupyter_notebook_config.py!!!")
elif "function" in k:
nb_server_app.log.info(
"Skipping password input for %s@%s" % (k["username"], k["name"])
)
nb_server_app.log.info("Skipping password input for %s@%s" % (k["username"], k["name"]))
else:
k["password"] = getpass(
"Input password for %s@%s:" % (k["username"], k["name"])
)
k["password"] = getpass("Input password for %s@%s:" % (k["username"], k["name"]))

context = {}
context["emails"] = emails
Expand All @@ -60,21 +45,15 @@ def load_jupyter_server_extension(nb_server_app):
context["postprocessors"] = postprocessors
context["user_templates"] = user_templates
context["templates"] = {}
context["templates"]["email"] = os.path.join(
os.path.dirname(__file__), "templates", "html_email.tpl"
)
context["templates"]["email"] = os.path.join(os.path.dirname(__file__), "templates", "html_email.tpl")
context["templates"]["email_nocode"] = os.path.join(
os.path.dirname(__file__), "templates", "hide_code_cells_html_email.tpl"
)
context["templates"]["html"] = os.path.join(
os.path.dirname(__file__), "templates", "html.tpl"
)
context["templates"]["html"] = os.path.join(os.path.dirname(__file__), "templates", "html.tpl")
context["templates"]["html_nocode"] = os.path.join(
os.path.dirname(__file__), "templates", "hide_code_cells_html.tpl"
)
context["templates"]["pdf"] = os.path.join(
os.path.dirname(__file__), "templates", "pdf.tplx"
)
context["templates"]["pdf"] = os.path.join(os.path.dirname(__file__), "templates", "pdf.tplx")
context["templates"]["pdf_nocode"] = os.path.join(
os.path.dirname(__file__), "templates", "hide_code_cells_pdf.tplx"
)
Expand All @@ -86,6 +65,4 @@ def load_jupyter_server_extension(nb_server_app):
host_pattern,
[(url_path_join(base_url, "email/get"), EmailsListHandler, context)],
)
web_app.add_handlers(
host_pattern, [(url_path_join(base_url, "email/run"), EmailHandler, context)]
)
web_app.add_handlers(host_pattern, [(url_path_join(base_url, "email/run"), EmailHandler, context)])
12 changes: 3 additions & 9 deletions jupyterlab_email/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,9 @@ def get(self):
ret = {}
ret["emails"] = [_["name"] for _ in self.emails]
ret["templates"] = [_ for _ in self.templates]
ret["user_templates"] = (
[""] + [_ for _ in self.user_templates] if self.user_templates else []
)
ret["user_templates"] = [""] + [_ for _ in self.user_templates] if self.user_templates else []
ret["headers"] = [""] + [_ for _ in self.headers] if self.headers else []
ret["footers"] = [""] + [_ for _ in self.footers] if self.footers else []
ret["signatures"] = (
[""] + [_ for _ in self.signatures] if self.signatures else []
)
ret["postprocessors"] = (
[""] + [_ for _ in self.postprocessors] if self.postprocessors else []
)
ret["signatures"] = [""] + [_ for _ in self.signatures] if self.signatures else []
ret["postprocessors"] = [""] + [_ for _ in self.postprocessors] if self.postprocessors else []
self.finish(json.dumps(ret))
12 changes: 3 additions & 9 deletions jupyterlab_email/nbconvert.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ def run(

# reexecute if needed
if execute:
argv.extend(
["--execute", "--ExecutePreprocessor.timeout=" + str(execute_timeout)]
)
argv.extend(["--execute", "--ExecutePreprocessor.timeout=" + str(execute_timeout)])

# pass in template arg
if template:
Expand All @@ -76,9 +74,7 @@ def run(
error = error.decode("ascii")

# extract out the cell error
m = re.search(
".*CellExecutionError:(?P<CellError>(.*\n)*)", error, flags=re.MULTILINE
)
m = re.search(".*CellExecutionError:(?P<CellError>(.*\n)*)", error, flags=re.MULTILINE)

g = m.groupdict()

Expand All @@ -92,9 +88,7 @@ def run(
for color, reg in _COLOR_CODES.items():
err = re.sub(
reg,
'<span style="color: {color}">{err}</span> '.format(
color=color, err=err
),
'<span style="color: {color}">{err}</span> '.format(color=color, err=err),
)

# remove and closers
Expand Down
20 changes: 5 additions & 15 deletions jupyterlab_email/postprocessors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from bs4 import Tag, NavigableString


def pivot_pandas_to_excel(
soup, show_intermediate_breakdown=False, show_total_breakdown=False
):
def pivot_pandas_to_excel(soup, show_intermediate_breakdown=False, show_total_breakdown=False):
"""pandas style pivot to excel style pivot formatting for outlook/html
This function is meant to be provided to the email functionality as a postprocessor.
Expand Down Expand Up @@ -41,9 +39,7 @@ def pivot_pandas_to_excel(

# max number of columns so table is even
num_columns_max = max(len(row.findAll()) for row in table.tbody.findAll("tr"))
num_headers_max = max(
len(row.findAll("th")) for row in table.tbody.findAll("tr")
)
num_headers_max = max(len(row.findAll("th")) for row in table.tbody.findAll("tr"))

# for special case where we delete the summation rows
last = False
Expand Down Expand Up @@ -87,9 +83,7 @@ def pivot_pandas_to_excel(
attrs={
"class": "empty_pivot_row_first",
"style": "margin-left:"
+ str(
10 * (num_headers_max - len(headers) + indent)
)
+ str(10 * (num_headers_max - len(headers) + indent))
+ "px;",
},
)
Expand All @@ -99,9 +93,7 @@ def pivot_pandas_to_excel(
attrs={
"class": "empty_pivot_row",
"style": "margin-left:"
+ str(
10 * (num_headers_max - len(headers) + indent)
)
+ str(10 * (num_headers_max - len(headers) + indent))
+ "px;",
},
)
Expand All @@ -114,9 +106,7 @@ def pivot_pandas_to_excel(
for j in range(num_columns_max - 1):
new_row.insert(
j + 1,
Tag(
name="td", attrs={"class": "empty_pivot_row_first"}
),
Tag(name="td", attrs={"class": "empty_pivot_row_first"}),
) if first_header else new_row.insert(
j + 1,
Tag(name="td", attrs={"class": "empty_pivot_row"}),
Expand Down
4 changes: 1 addition & 3 deletions jupyterlab_email/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@

class TestInit:
def test__jupyter_server_extension_paths(self):
assert _jupyter_server_extension_paths() == [
{"module": "jupyterlab_email.extension"}
]
assert _jupyter_server_extension_paths() == [{"module": "jupyterlab_email.extension"}]
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ dependencies = [

[project.optional-dependencies]
develop = [
"black>=23",
"check-manifest",
"ruff",
"pytest",
Expand Down

0 comments on commit 1f836ac

Please sign in to comment.