Move in-subtree archive to separate archive file
Part of
import org_rw
import os
def recursively_raise_depth_to(hl: org_rw.Headline, new_depth: int):
assert hl.depth >= new_depth
depth_change = hl.depth - new_depth
recursively_raise_depth(hl, depth_change)
def recursively_raise_depth(hl, depth_change):
for child in hl.children:
recursively_raise_depth(child, depth_change)
hl.depth -= depth_change
def move_in_subtree_archive_to_separate_archive_file(from_doc: org_rw.OrgDoc, to_doc: org_rw.OrgDoc):
# Get all archives
archives = []
for hl in from_doc.getAllHeadlines():
if hl.title.get_text().strip() == 'Archive' and 'ARCHIVE' in hl.tags:
archives.append(hl)
print("Found {} archives".format(len(archives)))
change_count = 0
for archive in archives:
if archive.parent.id is None:
print("Skipping archive as parent ({}; title={}) has no ID".format(archive.parent, archive.parent.title if isinstance(archive.parent, org_rw.Headline) else '(doc)'))
continue
parid = archive.parent.id
while len(archive.children) > 0:
change_count += 1
hl = archive.children.pop(0)
hl.set_property('ARCHIVE_PARENT_ID', parid)
recursively_raise_depth_to(hl, 1)
to_doc.headlines.append(hl)
print("{} changes applied".format(change_count))
with open(os.path.expanduser('~/.logs/logs.org.txt_archive'), 'rt') as farc:
with open(os.path.expanduser('~/.logs/logs.org.txt'), 'rt') as f:
from_doc = org_rw.load(f)
to_doc = org_rw.load(farc)
move_in_subtree_archive_to_separate_archive_file(from_doc, to_doc)
# Save archives first
with open(os.path.expanduser('~/.logs/logs.org.txt_archive'), 'wt') as farc:
org_rw.dump(to_doc, farc)
with open(os.path.expanduser('~/.logs/logs.org.txt'), 'wt') as f:
org_rw.dump(from_doc, f)