Skip to content

Commit

Permalink
tweaked
Browse files Browse the repository at this point in the history
  • Loading branch information
none committed Nov 8, 2023
1 parent 7573655 commit da10d94
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
39 changes: 21 additions & 18 deletions j2o/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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()
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ dependencies = [
]

[project.optional-dependencies]
test = [
"pytest"
]
test = [ "pytest" ]

[project.urls]
"Homepage" = "https://github.com/Anoncheg1/j2o"
Expand All @@ -32,5 +30,5 @@ test = [
[project.scripts]
mfs = "micro_file_server.__main__:main"

# [pytest]
# testpaths = "test"
[tool.pytest.ini_options]
pythonpath = [ "." ]
27 changes: 25 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down

0 comments on commit da10d94

Please sign in to comment.