From 114771b532c0d6a95082f8ee694e4b338323f60e Mon Sep 17 00:00:00 2001 From: Connor Ferster Date: Thu, 3 Jun 2021 22:03:46 -0700 Subject: [PATCH] Merged: #81 from Jim Zwartveld; Feature: Intertext as per #82 --- handcalcs/__init__.py | 3 +- handcalcs/handcalcs.py | 6 +- handcalcs/install_templates.py | 124 -------------------------- pyproject.toml | 6 +- setup.py | 5 +- test_handcalcs/test_handcalcs_file.py | 70 --------------- 6 files changed, 8 insertions(+), 206 deletions(-) delete mode 100644 handcalcs/install_templates.py diff --git a/handcalcs/__init__.py b/handcalcs/__init__.py index 69e89ac..bc901d7 100644 --- a/handcalcs/__init__.py +++ b/handcalcs/__init__.py @@ -12,8 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "1.3.0" # +__version__ = "1.4.0" # from handcalcs.decorator import handcalc -# from handcalcs.install_templates import install_html, install_latex __all__ = ["handcalc"] diff --git a/handcalcs/handcalcs.py b/handcalcs/handcalcs.py index 605774e..184d012 100644 --- a/handcalcs/handcalcs.py +++ b/handcalcs/handcalcs.py @@ -1123,8 +1123,8 @@ def convert_param_to_long(line: ParameterLine): return line -@convert_applicable_long_lines.register(ParameterLine) -def convert_param_to_long(line: ParameterLine): +@convert_applicable_long_lines.register(IntertextLine) +def convert_intertext_to_long(line: IntertextLine): return line @@ -1379,7 +1379,7 @@ def format_symbolic_line(line: SymbolicLine) -> SymbolicLine: @format_lines.register(IntertextLine) def format_intertext_line(line: IntertextLine) -> IntertextLine: cleaned_line = line.line.replace("##", "") - line.latex = f"\\intertext{cleaned_line}\\\n" + line.latex = f"& \\textrm{{{cleaned_line}}}" return line diff --git a/handcalcs/install_templates.py b/handcalcs/install_templates.py deleted file mode 100644 index 9ccff0b..0000000 --- a/handcalcs/install_templates.py +++ /dev/null @@ -1,124 +0,0 @@ -import os -import pathlib -import shutil -import nbconvert - -__all__ = ["install_latex", "install_html"] - -def install_latex(swap_in: str = "", swap_out: str = "article.tplx", restore: bool = False) -> None: - """ - Replaces the file 'swap_out' with the file 'swap_in'. The swapped in - file will be copied to the location of 'swap_out' and be given its file name. - The swapped out file is simply renamed with the string "_swapped" at the end so that - it can be restored. - - Parameters: - - 'swap_in' - The name of the template file to swap in. - If left as "", then install by swap will print the list of available - .tplx files located in the handcalcs/templates directory - - 'swap_out' - The name of the template file in the nbconvert/templates/latex directory - to swap_out. Default is "article.tplx", the default latex template for nbconvert. - - 'restore' - Default is False. If True, the nbconvert templates directory is searched for - a file with the name of 'swap_out' + "_swapped.tplx". If found, then any file in the - templates directory that has the name of 'swap_out' is deleted and the file with the - name 'swap_out' + "_swapped.tplx" is renamed to just the name of 'swap_out'. - """ - handcalcs_templates, nbconvert_templates_dir = retrieve_template_paths('latex') - available_templates = handcalcs_templates.glob("*.tplx") - if not swap_in and not restore: - print("Available templates: \n", [template.name for template in available_templates]) - return - - if restore: - restore_swapped(swap_out, nbconvert_templates_dir) - return - - swapped_name = pathlib.Path(swap_out).stem + "_swapped" + pathlib.Path(swap_out).suffix - if (nbconvert_templates_dir / swapped_name).exists() and swap_in: - print(f"Cannot install {swap_in} because {swapped_name} has not been restored.\n" - "Run this function again with restore=True first.") - return - - perform_template_swap(nbconvert_templates_dir, handcalcs_templates, swap_in, swap_out, swapped_name) - return - - -def install_html(swap_in: str = "", swap_out: str = "full.tpl", restore: bool = False) -> None: - """ - Replaces the file 'swap_out' with the file 'swap_in'. The swapped in - file will be copied to the location of 'swap_out' and be given its file name. - The swapped out file is simply renamed with the string "_swapped" at the end so that - it can be restored. - - Parameters: - - 'swap_in' - The name of the template file to swap in. - If left as "", then install by swap will print the list of available - .tpl files located in the handcalcs/templates directory - - 'swap_out' - The name of the template file in the nbconvert/templates/latex directory - to swap_out. Default is "full.tpl", the default html template for nbconvert. - - 'restore' - Default is False. If True, the nbconvert templates directory is searched for - a file with the name of 'swap_out' + "_swapped.tpl". If found, then any file in the - templates directory that has the name of 'swap_out' is deleted and the file with the - name 'swap_out' + "_swapped.tpl" is renamed to just the name of 'swap_out'. - """ - handcalcs_templates, nbconvert_templates_dir = retrieve_template_paths('html') - available_templates = handcalcs_templates.glob("*.tpl") - if not swap_in and not restore: - print("Available templates: \n", [template.name for template in available_templates]) - return - - if restore: - restore_swapped(swap_out, nbconvert_templates_dir) - return - - swapped_name = pathlib.Path(swap_out).stem + "_swapped" + pathlib.Path(swap_out).suffix - if (nbconvert_templates_dir / swapped_name).exists() and swap_in: - print(f"Cannot install {swap_in} because {swapped_name} has not been restored.\n" - "Run this function again with restore=True first.") - return - - perform_template_swap(nbconvert_templates_dir, handcalcs_templates, swap_in, swap_out, swapped_name) - return - - -def perform_template_swap(nbconvert_templates_dir: pathlib.Path, handcalcs_templates: pathlib.Path, swap_in: str, swap_out: str, swapped_name: str): - os.rename(nbconvert_templates_dir / swap_out, nbconvert_templates_dir / swapped_name) - incoming_template = handcalcs_templates / swap_in - shutil.copyfile(incoming_template, nbconvert_templates_dir / swap_out) - success_msg = (f"{nbconvert_templates_dir / swap_out}\n -is now- \n{nbconvert_templates_dir / swapped_name}.\n\n" - f"{incoming_template}\n -is now- \n{nbconvert_templates_dir / swap_out}.") - print(success_msg) - - -def retrieve_template_paths(template_type: str): - """ - 'template_type' is one of "html" or "latex" - """ - here = pathlib.Path(__file__).resolve() - templates = here.parent / "templates" / template_type - nbconvert_templates_dir = pathlib.Path(nbconvert.__file__).resolve().parent / "templates" / template_type - return templates, nbconvert_templates_dir - - -def restore_swapped(swap_file: pathlib.Path, nbconvert_templates_dir: pathlib.Path): - """ - swap_file is an absolute path - """ - swapped_name = pathlib.Path(swap_file).stem + "_swapped" + pathlib.Path(swap_file).suffix - if (nbconvert_templates_dir / swapped_name).exists(): - if (nbconvert_templates_dir / swap_file).exists(): - os.remove(nbconvert_templates_dir / swap_file) - os.rename(nbconvert_templates_dir / swapped_name, nbconvert_templates_dir / swap_file) - print(f"{nbconvert_templates_dir / swapped_name}\n-was restored to-\n{nbconvert_templates_dir / swap_file}") - return - print(f"Cannot restore. No nbconvert template exists with name {swapped_name}") - - - - diff --git a/pyproject.toml b/pyproject.toml index 27e63de..9cf20de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ show_missing = true [tool.poetry] name = "handcalcs" -version = "1.3.0" +version = "1.4.0" description = "Python calculations in Jupyter as though you wrote them by hand." authors = ["Connor Ferster"] license = "Apache-2.0" @@ -29,13 +29,9 @@ innerscope = "^0.2.0" [tool.poetry.dev-dependencies] pytest-cov = "^2.9.0" coverage = {extras = ["toml"], version = "^5.1"} -<<<<<<< HEAD jupyterlab = "^3.0.0" -======= -jupyterlab = "^2.1.5" nbconvert = "^6.0.0" pdbpp = "^0.10.2" ->>>>>>> f6b78140879f6ed864f7a9befda98932d8260aa6 ipython = "^7.18.1" sympy = "^1.6.2" forallpeople = "^2.0.1" diff --git a/setup.py b/setup.py index b49140c..ef7aa42 100644 --- a/setup.py +++ b/setup.py @@ -18,14 +18,15 @@ EMAIL = 'connorferster@gmail.com' AUTHOR = 'Connor Ferster' REQUIRES_PYTHON = '>=3.7.0' -VERSION = '1.3.0' +VERSION = '1.4.0' # What packages are required for this module to be executed? REQUIRED = [ 'pyparsing', 'nbconvert>=6.0.0', 'innerscope >= 0.2.0', - 'more-itertools >= 8.5.0' + 'more-itertools >= 8.5.0', + 'jupyterlab >= 3.0.0' # 'requests', 'maya', 'records', ] diff --git a/test_handcalcs/test_handcalcs_file.py b/test_handcalcs/test_handcalcs_file.py index fb40531..77e9936 100644 --- a/test_handcalcs/test_handcalcs_file.py +++ b/test_handcalcs/test_handcalcs_file.py @@ -30,7 +30,6 @@ from handcalcs.handcalcs import ParameterLine, CalcLine, LongCalcLine, ConditionalLine from handcalcs.handcalcs import ParameterCell, LongCalcCell, CalcCell from handcalcs.decorator import handcalc -from handcalcs.install_templates import install_html, install_latex # When writing a new test create a new "cell" .py file @@ -281,88 +280,19 @@ def test_handcalcs3(): def test_latex_exporter(): - """ - - """ - exporter = LatexHideInputExporter() assert exporter.exclude_input == True def test_pdf_exporter(): - """ - - """ - exporter = PDFHideInputExporter() assert exporter.exclude_input == True def test_html_exporter(): - """ - - """ - exporter = HTMLHideInputExporter() assert exporter.exclude_input == True - -# def test_install_latex(capsys): -# HERE = pathlib.Path(__file__).resolve().parent -# TEMPLATES = HERE.parent / "handcalcs" / "templates" -# MAIN_TEMPLATE = TEMPLATES / "latex" / "t-makaro_classic_romanoutput_noinput.tplx" -# NBCONVERT_TEMPLATES_DIR = ( -# pathlib.Path(nbconvert.__file__).resolve().parent / "templates" / "latex" -# ) -# -# install_latex() -# captured = capsys.readouterr() -# assert ( -# captured.out -# == "Available templates: \n ['t-makaro_classic_romanoutput_noinput.tplx']\n" -# ) -# install_latex(swap_in="t-makaro_classic_romanoutput_noinput.tplx") -# assert filecmp.cmp(MAIN_TEMPLATE, NBCONVERT_TEMPLATES_DIR / "article.tplx") -# -# -# def test_install_latex_restore(): -# HERE = pathlib.Path(__file__).resolve().parent -# TEMPLATES = HERE.parent / "handcalcs" / "templates" -# MAIN_TEMPLATE = TEMPLATES / "latex" / "t-makaro_classic_romanoutput_noinput.tplx" -# NBCONVERT_TEMPLATES_DIR = ( -# pathlib.Path(nbconvert.__file__).resolve().parent / "templates" / "latex" -# ) -# -# install_latex(restore=True) -# assert not filecmp.cmp(MAIN_TEMPLATE, NBCONVERT_TEMPLATES_DIR / "article.tplx") -# -# -# def test_install_html(capsys): -# HERE = pathlib.Path(__file__).resolve().parent -# TEMPLATES = HERE.parent / "handcalcs" / "templates" -# MAIN_TEMPLATE = TEMPLATES / "html" / "full_html_noinputs.tpl" -# NBCONVERT_TEMPLATES_DIR = ( -# pathlib.Path(nbconvert.__file__).resolve().parent / "templates" / "html" -# ) -# -# install_html() -# captured = capsys.readouterr() -# assert captured.out == "Available templates: \n ['full_html_noinputs.tpl']\n" -# install_html(swap_in="full_html_noinputs.tpl") -# assert filecmp.cmp(MAIN_TEMPLATE, NBCONVERT_TEMPLATES_DIR / "full.tpl") -# -# -# def test_install_html_restore(): -# HERE = pathlib.Path(__file__).resolve().parent -# TEMPLATES = HERE.parent / "handcalcs" / "templates" -# MAIN_TEMPLATE = TEMPLATES / "html" / "full_html_noinputs.tpl" -# NBCONVERT_TEMPLATES_DIR = ( -# pathlib.Path(nbconvert.__file__).resolve().parent / "templates" / "html" -# ) -# install_html(restore=True) -# assert not filecmp.cmp(MAIN_TEMPLATE, NBCONVERT_TEMPLATES_DIR / "full.tpl") - - # Test expected exceptions