diff --git a/obsidian_import/__init__.py b/obsidian_import/__init__.py index 007a500..5cbe719 100644 --- a/obsidian_import/__init__.py +++ b/obsidian_import/__init__.py @@ -84,7 +84,7 @@ def import_workout_csv(vault: ObsidianVault, rows: Rows) -> int: def import_step_counts_csv(vault: ObsidianVault, rows: Rows) -> int: - MINIMUM = 300 + MINIMUM_STEPS = 300 num_updated = 0 @@ -100,7 +100,7 @@ def import_step_counts_csv(vault: ObsidianVault, rows: Rows) -> int: } for date, steps in steps_per_date.items(): - if steps < MINIMUM: + if steps < MINIMUM_STEPS: continue was_updated = vault.add_statistic(date, 'Steps', steps) if was_updated: @@ -167,18 +167,6 @@ def import_activity_sample_csv( return num_updated -def import_activity_sample_csv_from_file( - vault: ObsidianVault, - data_path: Path, - content_mapper, - **kwargs, -) -> int: - rows = load_csv_file(data_path) - logger.info('Loaded CSV with %d lines (%s)', len(rows), data_path) - num_updated = import_activity_sample_csv(vault, rows, content_mapper, **kwargs) - logger.info('Updated %d files', num_updated) - - def map_watched_series_content(sample: RealizedActivitySample) -> EventContent: subject = sample.single_label_with_category('series.name') comment = '{} Episode {}: *{}*'.format( @@ -203,49 +191,34 @@ def map_games_played_content(sample: RealizedActivitySample) -> EventContent: ) -def import_watched_series_csv_from_file(vault: ObsidianVault) -> int: - data_path = Path('output/show_episodes_watched.csv') - return import_activity_sample_csv_from_file( - vault, - data_path, - map_watched_series_content, - ) +PATH_WATCHED = Path('output/show_episodes_watched.csv') +PATH_PLAYED = Path('output/games_played.csv') +PATH_WORKOUT = Path('/home/jmaa/Notes/workout.csv') +PATH_STEP_COUNTS = Path( + '/home/jmaa/personal-archive/misc-data/step_counts_2023-07-26_to_2024-09-21.csv', +) -def import_played_games_csv_from_file(vault: ObsidianVault) -> int: - data_path = Path('output/games_played.csv') - if not data_path.exists(): - logger.warning('Skipping import of played games: %s is missing', data_path) - return 0 - return import_activity_sample_csv_from_file( - vault, - data_path, - map_games_played_content, - group_category='game.name', - ) - +IMPORTERS = [ + {'path': PATH_WORKOUT, 'import_rows': import_workout_csv}, + {'path': PATH_STEP_COUNTS, 'import_rows': import_step_counts_csv}, + {'path': PATH_PLAYED, 'import_rows': lambda vault, rows: import_activity_sample_csv(vault, rows, map_games_played_content, group_category='game.name',) }, + {'path': PATH_WATCHED, 'import_rows': lambda vault, rows: import_activity_sample_csv(vault, rows, map_watched_series_content) }, +] def import_data(obsidian_path: Path, dry_run=True): vault = ObsidianVault(obsidian_path, read_only=dry_run and 'silent' or None) - if False: - data_path = Path('/home/jmaa/Notes/workout.csv') - rows = load_csv_file(data_path) + for import_def in IMPORTERS: + if not import_def['path'].exists(): + logger.warning('Skipping %s: %s is missing', import_def['import_rows'], import_def['path']) + continue + rows = load_csv_file(import_def['path']) logger.info('Loaded CSV with %d lines', len(rows)) - num_updated = import_workout_csv(vault, rows) - logger.info('Updated %d files', num_updated) + num_files_updated = import_def['import_rows'](vault, rows) + logger.info('Updated %d files', num_files_updated) + del import_def, rows - if False: - data_path = Path( - '/home/jmaa/personal-archive/misc-data/step_counts_2023-07-26_to_2024-09-21.csv', - ) - rows = load_csv_file(data_path) - logger.info('Loaded CSV with %d lines', len(rows)) - num_updated = import_step_counts_csv(vault, rows) - logger.info('Updated %d files', num_updated) - - import_played_games_csv_from_file(vault) - import_watched_series_csv_from_file(vault) num_dirty = len([f for f in vault.internal_file_text_cache.values() if f.is_dirty]) logger.info('dirty files in cache: %d', num_dirty)