From 150355d3d76044d9ef73161d6301e79d17507443 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Tue, 28 Aug 2012 15:55:27 -0400 Subject: MetadataQuery: warn if a string is used as argument to by_groups/by_profiles/etc. instead of a list --- src/lib/Bcfg2/Server/Plugins/Metadata.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/lib/Bcfg2/Server/Plugins/Metadata.py b/src/lib/Bcfg2/Server/Plugins/Metadata.py index efb03d3ee..9eb21b383 100644 --- a/src/lib/Bcfg2/Server/Plugins/Metadata.py +++ b/src/lib/Bcfg2/Server/Plugins/Metadata.py @@ -9,6 +9,7 @@ import time import copy import fcntl import socket +import logging import lxml.etree import Bcfg2.Server import Bcfg2.Server.Lint @@ -23,6 +24,7 @@ try: except ImportError: has_django = False +logger = logging.getLogger(__name__) try: all @@ -275,16 +277,32 @@ class MetadataQuery(object): all_groups, all_groups_in_category): # resolver is set later self.by_name = by_name - self.names_by_groups = by_groups - self.names_by_profiles = by_profiles + self.names_by_groups = self._warn_string(by_groups) + self.names_by_profiles = self._warn_string(by_profiles) self.all_clients = get_clients self.all_groups = all_groups self.all_groups_in_category = all_groups_in_category + def _warn_string(self, func): + # it's a common mistake to call by_groups, etc., in templates with + # a single string argument instead of a list. that doesn't cause + # errors because strings are iterables. this decorator warns + # about that usage. + def inner(arg): + if isinstance(arg, str): + logger.warning("%s: %s takes a list as argument, not a string" % + (self.__class__.__name__, func.__name__)) + return func(arg) + return inner + def by_groups(self, groups): + # don't need to decorate this with _warn_string because + # names_by_groups is decorated return [self.by_name(name) for name in self.names_by_groups(groups)] def by_profiles(self, profiles): + # don't need to decorate this with _warn_string because + # names_by_profiles is decorated return [self.by_name(name) for name in self.names_by_profiles(profiles)] def all(self): -- cgit v1.2.3-1-g7c22