Remove duplication and add partial test

 * Remove code duplication
 * Update docstring
 * Add partial test -- Travis CI is Linux but has no XDG_DATA_DIRS env variable
 * PEP8 and Pyflakes cleanup
 
For PR https://github.com/python-imaging/Pillow/pull/682.