/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/pyutils.py

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""General Python convenience functions."""
 
18
 
 
19
import sys
 
20
 
 
21
 
 
22
def get_named_object(module_name, member_name=None):
 
23
    """Get the Python object named by a given module and member name.
 
24
 
 
25
    This is usually much more convenient than dealing with ``__import__``
 
26
    directly::
 
27
 
 
28
        >>> doc = get_named_object('breezy.pyutils', 'get_named_object.__doc__')
 
29
        >>> doc.splitlines()[0]
 
30
        'Get the Python object named by a given module and member name.'
 
31
 
 
32
    :param module_name: a module name, as would be found in sys.modules if
 
33
        the module is already imported.  It may contain dots.  e.g. 'sys' or
 
34
        'os.path'.
 
35
    :param member_name: (optional) a name of an attribute in that module to
 
36
        return.  It may contain dots.  e.g. 'MyClass.some_method'.  If not
 
37
        given, the named module will be returned instead.
 
38
    :raises: ImportError or AttributeError.
 
39
    """
 
40
    # We may have just a module name, or a module name and a member name,
 
41
    # and either may contain dots.  __import__'s return value is a bit
 
42
    # unintuitive, so we need to take care to always return the object
 
43
    # specified by the full combination of module name + member name.
 
44
    if member_name:
 
45
        # Give __import__ a from_list.  It will return the last module in
 
46
        # the dotted module name.
 
47
        attr_chain = member_name.split('.')
 
48
        from_list = attr_chain[:1]
 
49
        obj = __import__(module_name, {}, {}, from_list)
 
50
        for attr in attr_chain:
 
51
            obj = getattr(obj, attr)
 
52
    else:
 
53
        # We're just importing a module, no attributes, so we have no
 
54
        # from_list.  __import__ will return the first module in the dotted
 
55
        # module name, so we look up the module from sys.modules.
 
56
        __import__(module_name, globals(), locals(), [])
 
57
        obj = sys.modules[module_name]
 
58
    return obj
 
59
 
 
60
 
 
61
def calc_parent_name(module_name, member_name=None):
 
62
    """Determine the 'parent' of a given dotted module name and (optional)
 
63
    member name.
 
64
 
 
65
    The idea is that ``getattr(parent_obj, final_attr)`` will equal
 
66
    get_named_object(module_name, member_name).
 
67
 
 
68
    :return: (module_name, member_name, final_attr) tuple.
 
69
    """
 
70
# +SKIP is not recognized by python2.4
 
71
# Typical use is::
 
72
#
 
73
#     >>> parent_mod, parent_member, final_attr = calc_parent_name(
 
74
#     ...     module_name, member_name) # doctest: +SKIP
 
75
#     >>> parent_obj = get_named_object(parent_mod, parent_member)
 
76
#     ... # doctest: +SKIP
 
77
    if member_name is not None:
 
78
        split_name = member_name.rsplit('.', 1)
 
79
        if len(split_name) == 1:
 
80
            return (module_name, None, member_name)
 
81
        else:
 
82
            return (module_name, split_name[0], split_name[1])
 
83
    else:
 
84
        split_name = module_name.rsplit('.', 1)
 
85
        if len(split_name) == 1:
 
86
            raise AssertionError(
 
87
                'No parent object for top-level module %r' % (module_name,))
 
88
        else:
 
89
            return (split_name[0], None, split_name[1])