Skip to content

Commit

Permalink
fine grained control of paths
Browse files Browse the repository at this point in the history
  • Loading branch information
none committed Jul 25, 2024
1 parent ca553e0 commit d6a8a62
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
22 changes: 13 additions & 9 deletions j2o/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,23 @@ def j2p_main(source_file_jupyter: str, target_file_org = str | None,
overwrite: bool = False):
"Prepare target file and directory for conversion."
# print(source_file_jupyter, target_file_org, overwrite)
source_file_name = os.path.splitext(source_file_jupyter)[0]
image_dir = source_file_name[:3] + '-' + source_file_name[-3:] + '-imgs'

if target_file_org:
s_path = os.path.dirname(target_file_org)
target_images_dir = os.path.normpath(os.path.join(s_path, image_dir))
else:
target_images_dir = "./" + image_dir
t_path, file_name = os.path.split(target_file_org) # "/var/va.org" - > ('/var', 'va.org')
else: # use source file
t_path, file_name = os.path.split(source_file_jupyter) # "/var/va.org" - > ('/var', 'va.org')

file_name_short = os.path.splitext(file_name)[0] # # "va.org" -> ('va', '.org')
image_dir = file_name_short[:3] + '-' + file_name_short[-3:] + '-imgs'
target_images_dir = os.path.normpath(os.path.join(t_path, image_dir))

if target_file_org is None:
target_file_org = os.path.join(t_path, file_name_short) + '.org'

# - create directory for images:
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 = source_file_name + '.org'

# - overwrite?
if not overwrite:
if os.path.isfile(target_file_org):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "hatchling.build"
[project]
name = "j2o"
readme = "README.md"
version = "0.0.5"
version = "0.0.6"
authors = [
{ name="Anoncheg1" }
]
Expand Down
16 changes: 8 additions & 8 deletions tests/draw-samples.org
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,29 @@ reflectivity[reflectivity == -1e6] = -1
#+end_src

** Погодные события
#+begin_src python :results file graphics :file ./autoimgs/8_0.png :exports both :session s1
#+begin_src python :results file graphics :file /tmp/a-a-imgs/8_0.png :exports both :session s1
_, axs = plt.subplots(1, len(events), figsize=(20, 2))
for index in range(len(events)):
axs[index].imshow(events[index])
axs[index].set_title(timestamps[index])
#+end_src

#+RESULTS:
[[file:./autoimgs/8_0.png]] <Figure size 1440x144 with 6 Axes>
[[file:/tmp/a-a-imgs/8_0.png]] <Figure size 1440x144 with 6 Axes>

** Интенсивность осадков
#+begin_src python :results file graphics :file ./autoimgs/10_0.png :exports both :session s1
#+begin_src python :results file graphics :file /tmp/a-a-imgs/10_0.png :exports both :session s1
_, axs = plt.subplots(1, len(intensity), figsize=(20, 2))
for index in range(len(intensity)):
axs[index].imshow(intensity[index])
axs[index].set_title(timestamps[index])
#+end_src

#+RESULTS:
[[file:./autoimgs/10_0.png]] <Figure size 1440x144 with 6 Axes>
[[file:/tmp/a-a-imgs/10_0.png]] <Figure size 1440x144 with 6 Axes>

** Радиальная скорость по высотам
#+begin_src python :results file graphics :file ./autoimgs/12_0.png :exports both :session s1
#+begin_src python :results file graphics :file /tmp/a-a-imgs/12_0.png :exports both :session s1
_, axs = plt.subplots(10, len(radial_velocity), figsize=(20, 20))
for index in range(len(radial_velocity)):
for row in range(10):
Expand All @@ -102,10 +102,10 @@ for index in range(len(radial_velocity)):
#+end_src

#+RESULTS:
[[file:./autoimgs/12_0.png]] <Figure size 1440x1440 with 60 Axes>
[[file:/tmp/a-a-imgs/12_0.png]] <Figure size 1440x1440 with 60 Axes>

** Отражаемость по высотам
#+begin_src python :results file graphics :file ./autoimgs/14_0.png :exports both :session s1
#+begin_src python :results file graphics :file /tmp/a-a-imgs/14_0.png :exports both :session s1
_, axs = plt.subplots(10, len(reflectivity), figsize=(20, 20))
for index in range(len(reflectivity)):
for row in range(10):
Expand All @@ -116,5 +116,5 @@ for index in range(len(reflectivity)):
#+end_src

#+RESULTS:
[[file:./autoimgs/14_0.png]] <Figure size 1440x1440 with 60 Axes>
[[file:/tmp/a-a-imgs/14_0.png]] <Figure size 1440x1440 with 60 Axes>

20 changes: 9 additions & 11 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

#
from j2o.__main__ import jupyter2org, DIR_AUTOIMGS, markdown_to_org
from j2o.__main__ import jupyter2org, markdown_to_org

jupyter_4_2 = "./tests/draw-samples.ipynb"
jupyter_4_2_saved = "./tests/draw-samples.org"
Expand All @@ -15,24 +15,24 @@

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))
t_path, file_name = os.path.split(target_file_org) # "/var/va.org" - > ('/var', 'va.org')
file_name_short = os.path.splitext(file_name)[0] # # "va.org" -> ('va', '.org')
image_dir = '/tmp/' + file_name_short[:3] + '-' + file_name_short[-3:] + '-imgs'
# - create directory for images:
if not os.path.exists(target_images_dir):
os.makedirs(target_images_dir)
if not os.path.exists(image_dir):
os.makedirs(image_dir)
with open(target_file_org, "w") as f:
jupyter2org(f,
source_file_jupyter=jupyter_4_2,
target_images_dir=target_images_dir)
target_images_dir=image_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))]
onlyfiles = [f for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_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")
assert jupyter_4_2_images_sizes[x] == os.stat(os.path.join(image_dir,x)).st_size


def test_markdown():
Expand All @@ -56,10 +56,8 @@ def test_markdown():
'print("Hello, world!")',
'#+end_src',
'Even more text']
print(markdown_to_org(markdown_lines))
for m, o in zip(markdown_to_org(markdown_lines), org_lines):
assert (m == o)
# print(org_lines)


if __name__ == "__main__":
Expand Down

0 comments on commit d6a8a62

Please sign in to comment.