aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/ui
diff options
context:
space:
mode:
authorW. Trevor King <wking@drexel.edu>2009-12-12 01:43:20 -0500
committerW. Trevor King <wking@drexel.edu>2009-12-12 01:43:20 -0500
commitf8a498f76d7bbcb42cf7bbc80164d98bfe57f8ab (patch)
tree00e4fa4923684f40763222f11b3f0fd8b13208d4 /libbe/ui
parent8b4ad37815cbef1e06532179f9ca098588d9cb44 (diff)
downloadbugseverywhere-f8a498f76d7bbcb42cf7bbc80164d98bfe57f8ab.tar.gz
Added libbe.ui.util.user for managing user ids.
Diffstat (limited to 'libbe/ui')
-rw-r--r--libbe/ui/base.py23
-rw-r--r--libbe/ui/util/user.py84
2 files changed, 84 insertions, 23 deletions
diff --git a/libbe/ui/base.py b/libbe/ui/base.py
index d26115f..e69de29 100644
--- a/libbe/ui/base.py
+++ b/libbe/ui/base.py
@@ -1,23 +0,0 @@
- def _setup_user_id(self, user_id):
- if isinstance(self.storage, storage.vcs.base.VCS):
- self.storage.user_id = user_id
- def _guess_user_id(self):
- if isinstance(self.storage, storage.vcs.base.VCS):
- return self.storage.get_user_id()
- def _set_user_id(self, old_user_id, new_user_id):
- self._setup_user_id(new_user_id)
- self._prop_save_settings(old_user_id, new_user_id)
-
- @_versioned_property(name="user_id",
- doc=
-"""The user's prefered name, e.g. 'John Doe <jdoe@example.com>'. Note
-that the Arch VCS backend *enforces* ids with this format.""",
- change_hook=_set_user_id,
- generator=_guess_user_id)
- def user_id(): return {}
-
- @_versioned_property(name="default_assignee",
- doc=
-"""The default assignee for new bugs e.g. 'John Doe <jdoe@example.com>'.""")
- def default_assignee(): return {}
-
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 <jdoe@example.com>'
+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 <jdoe@example.com>'
+ >>> 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 <jdoe@example.com>")
+ ('John Doe', 'jdoe@example.com')
+ >>> parse_id("John Doe")
+ ('John Doe', None)
+ >>> try:
+ ... parse_id("John Doe <jdoe@example.com><what?>")
+ ... 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)