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"""
# 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)
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)
- 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 {}
-
--- /dev/null
+# 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)
# 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")
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()