Skip to content

Instantly share code, notes, and snippets.

@Seanny123
Last active January 15, 2023 20:17
Show Gist options
  • Save Seanny123/eec6b5916ecc7675b6cb35eb0838bbe3 to your computer and use it in GitHub Desktop.
Save Seanny123/eec6b5916ecc7675b6cb35eb0838bbe3 to your computer and use it in GitHub Desktop.
Go through a bunch of exported MindMup .mup files looking for text
"""
Iterates through a directory of exported MindMup .mup files and prints the title of each file that matches the given
regular expression, as well as the matched node contents.
MindMup is a mind mapping tool which represents the Directed Acyclic Graph (DAG) of a mind map as a nested dictionary encoded as JSON.
"""
import argparse
import json
from pathlib import Path
import re
from typing import Generator
def nested_dict_iter(nested: dict) -> Generator[tuple[str, str], None, None]:
"""
Iterates through a nested dictionary yielding any top-level (no children/nesting) keys and values.
"""
for key, value in nested.items():
if isinstance(value, dict):
for inner_key, inner_value in nested_dict_iter(value):
yield inner_key, inner_value
else:
yield key, value
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("directory", help="the directory to search")
parser.add_argument("regex", help="the regular expression to use")
args = parser.parse_args()
exp = re.compile(args.regex)
search_dir = Path(args.directory)
assert search_dir.is_dir()
# Could instead read from stdin to enable piping from other commands
for mup in search_dir.glob("*.mup"):
with mup.open("r") as fi:
jmup = json.load(fi)
file_printed = False
for key, val in nested_dict_iter(jmup):
# node text contents are stored in the "title" key
if key == "title" and exp.search(val) is not None:
if not file_printed:
file_printed = True
print()
print(mup)
print(val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment