1
0

Standardized imports

This commit is contained in:
Jon Michael Aanes 2025-03-01 23:36:22 +01:00
parent 0e98fe6225
commit 2e41125952

View File

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