From 19d6678eb1b881eef1b7310d7227ccd61db3e071 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 16 Aug 2021 15:59:13 -0700 Subject: [PATCH] Allow importing org-to-jsonl as module for reuse To allow importing org-to-jsonl as module - Wrap code in __main__ into a org-to-jsonl method - Rename processor/org-mode to processor/org_mode - Add __init__.py to processor directory --- processor/{org-mode => }/__init__.py | 0 processor/org_mode/__init__.py | 0 .../org_to_jsonl.py} | 36 +++++++++++-------- processor/{org-mode => org_mode}/orgnode.py | 0 4 files changed, 21 insertions(+), 15 deletions(-) rename processor/{org-mode => }/__init__.py (100%) create mode 100644 processor/org_mode/__init__.py rename processor/{org-mode/org-to-jsonl.py => org_mode/org_to_jsonl.py} (94%) rename processor/{org-mode => org_mode}/orgnode.py (100%) diff --git a/processor/org-mode/__init__.py b/processor/__init__.py similarity index 100% rename from processor/org-mode/__init__.py rename to processor/__init__.py diff --git a/processor/org_mode/__init__.py b/processor/org_mode/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/processor/org-mode/org-to-jsonl.py b/processor/org_mode/org_to_jsonl.py similarity index 94% rename from processor/org-mode/org-to-jsonl.py rename to processor/org_mode/org_to_jsonl.py index 699eb08c..016435d3 100644 --- a/processor/org-mode/org-to-jsonl.py +++ b/processor/org_mode/org_to_jsonl.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # Import Modules -import orgnode +import processor.org_mode.orgnode import json import argparse import pathlib @@ -10,6 +10,24 @@ import gzip # Define Functions +def org_to_jsonl(org_files, org_file_filter, output_path, verbose=False): + # Get Org Files to Process + org_files = get_org_files(args.input_files, args.input_filter) + + # Extract Entries from specified Org files + entries = extract_org_entries(org_files) + + # Process Each Entry from All Notes Files + jsonl_data = convert_org_entries_to_jsonl(entries, verbose=args.verbose) + + # Compress JSONL formatted Data + if args.output_file.suffix == ".gz": + compress_jsonl_data(jsonl_data, args.output_file, verbose=args.verbose) + elif args.output_file.suffix == ".jsonl": + dump_jsonl(jsonl_data, args.output_file, verbose=args.verbose) + + return entries + def dump_jsonl(jsonl_data, output_path, verbose=0): "Write List of JSON objects to JSON line file" with open(get_absolute_path(output_path), 'w', encoding='utf-8') as f: @@ -123,17 +141,5 @@ if __name__ == '__main__': print("At least one of org-files or org-file-filter is required to be specified") exit(1) - # Get Org Files to Process - org_files = get_org_files(args.input_files, args.input_filter) - - # Extract Entries from specified Org files - entries = extract_org_entries(org_files) - - # Process Each Entry from All Notes Files - jsonl_data = convert_org_entries_to_jsonl(entries, verbose=args.verbose) - - # Compress JSONL formatted Data - if args.output_file.suffix == ".gz": - compress_jsonl_data(jsonl_data, args.output_file, verbose=args.verbose) - elif args.output_file.suffix == ".jsonl": - dump_jsonl(jsonl_data, args.output_file, verbose=args.verbose) + # Map notes in Org-Mode files to (compressed) JSONL formatted file + org_to_jsonl(args.input_files, args.input_filter, args.output_file, args.verbose) diff --git a/processor/org-mode/orgnode.py b/processor/org_mode/orgnode.py similarity index 100% rename from processor/org-mode/orgnode.py rename to processor/org_mode/orgnode.py