~launchpad-pqm/launchpad/devel

9209.14.3 by Michael Nelson
Rename and move user-agent detection function to lp.services and provide specific tests there.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Helpers for examining the browser user_agent header."""
5
6
__metaclass__ = type
7
__all__ = [
8
    'get_user_agent_distroseries',
9691.4.6 by Tim Penhey
Make the tal formatter match the expectations.
9
    'get_plural_text',
9209.14.3 by Michael Nelson
Rename and move user-agent detection function to lp.services and provide specific tests there.
10
    ]
11
12
import re
13
14
15
def get_user_agent_distroseries(user_agent_string):
16
    """Return the `DistroSeries` version number from the user-agent."""
17
18
    if user_agent_string is None:
19
        return None
20
21
    # We're matching on the Ubuntu/10.09 section of the user-agent string.
22
    pattern = 'Ubuntu/(?P<version>\d*\.\d*)'
23
    match = re.search(pattern, user_agent_string)
24
25
    if match is not None:
26
        # Great, the browser is telling us the platform is Ubuntu.
27
        # Now grab the Ubuntu series/version number:
28
        return match.groupdict()['version']
29
    else:
30
        return None
31
9691.4.6 by Tim Penhey
Make the tal formatter match the expectations.
32
33
def get_plural_text(count, singular, plural):
34
    """Return 'singular' if 'count' is 1, 'plural' otherwise."""
35
    if count == 1:
36
        return singular
37
    else:
38
        return plural