From da10d942d26f6346d781a0d61cc0635e366a895f Mon Sep 17 00:00:00 2001 From: none Date: Wed, 8 Nov 2023 02:03:50 +0000 Subject: [PATCH] tweaked --- j2o/__main__.py | 39 +++++++++++++++++++++------------------ pyproject.toml | 8 +++----- tests/test_main.py | 27 +++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/j2o/__main__.py b/j2o/__main__.py index 9c6833d..8cec898 100644 --- a/j2o/__main__.py +++ b/j2o/__main__.py @@ -8,12 +8,11 @@ # source_filename = './draw-samples.ipynb' -DIR_TARGET = './autoimgs' +DIR_AUTOIMGS = './autoimgs' org_babel_min_lines_for_block_output = 10 # ob-core.el org-babel-min-lines-for-block-output -def jupyter2org(f:TextIOWrapper, source_file_jupyter: str): - +def jupyter2org(f:TextIOWrapper, source_file_jupyter: str, target_images_dir: str): # TODO save images to target_images_dir # PRINT = lambda *x: print("".join(x)) # f = open("out.org", "w") PRINT = lambda *x: f.write("".join(x) + '\n') @@ -47,17 +46,18 @@ def jupyter2org(f:TextIOWrapper, source_file_jupyter: str): # - 1) save image 2) insert link to output text 3) format source block header with link # - decode image and remember link to file b64img = base64.b64decode(output["data"]["image/png"]) - fpath = os.path.join(DIR_TARGET, f'{i}_{j}.png') - o["file_path"] = fpath + filen = f'{i}_{j}.png' + local_image_file_path = os.path.join(DIR_AUTOIMGS, filen) + o["file_path"] = local_image_file_path # - save to file - with open(fpath, 'wb') as b64imgfile: + with open(os.path.join(target_images_dir, filen), 'wb') as b64imgfile: # real path b64imgfile.write(b64img) # - add description for link if "text/plain" in output["data"]: o["data_descr"] = output["data"]["text/plain"] # - change header for image if "graphics" not in header: # add only first image to header - header = f"#+begin_src python :results file graphics :file {fpath} :exports both :session s1" + header = f"#+begin_src python :results file graphics :file {local_image_file_path} :exports both :session s1" outputs.append(o) # -- print source @@ -105,7 +105,7 @@ def jupyter2org(f:TextIOWrapper, source_file_jupyter: str): PRINT() -def parse(): +def parse_arguments(): parser = argparse.ArgumentParser( description="Convert a Jupyter notebook to Org file (Emacs) and vice versa", usage="j2o myfile.py") @@ -120,27 +120,30 @@ def parse(): return parser.parse_args() -def main(source_file_jupyter: str, target_file_org: str = None, overwrite: bool = False): - """""" - print(source_file_jupyter, target_file_org, overwrite) +def j2p_main(source_file_jupyter: str, target_file_org: str = None, overwrite: bool = False): + # print(source_file_jupyter, target_file_org, overwrite) + s_path = os.path.dirname(target_file_org) + target_images_dir = os.path.normpath(os.path.join(s_path, DIR_AUTOIMGS)) # - create directory for images: - if not os.path.exists(DIR_TARGET): - os.makedirs(DIR_TARGET) + if not os.path.exists(target_images_dir): + os.makedirs(target_images_dir) # - create target_file_org if target_file_org is None: target_file_org = os.path.splitext(source_file_jupyter)[0] + '.org' - # - overwrite? if not overwrite: if os.path.isfile(target_file_org): logging.critical("File already exist.") return - # - create target file and start conversion with open(target_file_org, "w") as f: - jupyter2org(f, source_file_jupyter) + jupyter2org(f, source_file_jupyter, target_images_dir) + + +def main(): + args = parse_arguments() + j2p_main(args.jupfile, args.orgfile, args.overwrite) if __name__=="__main__": - args = parse() - main(args.jupfile, args.orgfile, args.overwrite) + main() diff --git a/pyproject.toml b/pyproject.toml index 80b3021..f48e241 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,9 +21,7 @@ dependencies = [ ] [project.optional-dependencies] -test = [ - "pytest" -] +test = [ "pytest" ] [project.urls] "Homepage" = "https://github.com/Anoncheg1/j2o" @@ -32,5 +30,5 @@ test = [ [project.scripts] mfs = "micro_file_server.__main__:main" -# [pytest] -# testpaths = "test" \ No newline at end of file +[tool.pytest.ini_options] +pythonpath = [ "." ] diff --git a/tests/test_main.py b/tests/test_main.py index 81ebeb5..2c8f35c 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,15 +1,38 @@ from filecmp import cmp +import os + # -from j2o.__main__ import jupyter2org +from j2o.__main__ import jupyter2org, DIR_AUTOIMGS jupyter_4_2 = "./tests/draw-samples.ipynb" jupyter_4_2_saved = "./tests/draw-samples.org" +# jupyter_4_2_images = ['10_0.png', '12_0.png', '14_0.png', '8_0.png'] +jupyter_4_2_images_sizes = {'8_0.png':71156, + '10_0.png':58359, + '12_0.png':196616, + '14_0.png':272601 + } def test_converter(): target_file_org = '/tmp/a.org' + s_path = os.path.dirname(target_file_org) + target_images_dir = os.path.normpath(os.path.join(s_path, DIR_AUTOIMGS)) + # - create directory for images: + if not os.path.exists(target_images_dir): + os.makedirs(target_images_dir) with open(target_file_org, "w") as f: - jupyter2org(f, jupyter_4_2) + jupyter2org(f, + source_file_jupyter=jupyter_4_2, + target_images_dir=target_images_dir) + # - compare output file with our saved one assert cmp(target_file_org, jupyter_4_2_saved, shallow=False) + # - check output files names and sizes in autoimgs directory + onlyfiles = [f for f in os.listdir(target_images_dir) if os.path.isfile(os.path.join(target_images_dir, f))] + assert len(onlyfiles) == len(jupyter_4_2_images_sizes.keys()) + assert all([x in onlyfiles for x in jupyter_4_2_images_sizes.keys()]) + for x in onlyfiles: + assert jupyter_4_2_images_sizes[x] == os.stat(os.path.join(target_images_dir,x)).st_size + print("success") if __name__=="__main__":