lib/keywords.py
author fabien@tzone.org
Sun, 28 Dec 2008 12:41:44 -0500
changeset 60 dad9733c99f0
parent 51 7015d65beb3d
child 71 591ffdca8041
permissions -rw-r--r--
Convert the prefs editor to the action library.

def get_keywords(form, name):
	"""extract keywords associate with field name 'name'."""
	kws = form.getvalue(name)
	if not isinstance(kws, type([])):
		if kws:
			kws = [kws]
		else:
			kws = []
	return map(int, kws)

def set_selection(db, bk, sel, exc = []):
	"""select keywords selected in the bookmarks list."""
	if len(bk) > 0:
		ids, names = map(list,apply(zip,db.get_keywords(bk)))
		for key in exc:
			if key not in ids:
				ids.append(key)
				names.append(db.get_keyword(key))
		allkw = map(lambda x,y: [x, y], ids, names)
	else:
		allkw = db.get_all_keywords()
	(kw, cnt) = map(list,apply(zip, db.get_keywords_count(bk)))
	res = []
	for key in allkw:
		is_selected = key[0] in sel
		is_excluded = key[0] in exc
		is_unselected = not (is_selected or is_excluded)
		if key[0] in kw:
			kcnt = cnt[kw.index(key[0])]
		else:
			kcnt = 0
		res.append({'id':key[0],
			'keyword': key[1],
			'count' : kcnt,
			'selected' : is_selected,
			'excluded' : is_excluded,
			'unselected' : is_unselected})
	return res

def sort_keywords(kw, pref):
	global _sk_rev_fact
	global _sk_sort
	if pref['keywords_reverse']:
		_sk_rev_fact = -1
	else:
		_sk_rev_fact = 1
	_sk_sort = pref['keywords_sort'] 
	kw.sort(lambda l, r: _sk_rev_fact*cmp(l[_sk_sort], r[_sk_sort]))
	return kw