327
294
indexes into the branch's revision history.
333
The ``Transport`` layer handles access to local or remote directories.
334
Each Transport object acts like a logical connection to a particular
335
directory, and it allows various operations on files within it. You can
336
*clone* a transport to get a new Transport connected to a subdirectory or
339
Transports are not used for access to the working tree. At present
340
working trees are always local and they are accessed through the regular
341
Python file io mechanisms.
346
Transports work in URLs. Take note that URLs are by definition only
347
ASCII - the decision of how to encode a Unicode string into a URL must be
348
taken at a higher level, typically in the Store. (Note that Stores also
349
escape filenames which cannot be safely stored on all filesystems, but
350
this is a different level.)
352
The main reason for this is that it's not possible to safely roundtrip a
353
URL into Unicode and then back into the same URL. The URL standard
354
gives a way to represent non-ASCII bytes in ASCII (as %-escapes), but
355
doesn't say how those bytes represent non-ASCII characters. (They're not
356
guaranteed to be UTF-8 -- that is common but doesn't happen everywhere.)
358
For example if the user enters the url ``http://example/%e0`` there's no
359
way to tell whether that character represents "latin small letter a with
360
grave" in iso-8859-1, or "latin small letter r with acute" in iso-8859-2
361
or malformed UTF-8. So we can't convert their URL to Unicode reliably.
363
Equally problematic if we're given a url-like string containing non-ascii
364
characters (such as the accented a) we can't be sure how to convert that
365
to the correct URL, because we don't know what encoding the server expects
366
for those characters. (Although this is not totally reliable we might still
367
accept these and assume they should be put into UTF-8.)
369
A similar edge case is that the url ``http://foo/sweet%2Fsour`` contains
370
one directory component whose name is "sweet/sour". The escaped slash is
371
not a directory separator. If we try to convert URLs to regular Unicode
372
paths this information will be lost.
374
This implies that Transports must natively deal with URLs; for simplicity
375
they *only* deal with URLs and conversion of other strings to URLs is done
376
elsewhere. Information they return, such as from ``list_dir``, is also in
377
the form of URL components.
380
Unicode and Encoding Support
381
============================
383
This section discusses various techniques that Bazaar uses to handle
384
characters that are outside the ASCII set.
389
When a ``Command`` object is created, it is given a member variable
390
accessible by ``self.outf``. This is a file-like object, which is bound to
391
``sys.stdout``, and should be used to write information to the screen,
392
rather than directly writing to ``sys.stdout`` or calling ``print``.
393
This file has the ability to translate Unicode objects into the correct
394
representation, based on the console encoding. Also, the class attribute
395
``encoding_type`` will effect how unprintable characters will be
396
handled. This parameter can take one of 3 values:
399
Unprintable characters will be represented with a suitable replacement
400
marker (typically '?'), and no exception will be raised. This is for
401
any command which generates text for the user to review, rather than
402
for automated processing.
403
For example: ``bzr log`` should not fail if one of the entries has text
404
that cannot be displayed.
407
Attempting to print and unprintable character will cause a UnicodeError.
408
This is for commands that are intended more as scripting support, rather
409
than plain user review.
410
For exampl: ``bzr ls`` is designed to be used with shell scripting. One
411
use would be ``bzr ls --null --unknows | xargs -0 rm``. If ``bzr``
412
printed a filename with a '?', the wrong file could be deleted. (At the
413
very least, the correct file would not be deleted). An error is used to
414
indicate that the requested action could not be performed.
417
Do not attempt to automatically convert Unicode strings. This is used
418
for commands that must handle conversion themselves.
419
For example: ``bzr diff`` needs to translate Unicode paths, but should
420
not change the exact text of the contents of the files.
423
``bzrlib.urlutils.unescape_for_display``
424
----------------------------------------
426
Because Transports work in URLs (as defined earlier), printing the raw URL
427
to the user is usually less than optimal. Characters outside the standard
428
set are printed as escapes, rather than the real character, and local
429
paths would be printed as ``file://`` urls. The function
430
``unescape_for_display`` attempts to unescape a URL, such that anything
431
that cannot be printed in the current encoding stays an escaped URL, but
432
valid characters are generated where possible.
435
297
Merge/review process
436
298
====================