Skip to content

Instantly share code, notes, and snippets.

@escalonn
Created August 17, 2015 21:43
Show Gist options
  • Save escalonn/7774c1ce9779f1bfcd0d to your computer and use it in GitHub Desktop.
Save escalonn/7774c1ce9779f1bfcd0d to your computer and use it in GitHub Desktop.
def update_tree(v):
for n2, v2 in v:
if isinstance(n2, ck2parser.String):
if ck2parser.is_codename(n2.val):
for n3, v3 in v2:
if n3.val == 'capital':
prov_key = 'PROV{}'.format(v3.val)
capital_name = localisation[prov_key]
if not v3.post_comment:
v3.post_comment = ck2parser.Comment(
capital_name)
break
v2.ker.post_comment = None
_, (nl, _) = v2.inline_str(0)
if nl >= 36:
comment = 'end ' + n2.val
prepend_post_comment(v2.ker, comment)
# if re.match(r'[ekd]_', n2.val):
# try:
# prepend_post_comment(v2.kel, localisation[n2.val])
# except KeyError:
# print('@@@ ' + n2.val)
baronies_to_remove = []
if n2.val.startswith('c_'):
# if v2.kel.post_comment:
# print('c ' + v2.kel.post_comment.val)
if (v2.kel.post_comment and
v2.kel.post_comment.val.isdigit()):
v2.kel.post_comment = None
try:
prov_id = province_id[n2.val]
comment = '{} ({})'.format(
localisation['PROV{}'.format(prov_id)],
prov_id)
prepend_post_comment(v2.kel, comment)
except KeyError:
print('!!! ' + n2.val)
num_baronies = 0
for child in v2.contents:
if child.key.val.startswith('b_'):
if (child.key.val in historical_baronies or
child.key.val in used_baronies[n2.val]):
num_baronies += 1
else:
baronies_to_remove.append(child)
if (num_baronies + len(baronies_to_remove) <
max_settlements[n2.val]):
print(('{} has {} subholdings '
'but {} max_settlements!').format(
num_baronies + len(baronies_to_remove),
max_settlements[n2.val]))
keep = max(0, max_settlements[n2.val] - num_baronies)
del baronies_to_remove[:keep]
v2.contents[:] = [v for v in v2.contents
if v not in baronies_to_remove]
allow_block = None
for child in v2.contents:
if child.key.val == 'allow':
allow_block = child
break
if allow_block:
if v2.contents[-1] != allow_block:
v2.contents.remove(allow_block)
v2.contents.append(allow_block)
post_barony_block = allow_block
else:
post_barony_block = v2.ker
for barony in reversed(baronies_to_remove):
b_is, _ = barony.inline_str(0)
comments = [ck2parser.Comment(s)
for s in b_is.split('\n')]
post_barony_block.pre_comments[0:0] = comments
n2_lower = n2.val.lower()
if any(n2_lower == s
for s in ['not', 'or', 'and', 'nand', 'nor']):
n2.val = n2_lower
if isinstance(v2, ck2parser.Obj) and v2.has_pairs:
update_tree(v2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment