/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 bzrlib/commands.py

  • Committer: John Arbash Meinel
  • Date: 2006-01-06 03:02:46 UTC
  • mto: (1685.1.1 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060106030246-f6939f2747212579
Updated cmd_inventory. Changing from having each Command request an encoded stdout to providing one before calling run()

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
from warnings import warn
33
33
from inspect import getdoc
34
34
import errno
 
35
import codecs
35
36
 
36
37
import bzrlib
37
38
import bzrlib.trace
184
185
    hidden
185
186
        If true, this command isn't advertised.  This is typically
186
187
        for commands intended for expert users.
 
188
 
 
189
    encoding_type
 
190
        Command objects will get a 'outf' attribute, which has been
 
191
        setup to properly handle encoding of unicode strings.
 
192
        encoding_type determines what will happen when characters cannot
 
193
        be encoded
 
194
            strict - abort if we cannot decode
 
195
            replace - put in a bogus character (typically '?')
 
196
            exact - do not encode sys.stdout
 
197
 
187
198
    """
188
199
    aliases = []
189
200
    takes_args = []
190
201
    takes_options = []
 
202
    encoding_type = 'strict'
191
203
 
192
204
    hidden = False
193
205
    
208
220
            r[o.name] = o
209
221
        return r
210
222
 
 
223
    def _setup_stdout(self):
 
224
        """Return a file linked to stdout, which has proper encoding."""
 
225
        assert self.encoding_type in ['strict', 'exact', 'replace']
 
226
 
 
227
        # Originally I was using self.stdout, but that looks
 
228
        # *way* too much like sys.stdout
 
229
        if self.encoding_type == 'exact':
 
230
            self.outf = sys.stdout
 
231
            return
 
232
 
 
233
        output_encoding = getattr(sys.stdout, 'encoding', None)
 
234
        if not output_encoding:
 
235
            output_encoding = bzrlib.user_encoding
 
236
            mutter('encoding stdout bzrlib.user_encoding %r', output_encoding)
 
237
        else:
 
238
            mutter('encoding stdout log as sys.stdout encoding %r', output_encoding)
 
239
 
 
240
        # use 'replace' so that we don't abort if trying to write out
 
241
        # in e.g. the default C locale.
 
242
        self.outf = codecs.getwriter(output_encoding)(sys.stdout, errors=self.encoding_type)
 
243
 
211
244
    def run_argv(self, argv):
212
245
        """Parse command line and run."""
213
246
        args, opts = parse_args(self, argv)
230
263
        all_cmd_args = cmdargs.copy()
231
264
        all_cmd_args.update(cmdopts)
232
265
 
 
266
        self._setup_stdout()
 
267
 
233
268
        return self.run(**all_cmd_args)
234
269
    
235
270
    def run(self):