Minor improvements to CardGenerator and the corresponding unit test

Make sure that the file gets closed when writing a card to/reading a
card from disk. Also add a unit test for the error condition when trying
to import a card from a non-existent file.
Whoops, sorry about that. Fixed in 5cb1c6a. Guess I know which function I should write a unit test for next ;)