Added libbe.ui.util.user for managing user ids.
authorW. Trevor King <wking@drexel.edu>
Sat, 12 Dec 2009 06:43:20 +0000 (01:43 -0500)
committerW. Trevor King <wking@drexel.edu>
Sat, 12 Dec 2009 06:43:20 +0000 (01:43 -0500)
libbe/storage/util/config.py
libbe/storage/vcs/base.py
libbe/ui/base.py
libbe/ui/util/user.py [new file with mode: 0644]
libbe/util/encoding.py

index ccd236b6dfd719daddab449e3b96211f43af984f..a0fea0ce949b21a93c4e1b66b318b7097ec85c21 100644 (file)
@@ -22,16 +22,15 @@ Create, save, and load the per-user config file at path().
 
 import ConfigParser
 import codecs
-import locale
 import os.path
-import sys
 
 import libbe
+import libbe.util.encoding
 if libbe.TESTING == True:
     import doctest
 
 
-default_encoding = sys.getfilesystemencoding() or locale.getpreferredencoding()
+default_encoding = libbe.util.encoding.get_filesystem_encoding()
 
 def path():
     """Return the path to the per-user config file"""
index abc7a017b490c5630b8ec82952f0560206d0ffc6..f8b072727fad606626a1ac79cfc12598bc4af83f 100644 (file)
@@ -207,11 +207,11 @@ class VCS(object):
     # methods for getting the BugDir situated in the filesystem
 
     def _find_root(self, path):
-        """
+        '''
         Search for an existing bug database dir and it's ancestors and
         return a BugDir rooted there.  Only called by __init__, and
         then only if sink_to_existing_root == True.
-        """
+        '''
         if not os.path.exists(path):
             self.root = None
             raise NoRootEntry(path)
@@ -229,9 +229,9 @@ class VCS(object):
             return beroot
 
     def _guess_storage(self, allow_storage_init=False):
-        """
+        '''
         Only called by __init__.
-        """
+        '''
         deepdir = self.get_path()
         if not os.path.exists(deepdir):
             deepdir = os.path.dirname(deepdir)
index d26115facc87d9fd0358aa2c2c0717877a73603a..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
@@ -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 (file)
index 0000000..de2138c
--- /dev/null
@@ -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)
index 21e40cfb8cddcb5fe9c4452a8e8764437a969357..67131bfb1d025e6296117176e9516cc9eb03e371 100644 (file)
@@ -43,6 +43,15 @@ def get_encoding():
         # Python 2.3 on windows doesn't know about 'XYZ' alias for 'cpXYZ'
     return encoding
 
+def get_input_encoding():
+    return get_encoding()
+
+def get_output_encoding():
+    return get_encoding():
+
+def get_filesystem_encoding():
+    return get_encoding()
+
 def known_encoding(encoding):
     """
     >>> known_encoding("highly-unlikely-encoding")
@@ -56,32 +65,5 @@ def known_encoding(encoding):
     except LookupError:
         return False
 
-def set_IO_stream_encodings(encoding):
-    sys.stdin = codecs.getreader(encoding)(sys.__stdin__)
-    sys.stdout = codecs.getwriter(encoding)(sys.__stdout__)
-    sys.stderr = codecs.getwriter(encoding)(sys.__stderr__)
-
-
-    def _guess_encoding(self):
-        return encoding.get_encoding()
-    def _check_encoding(value):
-        if value != None:
-            return encoding.known_encoding(value)
-    def _setup_encoding(self, new_encoding):
-        # change hook called before generator.
-        if new_encoding not in [None, settings_object.EMPTY]:
-            if self._manipulate_encodings == True:
-                encoding.set_IO_stream_encodings(new_encoding)
-    def _set_encoding(self, old_encoding, new_encoding):
-        self._setup_encoding(new_encoding)
-        self._prop_save_settings(old_encoding, new_encoding)
-
-    @_versioned_property(name="encoding",
-                         doc="""The default input/output encoding to use (e.g. "utf-8").""",
-                         change_hook=_set_encoding,
-                         generator=_guess_encoding,
-                         check_fn=_check_encoding)
-    def encoding(): return {}
-
 if libbe.TESTING == True:
     suite = doctest.DocTestSuite()