aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/ui/util
diff options
context:
space:
mode:
Diffstat (limited to 'libbe/ui/util')
-rw-r--r--libbe/ui/util/__init__.py15
-rw-r--r--libbe/ui/util/editor.py115
-rw-r--r--libbe/ui/util/pager.py65
-rw-r--r--libbe/ui/util/user.py134
4 files changed, 329 insertions, 0 deletions
diff --git a/libbe/ui/util/__init__.py b/libbe/ui/util/__init__.py
new file mode 100644
index 0000000..3b461a5
--- /dev/null
+++ b/libbe/ui/util/__init__.py
@@ -0,0 +1,15 @@
+# Copyright (C) 2009-2010 W. Trevor King <wking@drexel.edu>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
diff --git a/libbe/ui/util/editor.py b/libbe/ui/util/editor.py
new file mode 100644
index 0000000..1a430c7
--- /dev/null
+++ b/libbe/ui/util/editor.py
@@ -0,0 +1,115 @@
+# Bugs Everywhere, a distributed bugtracker
+# Copyright (C) 2008-2010 Gianluca Montecchi <gian@grys.it>
+# W. Trevor King <wking@drexel.edu>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+"""
+Define editor_string(), a function that invokes an editor to accept
+user-produced text as a string.
+"""
+
+import codecs
+import locale
+import os
+import sys
+import tempfile
+
+import libbe
+import libbe.util.encoding
+
+if libbe.TESTING == True:
+ import doctest
+
+
+comment_marker = u"== Anything below this line will be ignored\n"
+
+class CantFindEditor(Exception):
+ def __init__(self):
+ Exception.__init__(self, "Can't find editor to get string from")
+
+def editor_string(comment=None, encoding=None):
+ """Invokes the editor, and returns the user-produced text as a string
+
+ >>> if "EDITOR" in os.environ:
+ ... del os.environ["EDITOR"]
+ >>> if "VISUAL" in os.environ:
+ ... del os.environ["VISUAL"]
+ >>> editor_string()
+ Traceback (most recent call last):
+ CantFindEditor: Can't find editor to get string from
+ >>> os.environ["EDITOR"] = "echo bar > "
+ >>> editor_string()
+ u'bar\\n'
+ >>> os.environ["VISUAL"] = "echo baz > "
+ >>> editor_string()
+ u'baz\\n'
+ >>> os.environ["VISUAL"] = "echo 'baz\\n== Anything below this line will be ignored\\nHi' > "
+ >>> editor_string()
+ u'baz\\n'
+ >>> del os.environ["EDITOR"]
+ >>> del os.environ["VISUAL"]
+ """
+ if encoding == None:
+ encoding = libbe.util.encoding.get_filesystem_encoding()
+ editor = None
+ for name in ('VISUAL', 'EDITOR'):
+ if name in os.environ and os.environ[name] != '':
+ editor = os.environ[name]
+ break
+ if editor == None:
+ raise CantFindEditor()
+ fhandle, fname = tempfile.mkstemp()
+ try:
+ if comment is not None:
+ cstring = u'\n'+comment_string(comment)
+ os.write(fhandle, cstring.encode(encoding))
+ os.close(fhandle)
+ oldmtime = os.path.getmtime(fname)
+ os.system("%s %s" % (editor, fname))
+ output = libbe.util.encoding.get_file_contents(
+ fname, encoding=encoding, decode=True)
+ output = trimmed_string(output)
+ if output.rstrip('\n') == "":
+ output = None
+ finally:
+ os.unlink(fname)
+ return output
+
+
+def comment_string(comment):
+ """
+ >>> comment_string('hello') == comment_marker+"hello"
+ True
+ """
+ return comment_marker + comment
+
+
+def trimmed_string(instring):
+ """
+ >>> trimmed_string("hello\\n"+comment_marker)
+ u'hello\\n'
+ >>> trimmed_string("hi!\\n" + comment_string('Booga'))
+ u'hi!\\n'
+ """
+ out = []
+ for line in instring.splitlines(True):
+ if line.startswith(comment_marker):
+ break
+ out.append(line)
+ return ''.join(out)
+
+if libbe.TESTING == True:
+ suite = doctest.DocTestSuite()
diff --git a/libbe/ui/util/pager.py b/libbe/ui/util/pager.py
new file mode 100644
index 0000000..88b58af
--- /dev/null
+++ b/libbe/ui/util/pager.py
@@ -0,0 +1,65 @@
+# Copyright (C) 2009-2010 W. Trevor King <wking@drexel.edu>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+"""
+Automatic pager for terminal output (a la Git).
+"""
+
+import sys, os, select
+
+# see http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
+def run_pager(paginate='auto'):
+ """
+ paginate should be one of 'never', 'auto', or 'always'.
+
+ usage: just call this function and continue using sys.stdout like
+ you normally would.
+ """
+ if paginate == 'never' \
+ or sys.platform == 'win32' \
+ or not hasattr(sys.stdout, 'isatty') \
+ or sys.stdout.isatty() == False:
+ return
+
+ if paginate == 'auto':
+ if 'LESS' not in os.environ:
+ os.environ['LESS'] = '' # += doesn't work on undefined var
+ # don't page if the input is short enough
+ os.environ['LESS'] += ' -FRX'
+ if 'PAGER' in os.environ:
+ pager = os.environ['PAGER']
+ else:
+ pager = 'less'
+
+ read_fd, write_fd = os.pipe()
+ if os.fork() == 0:
+ # child process
+ os.close(read_fd)
+ os.close(0)
+ os.dup2(write_fd, 1)
+ os.close(write_fd)
+ if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty() == True:
+ os.dup2(1, 2)
+ return
+
+ # parent process, become pager
+ os.close(write_fd)
+ os.dup2(read_fd, 0)
+ os.close(read_fd)
+
+ # Wait until we have input before we start the pager
+ select.select([0], [], [])
+ os.execlp(pager, pager)
diff --git a/libbe/ui/util/user.py b/libbe/ui/util/user.py
new file mode 100644
index 0000000..460a1dd
--- /dev/null
+++ b/libbe/ui/util/user.py
@@ -0,0 +1,134 @@
+# Copyright (C) 2009-2010 W. Trevor King <wking@drexel.edu>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+"""Tools for getting, setting, creating, and parsing the user's ID.
+
+IDs will look like 'John Doe <jdoe@example.com>'. Note that the
+:mod:`libbe.storage.vcs.arch <Arch VCS backend>` *enforces* IDs with
+this format.
+
+Do not confuse the user IDs discussed in this module, which refer to
+humans, with the "user IDs" discussed in :mod:`libbe.util.id`, which
+are human-readable tags refering to objects.
+"""
+
+try:
+ from email.utils import formataddr, parseaddr
+except ImportErrror: # adjust to old python < 2.5
+ from email.Utils import formataddr, parseaddr
+import os
+import re
+from socket import gethostname
+
+import libbe
+import libbe.storage.util.config
+
+def get_fallback_username():
+ """Return a username extracted from environmental variables.
+ """
+ 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():
+ """Return an email address extracted from environmental variables.
+ """
+ hostname = gethostname()
+ name = get_fallback_username()
+ return "%s@%s" % (name, hostname)
+
+def create_user_id(name, email=None):
+ """Create a user ID string from given `name` and `email` strings.
+
+ Examples
+ --------
+
+ >>> create_user_id("John Doe", "jdoe@example.com")
+ 'John Doe <jdoe@example.com>'
+ >>> create_user_id("John Doe")
+ 'John Doe'
+
+ See Also
+ --------
+ parse_user_id : inverse
+ """
+ assert len(name) > 0
+ if email == None or len(email) == 0:
+ return name
+ else:
+ return formataddr((name, email))
+
+def parse_user_id(value):
+ """Parse a user ID string into `name` and `email` strings.
+
+ Examples
+ --------
+
+ >>> parse_user_id("John Doe <jdoe@example.com>")
+ ('John Doe', 'jdoe@example.com')
+ >>> parse_user_id("John Doe")
+ ('John Doe', None)
+ >>> parse_user_id("John Doe <jdoe@example.com><what?>")
+ ('John Doe', 'jdoe@example.com')
+
+ See Also
+ --------
+ create_user_id : inverse
+ """
+ if '<' not in value:
+ return (value, None)
+ return parseaddr(value)
+
+def get_user_id(storage=None):
+ """Return a user ID, checking a list of possible sources.
+
+ The source order is:
+
+ 1. Global BE configuration.
+ 2. `storage.get_user_id`, if that function is defined.
+ 3. :func:`get_fallback_username` and :func:`get_fallback_email`.
+
+ Notes
+ -----
+ Sometimes the storage will keep track of the user ID (e.g. most
+ VCSs, see :meth:`libbe.storage.vcs.base.VCS.get_user_id`). If so,
+ we prefer that ID to the fallback, since the user has likely
+ configured it directly.
+ """
+ user = libbe.storage.util.config.get_val('user')
+ if user != None:
+ return user
+ if storage != None and hasattr(storage, 'get_user_id'):
+ user = storage.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(user_id):
+ """Set the user ID in a user's BE configuration.
+
+ See Also
+ --------
+ libbe.storage.util.config.set_val
+ """
+ user = libbe.storage.util.config.set_val('user', user_id)