1643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#!/usr/bin/env python
2643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# Copyright (c) 2009, Google Inc. All rights reserved.
3643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# Copyright (c) 2009 Apple Inc. All rights reserved.
4231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#
5231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# Redistribution and use in source and binary forms, with or without
6231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# modification, are permitted provided that the following conditions are
7231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# met:
8d0825bca7fe65beaee391d30da42e937db621564Steve Block#
9231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Redistributions of source code must retain the above copyright
10231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# notice, this list of conditions and the following disclaimer.
11231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Redistributions in binary form must reproduce the above
12231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# copyright notice, this list of conditions and the following disclaimer
13231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# in the documentation and/or other materials provided with the
14231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# distribution.
15231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#     * Neither the name of Google Inc. nor the names of its
16231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# contributors may be used to endorse or promote products derived from
17231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# this software without specific prior written permission.
18d0825bca7fe65beaee391d30da42e937db621564Steve Block#
19231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
31231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockimport re
32231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
33d0825bca7fe65beaee391d30da42e937db621564Steve Block
34643ca7872b450ea4efacab6188849e5aac2ba161Steve Blockdef plural(noun):
35d0825bca7fe65beaee391d30da42e937db621564Steve Block    # This is a dumb plural() implementation that is just enough for our uses.
36643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    if re.search("h$", noun):
37643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        return noun + "es"
38643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    else:
39643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        return noun + "s"
40231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
41d0825bca7fe65beaee391d30da42e937db621564Steve Block
42643ca7872b450ea4efacab6188849e5aac2ba161Steve Blockdef pluralize(noun, count):
43643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    if count != 1:
44643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        noun = plural(noun)
45643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    return "%d %s" % (count, noun)
468a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block
478a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block
488a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Blockdef join_with_separators(list_of_strings, separator=', ', last_separator=', and '):
498a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block    if not list_of_strings:
508a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block        return ""
518a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block    if len(list_of_strings) == 1:
528a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block        return list_of_strings[0]
538a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block    return "%s%s%s" % (separator.join(list_of_strings[:-1]), last_separator, list_of_strings[-1])
54