Skip to content

Instantly share code, notes, and snippets.

@Omustardo
Created May 10, 2021 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Omustardo/2f62a321c8e9499e303caa93a6fe1c1d to your computer and use it in GitHub Desktop.
Save Omustardo/2f62a321c8e9499e303caa93a6fe1c1d to your computer and use it in GitHub Desktop.
Improved formatting for WikiTree GEDCOMpare output
# Instructions:
# 1. Import a gedcom file into wikitree.com
# 2. When adding new profile from the gedcom, copy the intended output into a file called 'input.txt' that is in the same directory as this script.
# 3. Run this script. It will continuously watch for changes to the 'input.txt' file and format the contents.
#
import os
import sys
import time
from datetime import datetime
INPUT_FILE = 'input.txt'
OUTPUT_FILE = 'input.txt' # output to the same file
def readfile():
with open(INPUT_FILE, "r") as f:
content = f.read().splitlines()
f.close()
# add a trailing newline. All text files should end
# with a newline on its own line, but it's not clear that the input here honors that.
content.append("\n")
return content
# Adds a newline before any lines starting with '''
# These indicate section headers, like:
# '''Residences'''
# or
# '''Born'''
def prepend_section_newlines(lines):
for i in range(len(lines)):
if lines[i].startswith("'''"):
lines[i] = "\n" + lines[i]
return lines
# Formats the "Files" listing from something like:
# File: {{Ancestry Tree Media|1234|abc-def-ghi}}
# Format: jpg.
# name1, name2, name3.
# to:
# * File: {{Ancestry Tree Media|1234|abc-def-ghi}} Format: jpg. name1, name2, name3.
#
# This makes all of the content formatted nicely with bullet points.
def format_files_listing(lines):
output = []
i = 0
while i < len(lines): # use while since we manipulate value of i
if lines[i].startswith("File: {{Ancestry"):
if i + 2 >= len(lines):
break
output.append("* " + lines[i] + " " + lines[i+1] + " " + lines[i+2])
i += 2
else:
output.append(lines[i])
i += 1
return output
def writefile(content):
with open(OUTPUT_FILE, "w") as f:
f.write(content)
f.close()
def wait_for_file_modification():
lastModifiedSeconds = os.path.getmtime(INPUT_FILE)
while lastModifiedSeconds == os.path.getmtime(INPUT_FILE):
time.sleep(1)
def main():
# Force stdout to flush the buffer so messages appear more quickly.
# Without this, sometimes a `print` call will not actually show the output immediately.
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
# Infinitely loop, waiting for modifications to the file and formatting the file content whenever there's a change.
while True:
print('Formatting ' + INPUT_FILE + ' ...')
lines = readfile()
lines = prepend_section_newlines(lines)
lines = format_files_listing(lines)
# print(lines) # DEBUG
print('Writing formatted content to ' + INPUT_FILE)
writefile("\n".join(lines))
print('Waiting for modification of ' + INPUT_FILE)
wait_for_file_modification()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment