-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
158 lines (123 loc) · 4.96 KB
/
example_usage.py
File metadata and controls
158 lines (123 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
"""
Example usage of flatten_directory.py
This script demonstrates how to use the flatten directory functionality programmatically.
"""
import sys
import os
from pathlib import Path
# Add the current directory to the path so we can import our module
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Import the functions from our flatten_directory module
from flatten_directory import collect_files, generate_output_content
def example_basic_usage():
"""Example of basic usage."""
print("=== Basic Usage Example ===")
# Get the test project directory
test_dir = Path("test_project")
if not test_dir.exists():
print("Test project directory not found. Please run the basic test first.")
return
# Collect all files
files = collect_files(test_dir)
print(f"Found {len(files)} files in {test_dir}")
# Generate the flattened content
content = generate_output_content(files, test_dir.name)
# Write to a file
output_file = "example_output.txt"
with open(output_file, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Flattened content written to: {output_file}")
print(f"Content length: {len(content)} characters")
print()
def example_filtered_usage():
"""Example of filtered usage."""
print("=== Filtered Usage Example ===")
# Get the test project directory
test_dir = Path("test_project")
if not test_dir.exists():
print("Test project directory not found. Please run the basic test first.")
return
# Collect all files
files = collect_files(test_dir)
# Filter to only include Python files
python_files = [f for f in files if f['extension'] == '.py']
print(f"Found {len(python_files)} Python files in {test_dir}")
# Generate the flattened content
content = generate_output_content(python_files, f"{test_dir.name}_python_only")
# Write to a file
output_file = "python_only_output.txt"
with open(output_file, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Python-only content written to: {output_file}")
print(f"Content length: {len(content)} characters")
print()
def example_custom_format():
"""Example of custom output format."""
print("=== Custom Format Example ===")
# Get the test project directory
test_dir = Path("test_project")
if not test_dir.exists():
print("Test project directory not found. Please run the basic test first.")
return
# Collect all files
files = collect_files(test_dir)
# Create a custom format
custom_content = []
custom_content.append("# Custom Flattened Directory Report")
custom_content.append(f"Directory: {test_dir.name}")
custom_content.append(f"Total Files: {len(files)}")
custom_content.append("")
# Add file summary
custom_content.append("## File Summary")
total_size = 0
for file_info in files:
size = file_info['size']
total_size += size
size_str = f"{size} bytes" if size < 1024 else f"{size // 1024} KB"
custom_content.append(f"- {file_info['path']} ({size_str})")
custom_content.append(f"")
custom_content.append(f"Total Size: {total_size} bytes ({total_size // 1024} KB)")
custom_content.append("")
# Add file contents with custom formatting
custom_content.append("## File Contents")
for i, file_info in enumerate(files, 1):
custom_content.append(f"### {i}. {file_info['path']}")
custom_content.append(f"**Size:** {file_info['size']} bytes")
custom_content.append(f"**Type:** {file_info['extension']}")
custom_content.append("")
custom_content.append("```")
custom_content.append(file_info['content'])
custom_content.append("```")
custom_content.append("")
# Write to a file
output_file = "custom_format_output.md"
with open(output_file, 'w', encoding='utf-8') as f:
f.write("\n".join(custom_content))
print(f"Custom format content written to: {output_file}")
content_str = '\n'.join(custom_content)
print(f"Content length: {len(content_str)} characters")
print()
def main():
"""Run all examples."""
print("Flatten Directory - Example Usage")
print("=" * 40)
print()
# Check if test project exists
test_dir = Path("test_project")
if not test_dir.exists():
print("Test project directory not found.")
print("Please run the basic test first to create the test project.")
print("You can run: python3 flatten_directory.py test_project")
return
# Run examples
example_basic_usage()
example_filtered_usage()
example_custom_format()
print("All examples completed!")
print("Check the generated output files:")
print("- example_output.txt")
print("- python_only_output.txt")
print("- custom_format_output.md")
if __name__ == "__main__":
main()