|
| 1 | +import json |
| 2 | +import os |
| 3 | +from textual.widgets import DataTable |
| 4 | + |
| 5 | +from .config import DATA_FILE_PATH, load_config |
| 6 | +from .t_status_data import get_data_table, get_overview_data_table |
| 7 | + |
| 8 | +CONFIG = load_config() |
| 9 | + |
| 10 | + |
| 11 | +class StatusTable(DataTable): |
| 12 | + """A custom widget for displaying data from a dynamically updating JSON file.""" |
| 13 | + |
| 14 | + # Store the last known modification time of the file |
| 15 | + last_mtime: float = 0.0 |
| 16 | + |
| 17 | + def load_data_from_json(self) -> None: |
| 18 | + """Reads and updates the table content from the JSON file.""" |
| 19 | + try: |
| 20 | + # Check if file has been modified recently |
| 21 | + current_mtime = os.path.getmtime(DATA_FILE_PATH) |
| 22 | + if current_mtime <= self.last_mtime: |
| 23 | + return # Skip if not modified |
| 24 | + self.last_mtime = current_mtime |
| 25 | + |
| 26 | + # Get new data |
| 27 | + with open(DATA_FILE_PATH, "r") as f: |
| 28 | + try: |
| 29 | + data = json.load(f) |
| 30 | + except Exception: |
| 31 | + return |
| 32 | + if not data: |
| 33 | + return # Skip if data is empty |
| 34 | + filtered_data = get_data_table(data) |
| 35 | + |
| 36 | + # Add new data |
| 37 | + data_uids = filtered_data.keys() |
| 38 | + current_table_uids = self.rows.keys() |
| 39 | + uids_to_add = [i for i in data_uids if i not in current_table_uids] |
| 40 | + for uid in uids_to_add: |
| 41 | + self.add_row(*filtered_data[uid].values(), key=uid, label=uid) |
| 42 | + |
| 43 | + # Remove old data |
| 44 | + uids_to_remove = [ |
| 45 | + i for i in current_table_uids if i not in data_uids] |
| 46 | + for uid in uids_to_remove: |
| 47 | + self.remove_row(uid) |
| 48 | + |
| 49 | + # Edit changed data |
| 50 | + uids_to_edit = [ |
| 51 | + i for i in data_uids |
| 52 | + if i not in uids_to_add |
| 53 | + and i not in uids_to_remove |
| 54 | + ] |
| 55 | + for uid in uids_to_edit: |
| 56 | + for c_no in range(len(CONFIG.textual_columns)): |
| 57 | + self.update_cell( |
| 58 | + uid, |
| 59 | + str(c_no), |
| 60 | + filtered_data[uid][CONFIG.textual_columns[c_no]] |
| 61 | + ) |
| 62 | + |
| 63 | + except FileNotFoundError: |
| 64 | + # Optional: Display a message if the file is missing |
| 65 | + self.app.log(f"JSON file not found: {DATA_FILE_PATH}") |
| 66 | + except Exception as e: |
| 67 | + self.app.log(f"Error loading JSON data: {e}") |
| 68 | + |
| 69 | + def on_mount(self) -> None: |
| 70 | + """Called when the widget is first attached to the app.""" |
| 71 | + |
| 72 | + columns = CONFIG.textual_columns |
| 73 | + for c_no in range(len(columns)): |
| 74 | + self.add_column(columns[c_no], key=str(c_no)) |
| 75 | + |
| 76 | + self.load_data_from_json() |
| 77 | + |
| 78 | + self.set_interval(1, self.load_data_from_json) |
| 79 | + |
| 80 | + |
| 81 | +class OverviewTable(DataTable): |
| 82 | + """A custom widget for displaying data from a dynamically updating JSON file.""" |
| 83 | + |
| 84 | + # Store the last known modification time of the file |
| 85 | + last_mtime: float = 0.0 |
| 86 | + |
| 87 | + def load_data_from_json(self) -> None: |
| 88 | + """Reads and updates the table content from the JSON file.""" |
| 89 | +# try: |
| 90 | + # Check if file has been modified recently |
| 91 | + current_mtime = os.path.getmtime(DATA_FILE_PATH) |
| 92 | + if current_mtime <= self.last_mtime: |
| 93 | + return # Skip if not modified |
| 94 | + self.last_mtime = current_mtime |
| 95 | + |
| 96 | + # Get new data |
| 97 | + with open(DATA_FILE_PATH, "r") as f: |
| 98 | + try: |
| 99 | + data = json.load(f) |
| 100 | + except Exception: |
| 101 | + return |
| 102 | + if not data: |
| 103 | + return # Skip if data is empty |
| 104 | + filtered_data = get_overview_data_table(data) |
| 105 | + |
| 106 | + # Add new data |
| 107 | + data_uids = filtered_data.keys() |
| 108 | + current_table_uids = self.rows.keys() |
| 109 | + uids_to_add = [i for i in data_uids if i not in current_table_uids] |
| 110 | + for uid in uids_to_add: |
| 111 | + self.add_row(*filtered_data[uid].values(), key=uid, label=uid) |
| 112 | + |
| 113 | + # Remove old data |
| 114 | + uids_to_remove = [ |
| 115 | + i for i in current_table_uids if i not in data_uids] |
| 116 | + for uid in uids_to_remove: |
| 117 | + self.remove_row(uid) |
| 118 | + |
| 119 | + # Edit changed data |
| 120 | + uids_to_edit = [ |
| 121 | + i for i in data_uids |
| 122 | + if i not in uids_to_add |
| 123 | + and i not in uids_to_remove |
| 124 | + ] |
| 125 | + for uid in uids_to_edit: |
| 126 | + for c_no in range(len(CONFIG.textual_overview_columns)): |
| 127 | + self.update_cell( |
| 128 | + uid, |
| 129 | + str(c_no), |
| 130 | + filtered_data[uid][ |
| 131 | + CONFIG.textual_overview_columns[c_no]] |
| 132 | + ) |
| 133 | + |
| 134 | +# except FileNotFoundError: |
| 135 | +# # Optional: Display a message if the file is missing |
| 136 | +# self.app.log(f"JSON file not found: {DATA_FILE_PATH}") |
| 137 | +# except Exception as e: |
| 138 | +# self.app.log(f"Error loading JSON data: {e}") |
| 139 | + |
| 140 | + def on_mount(self) -> None: |
| 141 | + """Called when the widget is first attached to the app.""" |
| 142 | + |
| 143 | + columns = CONFIG.textual_overview_columns |
| 144 | + for c_no in range(len(columns)): |
| 145 | + self.add_column(columns[c_no], key=str(c_no)) |
| 146 | + |
| 147 | + self.load_data_from_json() |
| 148 | + |
| 149 | + self.set_interval(1, self.load_data_from_json) |
0 commit comments