1 # Copyright (C) 2009-2010 W. Trevor King <wking@drexel.edu>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 """Tools for getting, setting, creating, and parsing the user's ID.
19 IDs will look like 'John Doe <jdoe@example.com>'. Note that the
20 :mod:`libbe.storage.vcs.arch <Arch VCS backend>` *enforces* IDs with
23 Do not confuse the user IDs discussed in this module, which refer to
24 humans, with the "user IDs" discussed in :mod:`libbe.util.id`, which
25 are human-readable tags refering to objects.
29 from email.utils import formataddr, parseaddr
30 except ImportErrror: # adjust to old python < 2.5
31 from email.Utils import formataddr, parseaddr
34 from socket import gethostname
37 import libbe.storage.util.config
39 def get_fallback_username():
40 """Return a username extracted from environmental variables.
43 for env in ["LOGNAME", "USERNAME"]:
44 if os.environ.has_key(env):
45 name = os.environ[env]
50 def get_fallback_email():
51 """Return an email address extracted from environmental variables.
53 hostname = gethostname()
54 name = get_fallback_username()
55 return "%s@%s" % (name, hostname)
57 def create_user_id(name, email=None):
58 """Create a user ID string from given `name` and `email` strings.
63 >>> create_user_id("John Doe", "jdoe@example.com")
64 'John Doe <jdoe@example.com>'
65 >>> create_user_id("John Doe")
70 parse_user_id : inverse
73 if email == None or len(email) == 0:
76 return formataddr((name, email))
78 def parse_user_id(value):
79 """Parse a user ID string into `name` and `email` strings.
84 >>> parse_user_id("John Doe <jdoe@example.com>")
85 ('John Doe', 'jdoe@example.com')
86 >>> parse_user_id("John Doe")
88 >>> parse_user_id("John Doe <jdoe@example.com><what?>")
89 ('John Doe', 'jdoe@example.com')
93 create_user_id : inverse
97 return parseaddr(value)
99 def get_user_id(storage=None):
100 """Return a user ID, checking a list of possible sources.
104 1. Global BE configuration.
105 2. `storage.get_user_id`, if that function is defined.
106 3. :func:`get_fallback_username` and :func:`get_fallback_email`.
110 Sometimes the storage will keep track of the user ID (e.g. most
111 VCSs, see :meth:`libbe.storage.vcs.base.VCS.get_user_id`). If so,
112 we prefer that ID to the fallback, since the user has likely
113 configured it directly.
115 user = libbe.storage.util.config.get_val('user')
118 if storage != None and hasattr(storage, 'get_user_id'):
119 user = storage.get_user_id()
122 name = get_fallback_username()
123 email = get_fallback_email()
124 user = create_user_id(name, email)
127 def set_user_id(user_id):
128 """Set the user ID in a user's BE configuration.
132 libbe.storage.util.config.set_val
134 user = libbe.storage.util.config.set_val('user', user_id)