Skip to content

Instantly share code, notes, and snippets.

@escalonn
Created August 17, 2015 22:02
Show Gist options
  • Save escalonn/eccd69a8da3681329682 to your computer and use it in GitHub Desktop.
Save escalonn/eccd69a8da3681329682 to your computer and use it in GitHub Desktop.
def process_province_history():
def mark_barony(barony, county_set):
try:
if barony.val.startswith('b_'):
county_set.add(barony.val)
except AttributeError:
pass
tree = ck2parser.parse_file(swmhpath / 'map/default.map')
defs = next(v.val for n, v in tree if n.val == 'definitions')
id_name = {}
with (swmhpath / 'map' / defs).open(newline='',
encoding='cp1252') as csvfile:
for row in csv.reader(csvfile, dialect='ckii'):
try:
id_name[int(row[0])] = row[4]
except (IndexError, ValueError):
continue
province_id = {}
used_baronies = collections.defaultdict(set)
max_settlements = {}
for path in ck2parser.files('history/provinces/*.txt', swmhpath):
try:
number, name = path.stem.split(' - ')
id_number = int(number)
if id_name[id_number] == name:
tree = ck2parser.parse_file(path)
try:
title = next(v.val for n, v in tree if n.val == 'title')
except StopIteration:
continue
for n, v in tree:
mark_barony(n, used_baronies[title])
mark_barony(v, used_baronies[title])
if isinstance(v, ck2parser.Obj):
if v.has_pairs:
for n2, v2 in v:
mark_barony(n2, used_baronies[title])
mark_barony(v2, used_baronies[title])
else:
for v2 in v:
mark_barony(v2, used_baronies[title])
if n.val == 'max_settlements':
max_settlements[title] = int(v.val)
province_id[title] = id_number
except:
print(path)
raise
return province_id, used_baronies, max_settlements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment