1 #!/usr/bin/env python 2 3 """ 4 Type deduction operations. 5 6 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 import sys 23 24 try: 25 set 26 except NameError: 27 from sets import Set as set 28 29 def get_object_types_for_usage(usage, objtable, name, unit_name, all_attributes, importer=None): 30 31 """ 32 Return for the given attribute 'usage', using the 'objtable', the object 33 types which satisfy such usage, reporting any problems for the given 'name' 34 and 'unit_name'. Each object type is given as a tuple of the form (type, 35 is_static). 36 37 Where 'all_attributes' is set to a true value, all attributes in a usage 38 list must be matched to provide object types. Otherwise, the presence of any 39 attribute from a usage list in a type's set of attributes will be enough to 40 provide that type as a suitable object type. 41 42 If 'importer' is specified and not None, it will be used to record warnings 43 about unsatisfiable usage situations. 44 """ 45 46 all_objtypes = set() 47 48 for attrnames in usage: 49 attrnames = attrnames or () 50 51 # Determine whether all attributes are required. 52 53 if all_attributes: 54 objtypes = objtable.all_possible_objects_plus_status(attrnames) 55 else: 56 objtypes = objtable.any_possible_objects_plus_status(attrnames) 57 if not objtypes and importer: 58 importer.add_usage_failure(all_attributes, unit_name, name, attrnames.keys()) 59 60 all_objtypes.update(objtypes) 61 62 return all_objtypes 63 64 def filter_using_self(objtypes, cls): 65 66 """ 67 Filter the given 'objtypes' (a collection of object names and static 68 indicators) for the given 'cls', returning only the (name, static) elements 69 compatible with 'cls' and its descendants. 70 """ 71 72 descendants = cls.all_descendants() 73 filtered = [] 74 for objname, is_static in objtypes: 75 if objname == cls.full_name() or objname in descendants: 76 filtered.append((objname, is_static)) 77 return filtered 78 79 # vim: tabstop=4 expandtab shiftwidth=4