You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CodeMap/src/readme_generator.py

142 lines
4.5 KiB

import os
from datetime import datetime
class ReadmeGenerator:
def __init__(self):
self.template = '''<h1 align="center" id="title">{title}</h1>
<p id="description">{description}</p>
## 🛠️ Prérequis
{prerequisites}
## 🚀 Installation
{installation_steps}
## 💻 Technologies Utilisées
{technologies}
## 👥 Contributeurs
{authors}
## 📝 Licence
Ce projet est distribué sous la licence {license}.
'''
def analyze_project(self, project_path):
project_info = {}
project_info['title'] = os.path.basename(project_path)
description = ""
readme_files = ['README.md', 'README.txt', 'README']
for readme in readme_files:
readme_path = os.path.join(project_path, readme)
if os.path.exists(readme_path):
with open(readme_path, 'r', encoding='utf-8') as f:
content = f.read()
import re
desc_match = re.search(r'description[>\n]+(.*?)\n\n', content, re.I | re.S)
if desc_match:
description = desc_match.group(1).strip()
break
if not description:
for root, _, files in os.walk(project_path):
if '.git' in root or '__pycache__' in root:
continue
for file in files:
if file.endswith('.py'):
try:
with open(os.path.join(root, file), 'r', encoding='utf-8') as f:
content = f.read()
doc_match = re.search(r'"""(.*?)"""', content, re.S)
if doc_match:
description = doc_match.group(1).strip()
break
except:
continue
if description:
break
project_info['description'] = description if description else "Description à remplir"
license_files = ['LICENSE', 'LICENSE.txt', 'LICENSE.md']
license_type = "MIT"
for license_file in license_files:
license_path = os.path.join(project_path, license_file)
if os.path.exists(license_path):
with open(license_path, 'r', encoding='utf-8') as f:
content = f.read().lower()
if 'apache' in content:
license_type = 'Apache'
elif 'gnu' in content or 'gpl' in content:
license_type = 'GPL'
elif 'mit' in content:
license_type = 'MIT'
elif 'bsd' in content:
license_type = 'BSD'
break
project_info['license'] = license_type
return project_info
def generate_readme_content(self, project_info):
prerequisites = "\n".join(project_info.get('prerequisites', []))
installation_steps = "\n\n".join(project_info.get('installation_steps', []))
technologies = "Ce projet utilise les technologies suivantes :\n\n"
technologies += "\n".join([f"* {tech}" for tech in project_info.get('technologies', [])])
authors = "\n\n".join([f"{author}" for author in project_info.get('authors', [])])
if not authors:
authors = "*Aucun contributeur listé*"
return self.template.format(
title=project_info.get('title', 'Projet Sans Titre'),
description=project_info.get('description', ''),
prerequisites=prerequisites,
installation_steps=installation_steps,
technologies=technologies,
authors=authors,
license=project_info.get('license', 'MIT')
)
def generate_readme(self, project_info, output_path):
project_analysis = self.analyze_project(output_path)
merged_info = project_analysis.copy()
merged_info.update(project_info)
content = self.generate_readme_content(merged_info)
readme_file = os.path.join(output_path, "README.md")
with open(readme_file, "w", encoding="utf-8") as f:
f.write(content)
return readme_file