~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: Jelmer Vernooij
  • Date: 2010-08-29 14:37:51 UTC
  • mto: This revision was merged to the branch mainline in revision 5418.
  • Revision ID: jelmer@samba.org-20100829143751-9ry91e6u887gswiz
Move some bzrdir-specific tests to bzrlib.tests.per_bzrdir.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""Classes to provide name-to-object registry-like support."""
18
18
 
19
19
 
20
 
from bzrlib.pyutils import get_named_object
21
 
 
22
 
 
23
20
class _ObjectGetter(object):
24
21
    """Maintain a reference to an object, and return the object on request.
25
22
 
61
58
        return the imported object.
62
59
        """
63
60
        if not self._imported:
64
 
            self._obj = get_named_object(self._module_name, self._member_name)
65
 
            self._imported = True
 
61
            self._do_import()
66
62
        return super(_LazyObjectGetter, self).get_obj()
67
63
 
 
64
    def _do_import(self):
 
65
        if self._member_name:
 
66
            segments = self._member_name.split('.')
 
67
            names = segments[0:1]
 
68
        else:
 
69
            names = [self._member_name]
 
70
        obj = __import__(self._module_name, globals(), locals(), names)
 
71
        if self._member_name:
 
72
            for segment in segments:
 
73
                obj = getattr(obj, segment)
 
74
        self._obj = obj
 
75
        self._imported = True
 
76
 
68
77
    def __repr__(self):
69
 
        return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
 
78
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
70
79
            self.__class__.__module__, self.__class__.__name__, id(self),
71
 
            self._module_name, self._member_name, self._imported)
 
80
            self._module_name, self._member_name)
72
81
 
73
82
 
74
83
class Registry(object):