From f8a498f76d7bbcb42cf7bbc80164d98bfe57f8ab Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 12 Dec 2009 01:43:20 -0500 Subject: Added libbe.ui.util.user for managing user ids. --- libbe/ui/util/user.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 libbe/ui/util/user.py (limited to 'libbe/ui/util/user.py') diff --git a/libbe/ui/util/user.py b/libbe/ui/util/user.py new file mode 100644 index 0000000..de2138c --- /dev/null +++ b/libbe/ui/util/user.py @@ -0,0 +1,84 @@ +# Copyright + +""" +Tools for getting, setting, creating, and parsing the user's id. For +example, + 'John Doe ' +Note that the Arch VCS backend *enforces* ids with this format. +""" + +import libbe.storage.util.config + +def _get_fallback_username(self): + name = None + for env in ["LOGNAME", "USERNAME"]: + if os.environ.has_key(env): + name = os.environ[env] + break + assert name != None + return name + +def _get_fallback_email(self): + hostname = gethostname() + name = _get_fallback_username() + return "%s@%s" % (name, hostname) + +def create_user_id(self, name, email=None): + """ + >>> create_id("John Doe", "jdoe@example.com") + 'John Doe ' + >>> create_id("John Doe") + 'John Doe' + """ + assert len(name) > 0 + if email == None or len(email) == 0: + return name + else: + return "%s <%s>" % (name, email) + +def parse_user_id(self, value): + """ + >>> parse_id("John Doe ") + ('John Doe', 'jdoe@example.com') + >>> parse_id("John Doe") + ('John Doe', None) + >>> try: + ... parse_id("John Doe ") + ... except AssertionError: + ... print "Invalid match" + Invalid match + """ + emailexp = re.compile("(.*) <([^>]*)>(.*)") + match = emailexp.search(value) + if match == None: + email = None + name = value + else: + assert len(match.groups()) == 3 + assert match.groups()[2] == "", match.groups() + email = match.groups()[1] + name = match.groups()[0] + assert name != None + assert len(name) > 0 + return (name, email) + +def get_user_id(self, storage=None): + """ + Sometimes the storage will also keep track of the user id (e.g. most VCSs). + """ + user = libbe.storage.util.config.get_val('user') + if user != None: + return user + if storage != None and hasattr(storage, 'get_user_id'): + user = vcs.get_user_id() + if user != None: + return user + name = _get_fallback_username() + email = _get_fallback_email() + user = _create_user_id(name, email) + return user + +def set_user_id(self, user_id): + """ + """ + user = libbe.storage.util.config.set_val('user', user_id) -- cgit From 4d057dab603f42ec40b911dbee6792dcf107bd14 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 13 Dec 2009 06:19:23 -0500 Subject: Converted libbe.storage.vcs.base to new Storage format. --- libbe/ui/util/user.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'libbe/ui/util/user.py') diff --git a/libbe/ui/util/user.py b/libbe/ui/util/user.py index de2138c..64eb30c 100644 --- a/libbe/ui/util/user.py +++ b/libbe/ui/util/user.py @@ -7,6 +7,10 @@ example, Note that the Arch VCS backend *enforces* ids with this format. """ +import re +from socket import gethostname + +import libbe import libbe.storage.util.config def _get_fallback_username(self): @@ -23,11 +27,11 @@ def _get_fallback_email(self): name = _get_fallback_username() return "%s@%s" % (name, hostname) -def create_user_id(self, name, email=None): +def create_user_id(name, email=None): """ - >>> create_id("John Doe", "jdoe@example.com") + >>> create_user_id("John Doe", "jdoe@example.com") 'John Doe ' - >>> create_id("John Doe") + >>> create_user_id("John Doe") 'John Doe' """ assert len(name) > 0 @@ -36,14 +40,14 @@ def create_user_id(self, name, email=None): else: return "%s <%s>" % (name, email) -def parse_user_id(self, value): +def parse_user_id(value): """ - >>> parse_id("John Doe ") + >>> parse_user_id("John Doe ") ('John Doe', 'jdoe@example.com') - >>> parse_id("John Doe") + >>> parse_user_id("John Doe") ('John Doe', None) >>> try: - ... parse_id("John Doe ") + ... parse_user_id("John Doe ") ... except AssertionError: ... print "Invalid match" Invalid match -- cgit From 55e0abfa27b693768f495044d10444d9d92e4fca Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 13 Dec 2009 07:39:55 -0500 Subject: More fixes for libbe.storage.vcs.hg + .git transition. --- libbe/ui/util/user.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'libbe/ui/util/user.py') diff --git a/libbe/ui/util/user.py b/libbe/ui/util/user.py index 64eb30c..a58f83a 100644 --- a/libbe/ui/util/user.py +++ b/libbe/ui/util/user.py @@ -13,7 +13,7 @@ from socket import gethostname import libbe import libbe.storage.util.config -def _get_fallback_username(self): +def get_fallback_username(self): name = None for env in ["LOGNAME", "USERNAME"]: if os.environ.has_key(env): @@ -22,9 +22,9 @@ def _get_fallback_username(self): assert name != None return name -def _get_fallback_email(self): +def get_fallback_email(self): hostname = gethostname() - name = _get_fallback_username() + name = get_fallback_username() return "%s@%s" % (name, hostname) def create_user_id(name, email=None): @@ -77,9 +77,9 @@ def get_user_id(self, storage=None): user = vcs.get_user_id() if user != None: return user - name = _get_fallback_username() - email = _get_fallback_email() - user = _create_user_id(name, email) + name = get_fallback_username() + email = get_fallback_email() + user = create_user_id(name, email) return user def set_user_id(self, user_id): -- cgit From 9b1f9db34189744ded87a28aaa395d9344593df2 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 14 Dec 2009 01:12:08 -0500 Subject: Transitioned assign to Command format --- libbe/ui/util/user.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'libbe/ui/util/user.py') diff --git a/libbe/ui/util/user.py b/libbe/ui/util/user.py index a58f83a..5b16b73 100644 --- a/libbe/ui/util/user.py +++ b/libbe/ui/util/user.py @@ -7,13 +7,14 @@ example, Note that the Arch VCS backend *enforces* ids with this format. """ +import os import re from socket import gethostname import libbe import libbe.storage.util.config -def get_fallback_username(self): +def get_fallback_username(): name = None for env in ["LOGNAME", "USERNAME"]: if os.environ.has_key(env): @@ -22,7 +23,7 @@ def get_fallback_username(self): assert name != None return name -def get_fallback_email(self): +def get_fallback_email(): hostname = gethostname() name = get_fallback_username() return "%s@%s" % (name, hostname) @@ -66,7 +67,7 @@ def parse_user_id(value): assert len(name) > 0 return (name, email) -def get_user_id(self, storage=None): +def get_user_id(storage=None): """ Sometimes the storage will also keep track of the user id (e.g. most VCSs). """ @@ -82,7 +83,7 @@ def get_user_id(self, storage=None): user = create_user_id(name, email) return user -def set_user_id(self, user_id): +def set_user_id(user_id): """ """ user = libbe.storage.util.config.set_val('user', user_id) -- cgit From 2f0ceedba5b6619faf476cd1aa67e826e91d5c7c Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 14 Dec 2009 03:29:20 -0500 Subject: Transitioned init to Command format --- libbe/ui/util/user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbe/ui/util/user.py') diff --git a/libbe/ui/util/user.py b/libbe/ui/util/user.py index 5b16b73..d6af89b 100644 --- a/libbe/ui/util/user.py +++ b/libbe/ui/util/user.py @@ -75,7 +75,7 @@ def get_user_id(storage=None): if user != None: return user if storage != None and hasattr(storage, 'get_user_id'): - user = vcs.get_user_id() + user = storage.get_user_id() if user != None: return user name = get_fallback_username() -- cgit