| Conditions | 16 |
| Total Lines | 118 |
| Code Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like elodie._update() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #!/usr/bin/env python |
||
| 239 | @click.command('update') |
||
| 240 | @click.option('--album', help='Update the image album.') |
||
| 241 | @click.option('--location', help=('Update the image location. Location ' |
||
| 242 | 'should be the name of a place, like "Las ' |
||
| 243 | 'Vegas, NV".')) |
||
| 244 | @click.option('--time', help=('Update the image time. Time should be in ' |
||
| 245 | 'YYYY-mm-dd hh:ii:ss or YYYY-mm-dd format.')) |
||
| 246 | @click.option('--title', help='Update the image title.') |
||
| 247 | @click.option('--debug', default=False, is_flag=True, |
||
| 248 | help='Override the value in constants.py with True.') |
||
| 249 | @click.argument('paths', nargs=-1, |
||
| 250 | required=True) |
||
| 251 | def _update(album, location, time, title, paths, debug): |
||
| 252 | """Update a file's EXIF. Automatically modifies the file's location and file name accordingly. |
||
| 253 | """ |
||
| 254 | constants.debug = debug |
||
| 255 | has_errors = False |
||
| 256 | result = Result() |
||
| 257 | |||
| 258 | files = set() |
||
| 259 | for path in paths: |
||
| 260 | path = os.path.expanduser(path) |
||
| 261 | if os.path.isdir(path): |
||
| 262 | files.update(FILESYSTEM.get_all_files(path, None)) |
||
| 263 | else: |
||
| 264 | files.add(path) |
||
| 265 | |||
| 266 | for current_file in files: |
||
| 267 | if not os.path.exists(current_file): |
||
| 268 | has_errors = True |
||
| 269 | result.append((current_file, False)) |
||
| 270 | log.warn('Could not find %s' % current_file) |
||
| 271 | log.all('{"source":"%s", "error_msg":"Could not find %s"}' % |
||
| 272 | (current_file, current_file)) |
||
| 273 | continue |
||
| 274 | |||
| 275 | current_file = os.path.expanduser(current_file) |
||
| 276 | |||
| 277 | # The destination folder structure could contain any number of levels |
||
| 278 | # So we calculate that and traverse up the tree. |
||
| 279 | # '/path/to/file/photo.jpg' -> '/path/to/file' -> |
||
| 280 | # ['path','to','file'] -> ['path','to'] -> '/path/to' |
||
| 281 | current_directory = os.path.dirname(current_file) |
||
| 282 | destination_depth = -1 * len(FILESYSTEM.get_folder_path_definition()) |
||
| 283 | destination = os.sep.join( |
||
| 284 | os.path.normpath( |
||
| 285 | current_directory |
||
| 286 | ).split(os.sep)[:destination_depth] |
||
| 287 | ) |
||
| 288 | |||
| 289 | media = Media.get_class_by_file(current_file, get_all_subclasses()) |
||
| 290 | if not media: |
||
| 291 | continue |
||
| 292 | |||
| 293 | updated = False |
||
| 294 | if location: |
||
| 295 | update_location(media, current_file, location) |
||
| 296 | updated = True |
||
| 297 | if time: |
||
| 298 | update_time(media, current_file, time) |
||
| 299 | updated = True |
||
| 300 | if album: |
||
| 301 | media.set_album(album) |
||
| 302 | updated = True |
||
| 303 | |||
| 304 | # Updating a title can be problematic when doing it 2+ times on a file. |
||
| 305 | # You would end up with img_001.jpg -> img_001-first-title.jpg -> |
||
| 306 | # img_001-first-title-second-title.jpg. |
||
| 307 | # To resolve that we have to track the prior title (if there was one. |
||
| 308 | # Then we massage the updated_media's metadata['base_name'] to remove |
||
| 309 | # the old title. |
||
| 310 | # Since FileSystem.get_file_name() relies on base_name it will properly |
||
| 311 | # rename the file by updating the title instead of appending it. |
||
| 312 | remove_old_title_from_name = False |
||
| 313 | if title: |
||
| 314 | # We call get_metadata() to cache it before making any changes |
||
| 315 | metadata = media.get_metadata() |
||
| 316 | title_update_status = media.set_title(title) |
||
| 317 | original_title = metadata['title'] |
||
| 318 | if title_update_status and original_title: |
||
| 319 | # @TODO: We should move this to a shared method since |
||
| 320 | # FileSystem.get_file_name() does it too. |
||
| 321 | original_title = re.sub(r'\W+', '-', original_title.lower()) |
||
| 322 | original_base_name = metadata['base_name'] |
||
| 323 | remove_old_title_from_name = True |
||
| 324 | updated = True |
||
| 325 | |||
| 326 | if updated: |
||
| 327 | updated_media = Media.get_class_by_file(current_file, |
||
| 328 | get_all_subclasses()) |
||
| 329 | # See comments above on why we have to do this when titles |
||
| 330 | # get updated. |
||
| 331 | if remove_old_title_from_name and len(original_title) > 0: |
||
|
|
|||
| 332 | updated_media.get_metadata() |
||
| 333 | updated_media.set_metadata_basename( |
||
| 334 | original_base_name.replace('-%s' % original_title, '')) |
||
| 335 | |||
| 336 | dest_path = FILESYSTEM.process_file(current_file, destination, |
||
| 337 | updated_media, move=True, allowDuplicate=True) |
||
| 338 | log.info(u'%s -> %s' % (current_file, dest_path)) |
||
| 339 | log.all('{"source":"%s", "destination":"%s"}' % (current_file, |
||
| 340 | dest_path)) |
||
| 341 | # If the folder we moved the file out of or its parent are empty |
||
| 342 | # we delete it. |
||
| 343 | FILESYSTEM.delete_directory_if_empty(os.path.dirname(current_file)) |
||
| 344 | FILESYSTEM.delete_directory_if_empty( |
||
| 345 | os.path.dirname(os.path.dirname(current_file))) |
||
| 346 | result.append((current_file, dest_path)) |
||
| 347 | # Trip has_errors to False if it's already False or dest_path is. |
||
| 348 | has_errors = has_errors is True or not dest_path |
||
| 349 | else: |
||
| 350 | has_errors = False |
||
| 351 | result.append((current_file, False)) |
||
| 352 | |||
| 353 | result.write() |
||
| 354 | |||
| 355 | if has_errors: |
||
| 356 | sys.exit(1) |
||
| 357 | |||
| 379 |