Skip to content

Instantly share code, notes, and snippets.

@kueller
Created January 1, 2018 10:52
Show Gist options
  • Save kueller/84067ecc4f5a16bb8ce37ce615695ac8 to your computer and use it in GitHub Desktop.
Save kueller/84067ecc4f5a16bb8ce37ce615695ac8 to your computer and use it in GitHub Desktop.
METADATA_START = 0xd000
# Extracts the DTA as raw text from the CONfile specified in "filepath"
# Like everything else in this script, it's probably a terrible way of
# doing it but it functions well enough.
def read_metadata(filepath):
with open(filepath, "rb") as f:
f.seek(METADATA_START)
metadata = []
byte = f.read(1)
while byte != '\x00':
metadata.append(byte)
byte = f.read(1)
return ''.join(metadata)
# This will parse in the DTA file as a single text string.
# raw_string is the text of the DTA file.
# tree at first should be an empty list that you can access later
# i is the tree level (start with 0)
# Ex: parse_metadata(dta_text, tree, 0)
def parse_metadata(raw_text, tree, i):
s = '' # Temp value to get string to add to tree.
read_active = False
# Shift to first opening paren
while raw_text[i] != '(':
i = i + 1
i = i + 1
# Do this until the matching close paren is found
while raw_text[i] != ')':
# Elements can have single or double quotation marks
if raw_text[i] == '\'':
i = i + 1
while raw_text[i] != '\'':
s = s + raw_text[i]
i = i + 1
tree.append(s)
s = ''
elif raw_text[i] == '\"':
i = i + 1
while raw_text[i] != '\"':
s = s + raw_text[i]
i = i + 1
tree.append(s)
s = ''
# C3 Magma exclusive extra metadata
elif raw_text[i] == ';':
i = i + 1
while raw_text[i] in ('\n', '\n'):
s = s + raw_text[i]
i = i + 1
tree.append(s)
s = ''
# Internal subsection, start parsing recursively
elif raw_text[i] == '(':
subtree = []
i = parse_metadata(raw_text, subtree, i)
tree.append(subtree)
# Grabs the extra shit
elif not raw_text[i].isspace() and not read_active:
s = s + raw_text[i]
read_active = True
elif not raw_text[i].isspace() and read_active:
s = s + raw_text[i]
elif raw_text[i].isspace and read_active:
tree.append(s)
s = ''
read_active = False
i = i + 1
if read_active:
tree.append(s)
return i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment