# Copyright (c) 2014 Rackspace, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # implied. # See the License for the specific language governing permissions and # limitations under the License. import cgi import itertools import pprint from oslo_log import log LOG = log.getLogger(__name__) class dict2obj(object): """Creates objects that behave much like a dictionaries.""" def __init__(self, d): for k in d: if isinstance(d[k], dict): self.__dict__[k] = dict2obj(d[k]) elif isinstance(d[k], (list, tuple)): l = [] for v in d[k]: if isinstance(v, dict): l.append(dict2obj(v)) else: l.append(v) self.__dict__[k] = l else: self.__dict__[k] = d[k] def __getitem__(self, name): if name in self.__dict__: return self.__dict__[name] def __iter__(self): return iter(self.__dict__.keys()) def __repr__(self): return pprint.pformat(self.__dict__) def help_escape(potentially_bad_string): if potentially_bad_string is None: LOG.warning('Should not happen: trying to escape a None object') return cgi.escape(potentially_bad_string or "") # remove duplicates # see http://bit.ly/1mX2Vcb for details def remove_duplicates(data): """Remove duplicates from the data (normally a list). The data must be sortable and have an equality operator """ data = sorted(data) return [k for k, _ in itertools.groupby(data)]