digest_directory: Use _normalize functions for argument prep.
This is a pattern I want to try out. Not sure if it's ugly yet.
This commit is contained in:
		
							parent
							
								
									633dbfa3cf
								
							
						
					
					
						commit
						42cef093a5
					
				
					 1 changed files with 49 additions and 37 deletions
				
			
		|  | @ -1203,6 +1203,35 @@ class PhotoDB( | ||||||
|         If a Photo object already exists for a file, it will be added to the |         If a Photo object already exists for a file, it will be added to the | ||||||
|         correct album. |         correct album. | ||||||
|         ''' |         ''' | ||||||
|  |         def _normalize_directory(directory): | ||||||
|  |             directory = pathclass.Path(directory) | ||||||
|  |             if not directory.is_dir: | ||||||
|  |                 raise ValueError('Not a directory: %s' % directory) | ||||||
|  |             directory.correct_case() | ||||||
|  |             return directory | ||||||
|  | 
 | ||||||
|  |         def _normalize_exclude_directories(exclude_directories): | ||||||
|  |             if exclude_directories is None: | ||||||
|  |                 exclude_directories = self.config['digest_exclude_dirs'] | ||||||
|  |             return exclude_directories | ||||||
|  | 
 | ||||||
|  |         def _normalize_exclude_filenames(exclude_filenames): | ||||||
|  |             if exclude_filenames is None: | ||||||
|  |                 exclude_filenames = self.config['digest_exclude_files'] | ||||||
|  |             return exclude_filenames | ||||||
|  | 
 | ||||||
|  |         def _normalize_new_photo_kwargs(new_photo_kargs): | ||||||
|  |             if 'commit' in new_photo_kwargs: | ||||||
|  |                 new_photo_kwargs.pop('commit') | ||||||
|  |             if 'filepath' in new_photo_kwargs: | ||||||
|  |                 new_photo_kwargs.pop('filepath') | ||||||
|  |             return new_photo_kwargs | ||||||
|  | 
 | ||||||
|  |         def _normalize_new_photo_ratelimit(new_photo_ratelimit): | ||||||
|  |             if isinstance(new_photo_ratelimit, (int, float)): | ||||||
|  |                 new_photo_ratelimit = ratelimiter.Ratelimiter(allowance=1, period=new_photo_ratelimit) | ||||||
|  |             new_photo_ratelimit | ||||||
|  | 
 | ||||||
|         def create_or_fetch_photos(files): |         def create_or_fetch_photos(files): | ||||||
|             photos = [] |             photos = [] | ||||||
|             for filepath in files: |             for filepath in files: | ||||||
|  | @ -1218,16 +1247,18 @@ class PhotoDB( | ||||||
| 
 | 
 | ||||||
|         def create_or_fetch_current_album(albums_by_path, current_directory): |         def create_or_fetch_current_album(albums_by_path, current_directory): | ||||||
|             current_album = albums_by_path.get(current_directory.absolute_path, None) |             current_album = albums_by_path.get(current_directory.absolute_path, None) | ||||||
|             if current_album is None: |             if current_album is not None: | ||||||
|                 try: |                 return current_album | ||||||
|                     current_album = self.get_album_by_path(current_directory.absolute_path) | 
 | ||||||
|                 except exceptions.NoSuchAlbum: |             try: | ||||||
|                     current_album = self.new_album( |                 current_album = self.get_album_by_path(current_directory.absolute_path) | ||||||
|                         associated_directory=current_directory.absolute_path, |             except exceptions.NoSuchAlbum: | ||||||
|                         commit=False, |                 current_album = self.new_album( | ||||||
|                         title=current_directory.basename, |                     associated_directory=current_directory.absolute_path, | ||||||
|                     ) |                     commit=False, | ||||||
|                 albums_by_path[current_directory.absolute_path] = current_album |                     title=current_directory.basename, | ||||||
|  |                 ) | ||||||
|  |             albums_by_path[current_directory.absolute_path] = current_album | ||||||
|             return current_album |             return current_album | ||||||
| 
 | 
 | ||||||
|         def orphan_join_parent_album(albums_by_path, current_album, current_directory): |         def orphan_join_parent_album(albums_by_path, current_album, current_directory): | ||||||
|  | @ -1236,34 +1267,15 @@ class PhotoDB( | ||||||
|                 if parent is not None: |                 if parent is not None: | ||||||
|                     parent.add_child(current_album, commit=False) |                     parent.add_child(current_album, commit=False) | ||||||
| 
 | 
 | ||||||
|         directory = pathclass.Path(directory) |         directory = _normalize_directory(directory) | ||||||
|         if not directory.is_dir: |         exclude_directories = _normalize_exclude_directories(exclude_directories) | ||||||
|             raise ValueError('Not a directory: %s' % directory) |         exclude_filenames = _normalize_exclude_filenames(exclude_filenames) | ||||||
|         directory.correct_case() |         new_photo_kwargs = _normalize_new_photo_kwargs(new_photo_kwargs) | ||||||
| 
 |         new_photo_ratelimit = _normalize_new_photo_ratelimit(new_photo_ratelimit) | ||||||
|         if exclude_directories is None: |  | ||||||
|             exclude_directories = self.config['digest_exclude_dirs'] |  | ||||||
|         if exclude_filenames is None: |  | ||||||
|             exclude_filenames = self.config['digest_exclude_files'] |  | ||||||
| 
 |  | ||||||
|         if isinstance(new_photo_ratelimit, (int, float)): |  | ||||||
|             new_photo_ratelimit = ratelimiter.Ratelimiter(allowance=1, period=new_photo_ratelimit) |  | ||||||
| 
 |  | ||||||
|         if 'commit' in new_photo_kwargs: |  | ||||||
|             new_photo_kwargs.pop('commit') |  | ||||||
|         if 'filepath' in new_photo_kwargs: |  | ||||||
|             new_photo_kwargs.pop('filepath') |  | ||||||
| 
 | 
 | ||||||
|         if make_albums: |         if make_albums: | ||||||
|             try: |             albums_by_path = {} | ||||||
|                 album = self.get_album_by_path(directory.absolute_path) |             main_album = create_or_fetch_current_album(albums_by_path, directory) | ||||||
|             except exceptions.NoSuchAlbum: |  | ||||||
|                 album = self.new_album( |  | ||||||
|                     associated_directory=directory.absolute_path, |  | ||||||
|                     commit=False, |  | ||||||
|                     title=directory.basename, |  | ||||||
|                 ) |  | ||||||
|             albums_by_path = {directory.absolute_path: album} |  | ||||||
| 
 | 
 | ||||||
|         walk_generator = spinal.walk_generator( |         walk_generator = spinal.walk_generator( | ||||||
|             directory, |             directory, | ||||||
|  | @ -1290,7 +1302,7 @@ class PhotoDB( | ||||||
|             self.commit() |             self.commit() | ||||||
| 
 | 
 | ||||||
|         if make_albums: |         if make_albums: | ||||||
|             return album |             return main_album | ||||||
|         else: |         else: | ||||||
|             return None |             return None | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue