138 lines
4.4 KiB
Python
138 lines
4.4 KiB
Python
import re
|
|
import sys
|
|
|
|
def generate_progress_bar(total, current, length=20):
|
|
if total == 0:
|
|
percent = 0
|
|
else:
|
|
percent = (current / total) * 100
|
|
|
|
filled_length = int(length * current // total) if total > 0 else 0
|
|
bar = '▓' * filled_length + '░' * (length - filled_length)
|
|
return f"> **Status:** `{bar}` **{int(percent)}%** ({current}/{total})"
|
|
|
|
def update_readme(file_path):
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
lines = f.readlines()
|
|
|
|
# Pass 1: Analyze sections
|
|
sections = []
|
|
current_section = None
|
|
global_total = 0
|
|
global_done = 0
|
|
|
|
# Store line indices for sections to insert bars later
|
|
# Structure: {'line_index': int, 'total': int, 'done': int, 'is_global': bool}
|
|
|
|
# First, let's identify section headers and count tasks
|
|
# We will reconstruct the file content dynamically
|
|
|
|
new_lines = []
|
|
|
|
# Global stats
|
|
total_tasks = 0
|
|
done_tasks = 0
|
|
|
|
# Temporary buffer to hold section content so we can count before writing the header
|
|
# This approach is tricky because we need to insert the bar *after* the header
|
|
# simpler approach: Read all, parse structure, then process.
|
|
|
|
# Let's map sections: (start_line, end_line, title, tasks_total, tasks_done)
|
|
|
|
section_map = []
|
|
current_header_index = -1
|
|
|
|
# Logic moved to second pass
|
|
|
|
|
|
# Refined Logic:
|
|
# 1. Parse file into blocks.
|
|
# 2. Check checkboxes in blocks.
|
|
# 3. Reassemble.
|
|
|
|
output_lines = []
|
|
|
|
# Regex for progress bar to remove existing ones
|
|
progress_regex = re.compile(r'^>\s*\*\*Status:\*\*\s*`[▓░]+`\s*\*\*\d+%\*\*.*$')
|
|
|
|
i = 0
|
|
while i < len(lines):
|
|
line = lines[i]
|
|
|
|
# Skip existing progress bars
|
|
if progress_regex.match(line.strip()):
|
|
i += 1
|
|
continue
|
|
|
|
output_lines.append(line)
|
|
i += 1
|
|
|
|
# Now we have clean lines without progress bars. Let's process them to add bars.
|
|
final_lines = []
|
|
|
|
# We need to do a pass to count EVERYTHING first for the Global Bar
|
|
for line in output_lines:
|
|
if re.search(r'^\s*-\s*\[ \]', line):
|
|
total_tasks += 1
|
|
elif re.search(r'^\s*-\s*\[x\]', line, re.IGNORECASE):
|
|
total_tasks += 1
|
|
done_tasks += 1
|
|
|
|
# Insert Global Bar after the main title (assuming first H1 or similar)
|
|
# Finding "Quadro de Status" header
|
|
inserted_global = False
|
|
|
|
# Process sections
|
|
curr_section_total = 0
|
|
curr_section_done = 0
|
|
section_start_index = -1
|
|
|
|
# We need to know counts PER SECTION.
|
|
# Let's iterate and build a dict of section_index -> counts
|
|
# But checking lines again is inefficient? No, it's fast enough.
|
|
|
|
section_stats = {}
|
|
current_sec_idx = -1
|
|
|
|
for idx, line in enumerate(output_lines):
|
|
if line.strip().startswith('### '):
|
|
current_sec_idx = idx
|
|
section_stats[current_sec_idx] = {'total': 0, 'done': 0}
|
|
|
|
if current_sec_idx != -1:
|
|
if re.search(r'^\s*-\s*\[ \]', line):
|
|
section_stats[current_sec_idx]['total'] += 1
|
|
elif re.search(r'^\s*-\s*\[x\]', line, re.IGNORECASE):
|
|
section_stats[current_sec_idx]['total'] += 1
|
|
section_stats[current_sec_idx]['done'] += 1
|
|
|
|
# Now Write
|
|
for idx, line in enumerate(output_lines):
|
|
final_lines.append(line)
|
|
|
|
# Global Bar insertion
|
|
if "## 📊 Quadro de Status dos Manuais" in line and not inserted_global:
|
|
bar = generate_progress_bar(total_tasks, done_tasks, length=30)
|
|
final_lines.append(f"\n{bar}\n")
|
|
inserted_global = True
|
|
|
|
# Section Bar insertion
|
|
if idx in section_stats:
|
|
stats = section_stats[idx]
|
|
if stats['total'] > 0: # Only show if there are tasks
|
|
bar = generate_progress_bar(stats['total'], stats['done'])
|
|
final_lines.append(f"{bar}\n")
|
|
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
f.writelines(final_lines)
|
|
|
|
print(f"Updated README.md with progress bars. Global: {done_tasks}/{total_tasks}")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) > 1:
|
|
target_file = sys.argv[1]
|
|
else:
|
|
target_file = r"c:\Users\joao.goncalves\Desktop\manuais zammad\README.md"
|
|
|
|
update_readme(target_file)
|