# Bryan Schumaker (5 / 28 / 2011)

import threading
from libsaria import storage
dattrs_lock   = threading.Lock()
lock_dattrs   = dattrs_lock.acquire
unlock_dattrs = dattrs_lock.release

# dynamic attributes
dattrs = dict()

def load_dattrs():
	global dattrs
	dattrs_dict = storage.load_obj("dattrs", dict)
	lock_dattrs()
	dattrs = dattrs_dict
	unlock_dattrs()

def save_dattrs():
	storage.save_obj("dattrs", dattrs)

def get_dattr(id, attribute):
	lock_dattrs()
	attrs = dattrs.get(id, None)
	if attrs == None:
		unlock_dattrs()
		return None
	attr = attrs.get(attribute, None)
	unlock_dattrs()
	return attr

def set_dattr(id, attribute, value):
	lock_dattrs()
	attrs = dattrs.setdefault(id, dict())
	attrs[attribute] = value
	save_dattrs()
	unlock_dattrs()

def get_like(id):
	return get_dattr(id, "like")

def get_count(id):
	count = get_dattr(id, "count")
	if count != None:
		return count
	return 0

def inc_count(id):
	count = get_count(id)
	set_dattr(id, "count", count + 1)
