[csw-devel] [PATCH] Add support for reading password from a file by default

Maciej Bliziński maciej at opencsw.org
Fri Apr 22 14:10:56 CEST 2011


2011/4/22 Ben Walton <bwalton at opencsw.org>:
>> To get the password, you can:
>>
>> password = af.read() # reads the whole file
>>
>> and with stripping:
>>
>> password = af.read().strip()
>>
>> (strips ws from both sides)
>
> Do we want that though?  It shouldn't hurt, but it also shouldn't be
> necessary.  I'm only looking for strip()-like functionality to mimick
> chomp() from perl/ruby.

Right, we only need to strip the newline character if it's there.  I
personally use strip() as default, unless there's a reason do to
otherwise.  As far as reading from files, I usually stick to two
idioms:

file_descriptor.read() # to read the whole file

for line in file_descriptor:
   do_stuff_with(line) # for line-by-line processing

>> > +  except:
>>
>> Add an exception class, I suppose it'll be an IOError; let other
>> exceptions propagate.  Add a warning that the file couldn't be found
>> (logging.warning(...)).
>
> Ok, I wasn't sure what the best thing to catch was.  I thought that
> catching everything around this small block was the way to go.

I almost never catch everything.  In an unexpected scenario, that
block might be catching a surprising and revealing exception, which
should propagate rather than being caught and not handled.  The
handling code - using getpass - only handles the case of a missing
file, which manifests itself only by IOError.

When logging a warning, include the exception message as well:

except IOError, e:
  logging.warning("Could not read %s: %s", authfile, e)
  ...

Maciej


More information about the devel mailing list