Skip to content

Commit 45504ad

Browse files
author
Agent VM
committed
Convert _format_bytes to staticmethod
Add @staticmethod decorator to _format_bytes and update all call sites to use class-based invocation. This method does not use instance state and was already being called statically in cmd_list, making this change more consistent with actual usage. Changes: - Add @staticmethod decorator - Remove self parameter from method signature - Update all calls from self._format_bytes() or backup._format_bytes() to OpenSearchBackup._format_bytes() Addresses review feedback on PR #23.
1 parent fc751b1 commit 45504ad

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

scripts/opensearch_backup.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def get_index_stats(self, index: str) -> Dict[str, Any]:
175175
size_bytes = store.get('size_in_bytes', 0)
176176

177177
# Calculate human-readable size
178-
size_human = self._format_bytes(size_bytes)
178+
size_human = OpenSearchBackup._format_bytes(size_bytes)
179179

180180
return {
181181
'doc_count': doc_count,
@@ -186,7 +186,8 @@ def get_index_stats(self, index: str) -> Dict[str, Any]:
186186
self.logger.error(f"Failed to get stats for {index}: {e}")
187187
raise
188188

189-
def _format_bytes(self, size_bytes: int) -> str:
189+
@staticmethod
190+
def _format_bytes(size_bytes: int) -> str:
190191
"""Format bytes to human-readable size."""
191192
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
192193
if size_bytes < 1024.0:
@@ -287,7 +288,7 @@ def backup_index(
287288

288289
file_size = output_path.stat().st_size
289290
self.logger.info(f"✓ Backup complete: {docs_written:,} documents written to {output_path}")
290-
self.logger.info(f" Backup file size: {self._format_bytes(file_size)}")
291+
self.logger.info(f" Backup file size: {OpenSearchBackup._format_bytes(file_size)}")
291292

292293
return {
293294
'index': index,
@@ -542,8 +543,8 @@ def cmd_backup(args):
542543

543544
print("\n" + "-" * 70)
544545
print(f"Total documents: {total_docs:,}")
545-
print(f"Total index size: {backup._format_bytes(total_size)}")
546-
print(f"Estimated backup size: {backup._format_bytes(int(estimated_size))}")
546+
print(f"Total index size: {OpenSearchBackup._format_bytes(total_size)}")
547+
print(f"Estimated backup size: {OpenSearchBackup._format_bytes(int(estimated_size))}")
547548
if args.compress:
548549
print(" (with gzip compression)")
549550
print("=" * 70 + "\n")
@@ -582,7 +583,7 @@ def cmd_backup(args):
582583
print(f"\nIndex: {result['index']}")
583584
print(f" Documents: {result['documents']:,}")
584585
print(f" File: {result['file_path']}")
585-
print(f" Size: {backup._format_bytes(result['file_size'])}")
586+
print(f" Size: {OpenSearchBackup._format_bytes(result['file_size'])}")
586587
print("=" * 70 + "\n")
587588

588589
return 0
@@ -626,7 +627,7 @@ def cmd_restore(args):
626627
print("=" * 70)
627628
print(f"\nSource file: {input_path}")
628629
print(f"Target index: {target_index}")
629-
print(f"File size: {backup._format_bytes(input_path.stat().st_size)}")
630+
print(f"File size: {OpenSearchBackup._format_bytes(input_path.stat().st_size)}")
630631
print("\n" + "=" * 70 + "\n")
631632

632633
if not confirm_action("Proceed with restore?"):
@@ -679,7 +680,7 @@ def cmd_list(args):
679680
for backup_file in backups:
680681
size = backup_file.stat().st_size
681682
# Format size
682-
size_str = OpenSearchBackup._format_bytes(None, size)
683+
size_str = OpenSearchBackup._format_bytes(size)
683684
print(f"{backup_file.name}")
684685
print(f" Size: {size_str}")
685686
print(f" Path: {backup_file}")

0 commit comments

Comments
 (0)