82
81
warnings.warn("register_transport(override) is deprecated")
83
82
_protocol_handlers.setdefault(prefix, []).insert(0, klass)
85
def _add_hints_to_get(klass):
86
if getattr(klass,'_get_without_hints',None) is None:
87
# Importing inspect is costly (see
88
# bzrlib.inspect_for_copy). But, on the other hand, it
89
# provides us the service we want in an official way, so
90
# better use it instead of rewriting our own version.
92
args, varargs, varkw, defaults = inspect.getargspec(klass.get)
94
# Looks like we have a candidate here
95
mutter('in %(klass)r, original proto is: %(name)s%(joined)s' % \
96
dict(klass=klass, name='get',
97
joined=inspect.formatargspec(args, varargs, varkw,
99
klass._get_without_hints = klass.get
100
def get(self, *gvarargs, **hints):
101
return self._get_without_hints(*gvarargs)
105
args, varargs, varkw, defaults = inspect.getargspec(klass.get)
106
mutter('in %(klass)r, proto is now: %(name)s%(joined)s' % \
107
dict(klass=klass, name='get',
108
joined=inspect.formatargspec(args, varargs, varkw,
113
85
def register_lazy_transport(scheme, module, classname):
114
86
"""Register lazy-loaded transport class.
128
100
def _loader(base):
129
101
mod = __import__(module, globals(), locals(), [classname])
130
102
klass = getattr(mod, classname)
131
# FIXME: Cache the patched transports ?
132
if _add_hints_to_get(klass):
133
symbol_versioning.warn('Transport %s should declare a **hints'
134
' parameter for its get method'
138
103
return klass(base)
139
104
_loader.module = module
140
_loader.classname = classname
141
105
register_transport(scheme, _loader)
228
192
(other.start, other.length, other.ranges))
231
class TransportHints(dict):
232
"""A specialization of dict targeted to hints handling.
234
This class is only a helper for daughter classes and serve no
235
purpose by itself: its main purpose is to simplify the
236
writing of the daughter classes respecting some simple rules.
239
_deprecated_hints = {'deprecated_hint_example': 'use shiny_hint instead'}
240
"""Hint name associated with the explanation presented as a note"""
243
"""Hint name with its default value"""
245
def __init__(self, **hints):
246
"""Init object from daughter classes definitions"""
247
for (name, value) in hints.iteritems():
248
status, value = self.check_hint(name, value)
249
if status is 'valid':
251
elif status is 'deprecated':
252
symbol_versioning.warn('hint %s is deprecated: %s' % (name,
256
raise errors.UnknownHint(name)
259
for name, value in self._valid_hints.iteritems():
260
if not self.has_key(name):
263
def check_hint(self, name, value):
264
if self._valid_hints.has_key(name):
265
return 'valid', value
266
elif self._deprecated_hints.has_key(name):
267
return 'deprecated', self._deprecated_hints[name]
269
return 'unknown', None
272
class TransportGetHints(TransportHints):
273
"""Hints for transport get method"""
275
_valid_hints = TransportHints._valid_hints
277
# When a transport is queried for a file, it will silently
278
# follow redirections (if any) except if told otherwise.
279
_valid_hints['follow_redirections'] = True
282
195
class Transport(object):
283
196
"""This class encapsulates methods for retrieving or putting a file
284
197
from/to a storage location.
308
221
super(Transport, self).__init__()
312
def create_get_hints(klass, **hints):
313
"""Create a hints object to be used with the get method"""
314
return TransportGetHints(**hints)
316
224
def _translate_error(self, e, path, raise_generic=True):
317
225
"""Translate an IOError or OSError into an appropriate bzr error.
524
432
"(but must claim to be listable "
525
433
"to trigger this error).")
527
def get(self, relpath, **hints):
435
def get(self, relpath):
528
436
"""Get the file at the given relative path.
530
438
:param relpath: The relative path to the file
1240
1148
self._adapted = adapted
1241
1149
self._calls = []
1243
def get(self, name, **hints):
1244
# There is only one test using the information collected
1245
# below and it asks for the name only. So we do not
1246
# record the hints parameter.
1247
# FIXME: Fix the test instead, and record the hints !
1151
def get(self, name):
1248
1152
self._calls.append((name,))
1249
return self._adapted.get(name, **hints)
1153
return self._adapted.get(name)
1251
1155
def __getattr__(self, name):
1252
1156
"""Thunk all undefined access through to self._adapted."""