From f8a498f76d7bbcb42cf7bbc80164d98bfe57f8ab Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 12 Dec 2009 01:43:20 -0500 Subject: [PATCH] Added libbe.ui.util.user for managing user ids. --- libbe/storage/util/config.py | 5 +-- libbe/storage/vcs/base.py | 8 ++-- libbe/ui/base.py | 23 ---------- libbe/ui/util/user.py | 84 ++++++++++++++++++++++++++++++++++++ libbe/util/encoding.py | 36 ++++------------ 5 files changed, 99 insertions(+), 57 deletions(-) create mode 100644 libbe/ui/util/user.py diff --git a/libbe/storage/util/config.py b/libbe/storage/util/config.py index ccd236b..a0fea0c 100644 --- a/libbe/storage/util/config.py +++ b/libbe/storage/util/config.py @@ -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""" diff --git a/libbe/storage/vcs/base.py b/libbe/storage/vcs/base.py index abc7a01..f8b0727 100644 --- a/libbe/storage/vcs/base.py +++ b/libbe/storage/vcs/base.py @@ -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) 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 '. 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 '.""") - 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 ' +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) diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index 21e40cf..67131bf 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -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() -- 2.26.2