Skip to content

Instantly share code, notes, and snippets.

@seanjensengrey
Forked from kwharrigan/cmanage.py
Created June 22, 2010 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanjensengrey/447986 to your computer and use it in GitHub Desktop.
Save seanjensengrey/447986 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import optparse
import simplejson as json
import time
LONGNAME = "%s@en-US"
def load_file(filename):
'''
Load a json file and return the resulting object
'''
return json.loads(open(filename, 'r').read())
def write_file(coll, filename):
open(filename, 'w').write(json.dumps(coll))
def add(coll, item):
'''
Add an item to collection
'''
print "Adding item %s" % item
time_ms = int(time.time()*1000)
new_item = {}
name = LONGNAME % item
if not ((name) in coll):
coll[name] = {'items':[],'lastAccess':time_ms}
def delete(coll, item):
name = LONGNAME % item
if name in coll:
del coll[name]
else:
print 'item %s not found' % item
def main():
# Generate options and parse arguments
list = False
usage = "usage: %prog [options] filename"
p = optparse.OptionParser(usage=usage)
p.add_option('--add', '-a', help="add an item")
p.add_option('--delete', '-d', help="delete an item")
p.add_option('-l', action="store_true", dest="list", help="list item names")
options, args = p.parse_args()
# Must have only 1 argument, the filename
if len(args) != 1:
p.print_help()
return
# Load the json file
coll = load_file(args[0])
# Process options
if options.add:
add(coll, options.add)
if options.delete:
delete(coll, options.delete)
if options.list:
for key in coll.keys():
print str(key.split('@')[0])
# Write back to file
write_file(coll, args[0])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment