~jdstrand/ufw/0.33

« back to all changes in this revision

Viewing changes to src/util.py

  • Committer: Jamie Strandboge
  • Date: 2012-07-06 16:28:17 UTC
  • mfrom: (774.1.10 0.32)
  • Revision ID: jamie@canonical.com-20120706162817-xifxo77ol8rb817e
pull in last minute fixes from 0.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
292
292
    out = sp2.communicate()[0]
293
293
    return [sp2.returncode, str(out)]
294
294
 
 
295
# TODO: this is pretty horrible. We should be using only unicode strings
 
296
#       internally and decode() when printing rather than doing this.
 
297
def _print(output, s):
 
298
    '''Implement our own print statement that will output utf-8 when
 
299
       appropriate.'''
 
300
    try: # python3
 
301
        writer = output.buffer
 
302
    except:
 
303
        writer = output
 
304
 
 
305
    try:
 
306
        out = s.encode('utf-8', 'ignore')
 
307
    except:
 
308
        out = s
 
309
 
 
310
    writer.write(bytes(out))
 
311
    output.flush()
 
312
 
295
313
 
296
314
def error(out, do_exit=True):
297
315
    '''Print error message and exit'''
298
316
    try:
299
 
        print("ERROR: %s" % (out), file=sys.stderr)
300
 
        sys.stderr.flush()
 
317
        _print(sys.stderr, 'ERROR: %s\n' % out)
301
318
    except IOError:
302
319
        pass
303
320
 
308
325
def warn(out):
309
326
    '''Print warning message'''
310
327
    try:
311
 
        print("WARN: %s" % (out), file=sys.stderr)
312
 
        sys.stderr.flush()
 
328
        _print(sys.stderr, 'WARN: %s\n' % out)
313
329
    except IOError:
314
330
        pass
315
331
 
318
334
    '''Print message'''
319
335
    try:
320
336
        if newline:
321
 
            print("%s" % (out), file=output)
 
337
            _print(output, '%s\n' % out)
322
338
        else:
323
 
            print("%s" % (out), file=output, end="")
324
 
        output.flush()
 
339
            _print(output, '%s' % out)
325
340
    except IOError:
326
341
        pass
327
342
 
330
345
    '''Print debug message'''
331
346
    if DEBUGGING:
332
347
        try:
333
 
            print("DEBUG: %s" % (out), file=sys.stderr)
334
 
            sys.stderr.flush()
 
348
            _print(sys.stderr, 'DEBUG: %s\n' % out)
335
349
        except IOError:
336
350
            pass
337
351