facet.py revision 1505
1505N/A#!/usr/bin/python
1505N/A#
1505N/A# CDDL HEADER START
1505N/A#
1505N/A# The contents of this file are subject to the terms of the
1505N/A# Common Development and Distribution License (the "License").
1505N/A# You may not use this file except in compliance with the License.
1505N/A#
1505N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1505N/A# or http://www.opensolaris.org/os/licensing.
1505N/A# See the License for the specific language governing permissions
1505N/A# and limitations under the License.
1505N/A#
1505N/A# When distributing Covered Code, include this CDDL HEADER in each
1505N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1505N/A# If applicable, add the following below this CDDL HEADER, with the
1505N/A# fields enclosed by brackets "[]" replaced with your own identifying
1505N/A# information: Portions Copyright [yyyy] [name of copyright owner]
1505N/A#
1505N/A# CDDL HEADER END
1505N/A#
1505N/A
1505N/A# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
1505N/A# Use is subject to license terms.
1505N/A
1505N/A# basic facet support
1505N/A
1505N/Afrom pkg.misc import EmptyI
1505N/Aimport fnmatch
1505N/A
1505N/Aclass Facets(dict):
1505N/A # store information on facets; subclass dict
1505N/A # and maintain ordered list of keys sorted
1505N/A # by length.
1505N/A
1505N/A # subclass __getitem_ so that queries w/
1505N/A # actual facets find match
1505N/A
1505N/A def __init__(self, init=EmptyI):
1505N/A dict.__init__(self)
1505N/A self.__keylist = []
1505N/A for i in init:
1505N/A self[i] = init[i]
1505N/A
1505N/A def __repr__(self):
1505N/A s = "<"
1505N/A s += ", ".join(["%s:%s" % (k, dict.__getitem__(self, k)) for k in self.__keylist])
1505N/A s += ">"
1505N/A
1505N/A return s
1505N/A
1505N/A def __setitem__(self, item, value):
1505N/A if not item.startswith("facet."):
1505N/A raise KeyError, 'key must start with "facet".'
1505N/A
1505N/A if not (value == True or value == False):
1505N/A raise ValueError, "value must be boolean"
1505N/A
1505N/A if item not in self:
1505N/A self.__keylist.append(item)
1505N/A self.__keylist.sort(cmp=lambda x, y: len(y) - len(x))
1505N/A dict.__setitem__(self, item, value)
1505N/A
1505N/A def __getitem__(self, item):
1505N/A """implement facet lookup algorithm here"""
1505N/A if not item.startswith("facet."):
1505N/A raise KeyError, "key must start w/ facet."
1505N/A
1505N/A if item in self:
1505N/A return dict.__getitem__(self, item)
1505N/A for k in self.__keylist:
1505N/A if fnmatch.fnmatch(item, k):
1505N/A return dict.__getitem__(self, k)
1505N/A
1505N/A return True # be inclusive
1505N/A
1505N/A def __delitem__(self, item):
1505N/A dict.__delitem__(self, item)
1505N/A self.__keylist.remove(item)
1505N/A
1505N/A def pop(self, item, default=None):
1505N/A self.__keylist.remove(item)
1505N/A return dict.pop(self, item, default)
1505N/A
1505N/A def popitem(self):
1505N/A popped = dict.popitem(self)
1505N/A self.__keylist.remove(popped[0])
1505N/A return popped
1505N/A
1505N/A def setdefault(self, item, default=None):
1505N/A if item not in self:
1505N/A self[item] = default
1505N/A return self[item]
1505N/A
1505N/A def update(self, d):
1505N/A for k, v in d.iteritems():
1505N/A self[k] = v
1505N/A
1505N/A def keys(self):
1505N/A return self.__keylist[:]
1505N/A
1505N/A def values(self):
1505N/A return [self[k] for k in self.__keylist]
1505N/A
1505N/A def items(self):
1505N/A return [a for a in self.iteritems()]
1505N/A
1505N/A def iteritems(self): # return in sorted order for display
1505N/A for k in self.__keylist:
1505N/A yield k, self[k]
1505N/A
1505N/A def copy(self):
1505N/A return Facets(self)
1505N/A
1505N/A def clear(self):
1505N/A self.__keylist = []
1505N/A dict.clear(self)
1505N/A
1505N/A def allow_action(self, action):
1505N/A """ determine if facets permit this action; if any facets
1505N/A allow it, return True; also return True if no facets are present"""
1505N/A facets = [k for k in action.attrs.keys() if k.startswith("facet.")]
1505N/A
1505N/A ret = True
1505N/A
1505N/A for f in facets:
1505N/A if self[f]:
1505N/A return True
1505N/A else:
1505N/A ret = False
1505N/A
1505N/A return ret