|I often hassle Renedox about excessive animated|
gifs. But sod it. This seems appropriate.
It's at times like this that I find myself REALLY missing my office. What makes right this very moment (actually... I'm writing this a few days before it gets published....) quite different? It's the fact that I've finally figured out a piece of code that's taken me a hellishly long time to figure out.
And in an office, in the weekend or in the evening when no one's around, I would do the dance of the victorious programmer. I would swing around in an office chair and wave my arms about, pretend to be Mick Jagger (except a Mick Jagger who can't move), look upon the code in pride (this time around it's less than a page long and has taken me almost a week to figure it out).
This isn't the pride of seeing a piece of work that you've done or been a part of being brought to fruition. Speaking of which, check out this link. Unfortunately I had a falling out with one of the people involved in the project and so am not kept up to date with it or credited with any of it's conception. It's this whole other big buzz, have a celebration drink, take 5 minutes out to be proud, then carry on life... it's short lived. Hell... there's a chance I'll come back months after writing this, see the code and think "What was I on?".
Anyway, a bit of code for that chrome management thingee. Oh - and this code will eventually end up on github... I'm just not sure what to call it. I should probably explain what it does. It takes in a Preferences file, as used by Chrome and Chromium, and another file, one that contains settings, in the same format as Preferences (json) and outputs a merged version of it.
#!/usr/bin/env python import json def get_index( original, value ): # This has been written for a very specific use case. # I imagine it would need to be extended or values # given to the function to specify search keys. # It'll do for now... if 'name' in value: for (counter, item ) in enumerate( original ): if 'name' in item: if 'path' in item and 'path' in value: if item[ 'name' ] == value[ 'name' ] and \ item[ 'path' ] == value[ 'path' ]: return counter else: if item[ 'name' ] == value[ 'name' ]: return counter return -1 def merge( original, changes ): if isinstance( changes, ( dict ) ): for key, value in changes.iteritems(): if key not in original: original[ key ] = value elif isinstance( value, (dict) ): original=dict( original.items() + merge( original [ key ], value ).items()) elif isinstance( value, (list) ): for item in value: changed=merge( original[key], value) else: original[ key ]=value elif isinstance( changes, ( list ) ): for ( counter, value ) in enumerate( changes ): if isinstance( value, (dict) ): index = get_index(original,value) if index == -1: index = len( original ) for key, value2 in value.iteritems(): if key=="name": pass else: original[ index ][ key ]=value2 elif isinstance( value, (list) ): index = get_index( original, value ) if index == -1: index = len( original ) original[ index ] = merge( original[ index ], value ) else: if value not in original: original.append( value ) return original # code for testing.... # # Preferences would be a Preferences file used by # Chrome and Chromium to store settings. f=open( 'Full-Preferences', 'r' ) preferences = json.loads( f.read() ) f.close() # The changes.json file contains the settings you # intend to change. f=open( 'changes.json', 'r' ) merge_file = json.loads( f.read() ) f.close() print json.dumps(merge( preferences, merge_file ), indent=3)