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