This looks a lot of duplicated code..
I would factorize this stuff in a similar manner as config.py.