From 85a3af6299e758e897010d7087eea8a92e6e7ea3 Mon Sep 17 00:00:00 2001 From: amin-hdn <94766481+amin-hdn@users.noreply.github.com> Date: Mon, 16 Dec 2024 09:34:24 +0100 Subject: [PATCH 1/3] add export to csv --- flomo/tracker.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/flomo/tracker.py b/flomo/tracker.py index d8b2743..b367af4 100644 --- a/flomo/tracker.py +++ b/flomo/tracker.py @@ -138,3 +138,23 @@ def show_sessions(): console.print(table) db.conn.close() + +def export_to_csv(self, file_path: str): + """ + Exports all sessions in the database to a CSV file. + + Args: + file_path (str): The path to save the CSV file. + """ + # Fetch all sessions from the database + sessions = self.get_sessions() + + # Define the column headers + headers = ["id", "date_time", "tag", "name", "total_time"] + + # Write to CSV + with open(file_path, mode="w", newline="", encoding="utf-8") as csv_file: + writer = csv.writer(csv_file) + writer.writerow(headers) # Write headers + writer.writerows(sessions) # Write session rows + From 064b31828d1309cd6d6900ec5f8804d7546f95be Mon Sep 17 00:00:00 2001 From: amin-hdn <94766481+amin-hdn@users.noreply.github.com> Date: Mon, 16 Dec 2024 09:40:09 +0100 Subject: [PATCH 2/3] add export command --- flomo/cli.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/flomo/cli.py b/flomo/cli.py index ee01fe2..4aef46c 100644 --- a/flomo/cli.py +++ b/flomo/cli.py @@ -152,6 +152,24 @@ def update(session_id: str, tag: str | None, name: str | None): helpers.error_log(str(e)) print(e) +@flomo.command(aliases=["e"]) +@click.argument("file_path", default="sessions_export.csv") +def export(file_path: str): + """ + Export all sessions to a CSV file. + + Args: + file_path (str): The file path where the CSV will be saved. + """ + try: + db = tracker.Tracker() + db.export_to_csv(file_path) + db.conn.close() + print(f"Exported sessions to {file_path}") + except Exception as e: + helpers.error_log(f"Export Error: {e}") + print(f"Error exporting to CSV: {e}") + @flomo.command(aliases=["c"]) @click.option( From 3ad0e2e9520bae31a951d94031e531fafbcd0da8 Mon Sep 17 00:00:00 2001 From: amin-hdn Date: Mon, 6 Jan 2025 20:49:34 +0100 Subject: [PATCH 3/3] the fonction of export is completed --- flomo/cli.py | 6 +++--- flomo/tracker.py | 37 ++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/flomo/cli.py b/flomo/cli.py index 4aef46c..0865421 100644 --- a/flomo/cli.py +++ b/flomo/cli.py @@ -152,8 +152,8 @@ def update(session_id: str, tag: str | None, name: str | None): helpers.error_log(str(e)) print(e) -@flomo.command(aliases=["e"]) -@click.argument("file_path", default="sessions_export.csv") +@flomo.command(aliases=["x"]) +@click.argument('file_path') def export(file_path: str): """ Export all sessions to a CSV file. @@ -163,7 +163,7 @@ def export(file_path: str): """ try: db = tracker.Tracker() - db.export_to_csv(file_path) + db.export(file_path) db.conn.close() print(f"Exported sessions to {file_path}") except Exception as e: diff --git a/flomo/tracker.py b/flomo/tracker.py index b367af4..c8498d9 100644 --- a/flomo/tracker.py +++ b/flomo/tracker.py @@ -2,6 +2,7 @@ import datetime import sqlite3 from typing import Tuple +import csv from rich.console import Console from rich.table import Table @@ -107,7 +108,24 @@ def update_session(self, session_id: str, tag: str | None, name: str | None): ) print(f'Name updated to "{name}" for session {session_id}') self.conn.commit() + + def export(self, file_path: str): + """ + Exports all sessions in the database to a CSV file. + Args: + file_path (str): The path to save the CSV file. + """ + # Fetch all sessions from the database + sessions = self.get_sessions() + # Define the column headers + headers = ["id", "date_time", "tag", "name", "total_time"] + + # Write to CSV + with open(file_path, mode="w", newline="", encoding="utf-8") as csv_file: + writer = csv.writer(csv_file) + writer.writerow(headers) # Write headers + writer.writerows(sessions) # Write session rows def end_session(session_id: str): db = Tracker() @@ -118,7 +136,6 @@ def end_session(session_id: str): def show_sessions(): db = Tracker() sessions = db.get_sessions() - table = Table(title="You like this?") table.add_column("ID", style="cyan", justify="right") @@ -126,7 +143,6 @@ def show_sessions(): table.add_column("Tag", style="green") table.add_column("Name", style="yellow") table.add_column("Duration", style="red", justify="right") - for session in sessions: _id, datetime, tag, name, duration = session color = helpers.tag_color(tag) @@ -139,22 +155,5 @@ def show_sessions(): db.conn.close() -def export_to_csv(self, file_path: str): - """ - Exports all sessions in the database to a CSV file. - - Args: - file_path (str): The path to save the CSV file. - """ - # Fetch all sessions from the database - sessions = self.get_sessions() - # Define the column headers - headers = ["id", "date_time", "tag", "name", "total_time"] - - # Write to CSV - with open(file_path, mode="w", newline="", encoding="utf-8") as csv_file: - writer = csv.writer(csv_file) - writer.writerow(headers) # Write headers - writer.writerows(sessions) # Write session rows