cosmoguard-bd/utils/changelog.py

140 lines
No EOL
4.2 KiB
Python

#!/usr/bin/env python3
"""
Change Log Manager
Manages a change.log file with external and internal changes
"""
import os
from datetime import datetime
from enum import Enum
class ChangeType(Enum):
EXTERNAL = "EXTERNAL"
INTERNAL = "INTERNAL"
class ChangeLogManager:
def __init__(self, log_file="change.log"):
self.log_file = log_file
self._ensure_log_exists()
def _ensure_log_exists(self):
"""Create the log file if it doesn't exist"""
if not os.path.exists(self.log_file):
with open(self.log_file, 'w') as f:
f.write("# Change Log\n")
f.write(f"# Created: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
def add_change(self, change_type, description):
"""
Add a change entry to the log
Args:
change_type (ChangeType): Type of change (EXTERNAL or INTERNAL)
description (str): Description of the change
"""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
entry = f"[{timestamp}] [{change_type.value}] {description}\n"
with open(self.log_file, 'a') as f:
f.write(entry)
print(f"✓ Change added: {change_type.value} - {description}")
def view_log(self, filter_type=None):
"""
View the change log with optional filtering
Args:
filter_type (ChangeType, optional): Filter by change type
"""
if not os.path.exists(self.log_file):
print("No change log found.")
return
with open(self.log_file, 'r') as f:
lines = f.readlines()
print("\n" + "="*70)
print("CHANGE LOG")
print("="*70 + "\n")
for line in lines:
if filter_type and f"[{filter_type.value}]" not in line:
continue
print(line, end='')
print("\n" + "="*70 + "\n")
def get_statistics(self):
"""Display statistics about the change log"""
if not os.path.exists(self.log_file):
print("No change log found.")
return
with open(self.log_file, 'r') as f:
lines = f.readlines()
external_count = sum(1 for line in lines if "[EXTERNAL]" in line)
internal_count = sum(1 for line in lines if "[INTERNAL]" in line)
total = external_count + internal_count
print("\n" + "="*40)
print("CHANGE LOG STATISTICS")
print("="*40)
print(f"Total changes: {total}")
print(f"External changes: {external_count}")
print(f"Internal changes: {internal_count}")
print("="*40 + "\n")
def main():
manager = ChangeLogManager()
while True:
print("\n📝 Change Log Manager")
print("1. Add External Change")
print("2. Add Internal Change")
print("3. View All Changes")
print("4. View External Changes Only")
print("5. View Internal Changes Only")
print("6. Show Statistics")
print("7. Exit")
choice = input("\nSelect an option (1-7): ").strip()
if choice == '1':
description = input("Enter external change description: ").strip()
if description:
manager.add_change(ChangeType.EXTERNAL, description)
else:
print("Description cannot be empty.")
elif choice == '2':
description = input("Enter internal change description: ").strip()
if description:
manager.add_change(ChangeType.INTERNAL, description)
else:
print("Description cannot be empty.")
elif choice == '3':
manager.view_log()
elif choice == '4':
manager.view_log(filter_type=ChangeType.EXTERNAL)
elif choice == '5':
manager.view_log(filter_type=ChangeType.INTERNAL)
elif choice == '6':
manager.get_statistics()
elif choice == '7':
print("Goodbye!")
break
else:
print("Invalid option. Please select 1-7.")
if __name__ == "__main__":
main()