import os import fnmatch from pathlib import Path def should_skip_file(filepath, filename): """Check if file should be skipped based on patterns""" skip_patterns = [ '__pycache__', '*.pyc', '.env*', '*.log', '.git*', '*.tmp', '*.temp', 'node_modules', '.vscode', '.idea', '*.DS_Store', 'Thumbs.db' ] skip_dirs = [ '__pycache__', '.git', 'node_modules', '.vscode', '.idea', 'venv', 'env', '.env' ] # Check if any parent directory should be skipped path_parts = Path(filepath).parts for part in path_parts: if part in skip_dirs: return True # Check if filename matches skip patterns for pattern in skip_patterns: if fnmatch.fnmatch(filename, pattern): return True return False def get_file_content(filepath): """Read file content safely""" try: with open(filepath, 'r', encoding='utf-8') as file: return file.read() except UnicodeDecodeError: try: with open(filepath, 'r', encoding='latin-1') as file: return file.read() except Exception as e: return f"Error reading file: {str(e)}" except Exception as e: return f"Error reading file: {str(e)}" def generate_documentation(root_path='.', output_file='PROJECT_DOCUMENTATION.md'): """Generate complete project documentation""" documentation = [] documentation.append("# Project Documentation\n") documentation.append(f"Generated from: {os.path.abspath(root_path)}\n") documentation.append("---\n") # Get all Python files python_files = [] for root, dirs, files in os.walk(root_path): # Remove directories that should be skipped dirs[:] = [d for d in dirs if not should_skip_file(os.path.join(root, d), d)] for file in files: filepath = os.path.join(root, file) # Skip system files and focus on Python files if should_skip_file(filepath, file): continue # Only include Python files and important config files if file.endswith('.py') or file in ['requirements.txt', 'README.md', '.env.example']: python_files.append(filepath) # Sort files for consistent output python_files.sort() # Generate documentation for each file for filepath in python_files: relative_path = os.path.relpath(filepath, root_path) # Add file header documentation.append(f"## {relative_path}\n") # Get file content content = get_file_content(filepath) # Add code block with syntax highlighting if filepath.endswith('.py'): documentation.append("```python") elif filepath.endswith('.md'): documentation.append("```markdown") elif filepath.endswith('.txt'): documentation.append("```text") else: documentation.append("```") documentation.append(content) documentation.append("```\n") documentation.append("---\n") # Write documentation to file with open(output_file, 'w', encoding='utf-8') as f: f.write('\n'.join(documentation)) print(f"Documentation generated successfully: {output_file}") print(f"Total files documented: {len(python_files)}") def main(): """Main function to run the documentation generator""" # You can modify these paths as needed project_root = "." # Current directory output_filename = "PROJECT_DOCUMENTATION.md" print("Starting documentation generation...") print(f"Project root: {os.path.abspath(project_root)}") print(f"Output file: {output_filename}") print("=" * 50) generate_documentation(project_root, output_filename) print("=" * 50) print("Documentation generation completed!") if __name__ == "__main__": main()