def take_photo(): file_settings = FileSettings() logging.basicConfig( filename=file_settings.log_file(), format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO) logging.debug('Starting take_photo') try: logging.debug('Creating FileManager') file_mgr = FileManager(file_settings.project_dir) logging.debug('Getting file path') file_name_path = file_mgr.get_file_path() logging.debug('Photo file path name: %s', file_name_path) except: # Got an error trying to create the directory. # NEXT file_name_path isn't set on an exception. What exception # message is raised? Print that? Yes, need to debug the error. # NEXT Create a unit (or behavioral test) to check this logic - does # it need to be put into a separate module? logging.error('Failed to create: %s', os.path.dirname(file_name_path)) return logging.info('Taking photo with file path: %s', file_name_path) camera_settings = CameraSettings() # Camera is mounted upside down in the weatherproof housing # Need to rotate 180 degrees so photo is right side up camera_settings.rotation = 180 take_and_save_photo(camera_settings, file_name_path)
def test_get_file_path(self): # Successful cases root_dir = os.path.join("temp") try: f = FileManager(root_dir) file_path = f.get_file_path() # TODO: The regex path check is unix specific. Is there # a way to have a regexp check for os path separator? path, basename = os.path.split(file_path) self.assertRegexpMatches( path, "\d\d\d\d/\d\d/\d\d", "Verify path format is yyyy/mm/dd") self.assertRegexpMatches( basename, "\d\d\d\d-\d\d-\d\d_\d\d-\d\d-\d\d.jpg", "Verify file name format is year-month-day_hour-minute-second.jpg") finally: # Clean up - remove directories created just for this test os.removedirs(os.path.dirname(file_path)) self.assertFalse( os.path.isdir(root_dir), "Verify test directory does not exist after clean up") # Error Cases # Create a directory that can't be written to unwritable_dir = "unwritable" TEST_UMASK = 0o222 orig_umask = os.umask(TEST_UMASK) os.makedirs(unwritable_dir) try: # Make sure we get an exception if creating the directory fails f = FileManager(unwritable_dir) self.assertRaises(OSError, f.get_file_path ) finally: os.umask(orig_umask) os.removedirs(unwritable_dir)