~qbzr-dev/qbzr/revision-selector

« back to all changes in this revision

Viewing changes to lib/util.py

  • Committer: Gary van der Merwe
  • Date: 2009-07-15 11:23:55 UTC
  • mfrom: (615.1.203 trunk)
  • Revision ID: garyvdm@gmail.com-20090715112355-8jovtikp24pqifui
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
from bzrlib.plugins.qbzr.lib import trace
43
43
from bzrlib.plugins.qbzr.lib import i18n
44
44
from bzrlib.plugins.qbzr.lib.i18n import gettext, N_, ngettext
 
45
 
 
46
# pyflakes says this is not needed, but it is.
45
47
import bzrlib.plugins.qbzr.lib.resources
46
48
 
 
49
from bzrlib.lazy_import import lazy_import
 
50
lazy_import(globals(), '''
 
51
from bzrlib import errors
 
52
from bzrlib.workingtree import WorkingTree
 
53
''')
 
54
 
47
55
 
48
56
_email_re = lazy_regex.lazy_compile(r'([a-z0-9_\-.+]+@[a-z0-9_\-.+]+)', re.IGNORECASE)
49
57
_link1_re = lazy_regex.lazy_compile(r'([\s>])(https?)://([^\s<>{}()]+[^\s.,<>{}()])', re.IGNORECASE)
977
985
def get_message(rev):
978
986
    return rev.message or gettext('(no message)')
979
987
 
 
988
def ensure_unicode(s, encoding='ascii'):
 
989
    """Convert s to unicode if s is plain string.
 
990
    Using encoding for unicode decode.
 
991
 
 
992
    In the case when s is not string, return it
 
993
    without any changes.
 
994
    """
 
995
    if isinstance(s, str):
 
996
        return s.decode(encoding)
 
997
    return s
 
998
 
 
999
 
 
1000
def open_tree(directory, ui_mode=False,
 
1001
    _critical_dialog=QtGui.QMessageBox.critical):
 
1002
    """Open working tree with its root at specified directory or above
 
1003
    (similar to WorkingTree.open_containing).
 
1004
    If there is no working tree and ui_mode is True then show GUI dialog
 
1005
    with error message and None will be returned. Otherwise errors
 
1006
    (NotBranchError or NoWorkingTree) will be propagated to caller.
 
1007
 
 
1008
    If directory is None then current directory will be used.
 
1009
 
 
1010
    @param _critical_dialog: could be used to provide mock object for testing.
 
1011
    """
 
1012
    if directory is None:
 
1013
        directory = u'.'
 
1014
    try:
 
1015
        return WorkingTree.open_containing(directory)[0]
 
1016
    except errors.NotBranchError:
 
1017
        if ui_mode:
 
1018
            _critical_dialog(None,
 
1019
                gettext("Error"),
 
1020
                gettext('Not a branch "%s"'
 
1021
                    ) % os.path.abspath(directory),
 
1022
                gettext('&Close'))
 
1023
            return None
 
1024
        else:
 
1025
            raise
 
1026
    except errors.NoWorkingTree:
 
1027
        if ui_mode:
 
1028
            _critical_dialog(None,
 
1029
                gettext("Error"),
 
1030
                gettext('No working tree exists for "%s"'
 
1031
                    ) % os.path.abspath(directory),
 
1032
                gettext('&Close'))
 
1033
            return None
 
1034
        else:
 
1035
            raise