Skip to content
This repository has been archived by the owner on Feb 1, 2020. It is now read-only.

Commit

Permalink
Starting to break in the merging tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Tagliamonte committed Feb 2, 2012
1 parent bc92da4 commit 4ee914c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
James Turk <jturk@sunlightfoundation.com>
Michael Stephens <mstephens@sunlightfoundation.com>
Thom Neale <tneale@sunlightfoundation.com>
Paul Tagliamonte <paultag@sunlightfoundation.com>

Spun out of openstates project on 9/23/2011, authors with commits affecting the preserved subtrees reflected here. For full history of project see http://github.com/sunlightlabs/openstates/
Spun out of openstates project on 9/23/2011, authors with commits affecting the
preserved subtrees reflected here. For full history of project see
http://github.com/sunlightlabs/openstates/
5 changes: 5 additions & 0 deletions billy/tests/leg_merge_test_data/leg_id_sanity/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"_id" : "LOL100",
"leg_id" : "LOL100",
"_all_ids" : [ "LOL100" ]
}
7 changes: 7 additions & 0 deletions billy/tests/leg_merge_test_data/leg_id_sanity/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"_all_ids": [
"LOL200"
],
"_id": "LOL200",
"leg_id": "LOL200"
}
7 changes: 7 additions & 0 deletions billy/tests/leg_merge_test_data/leg_id_sanity/merged.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"_all_ids": [
"LOL100", "LOL200"
],
"_id": "LOL100",
"leg_id": "LOL100"
}
35 changes: 35 additions & 0 deletions billy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,38 @@ def configure_logging(verbosity_count=0, module=None):
else:
format = "%(asctime)s %(name)s %(levelname)s %(message)s"
logging.basicConfig(level=verbosity, format=format, datefmt="%H:%M:%S")

def merge_legislators(leg1, leg2):
assert leg1['_id'] != leg2['_id']
if leg1['_id'] > leg2['_id']:
leg1, leg2 = leg2, leg1

no_compare = set(('_id', 'leg_id', '_all_ids', '_locked_fields',
'created_at', 'updated_at', 'roles', 'old_roles' ))

leg1['_all_ids'] += leg2['_all_ids']

# get set of keys that appear in both legislators and are not blacklisted
leg1keys = set(leg1.keys())
leg2keys = set(leg2.keys())
compare_keys = (leg1keys & leg2keys) - no_compare
leg1_locked = leg1.get('_locked_fields', [])
leg2_locked = leg2.get('_locked_fields', [])

# if a key is in both, copy 2 to 1 if they differ and key is locked in 2
# or unlocked in 1
for key in compare_keys:
if (leg1[key] != leg2[key] and leg2[key] and (key in leg2_locked or
key not in leg1_locked)):
leg1[key] = leg2[key]

# locked is now union of the two
leg1['_locked_fields'] = list(set(leg1_locked + leg2_locked))

# copy new keys over to old legislator
for key in leg2keys - leg1keys:
leg1[key] = leg2[key]

# XXX: Set updated_at

return leg1

0 comments on commit 4ee914c

Please sign in to comment.