Created
November 17, 2024 16:56
-
-
Save danielscholl/1265f8a0e0eed8281cb0f8e0a6f1d187 to your computer and use it in GitHub Desktop.
Retrieve specific bicep modules for AI use.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
def extract_bicep_lines(content): | |
# Extract lines that match the pattern '- **bicep' | |
bicep_lines = [] | |
for line in content.splitlines(): | |
if line.startswith('- **bicep'): | |
bicep_lines.append(line) | |
# Print each extracted Bicep line with its line number | |
for index, bicep_line in enumerate(bicep_lines, start=1): | |
print(f"{index}: {bicep_line}") | |
return bicep_lines | |
def fetch_markdown_blocks(content, bicep_lines, identifier, selected_blocks): | |
print() # Add newline after input before showing results | |
# Check if the identifier is a number (line number) | |
if identifier.isdigit(): | |
filtered_index = int(identifier) - 1 # Convert to zero-based index | |
if hasattr(fetch_markdown_blocks, 'current_filtered_lines'): | |
# Use the stored filtered lines | |
if 0 <= filtered_index < len(fetch_markdown_blocks.current_filtered_lines): | |
line = fetch_markdown_blocks.current_filtered_lines[filtered_index] | |
else: | |
print(f"Line number {identifier} is out of range.") | |
return | |
else: | |
# Use the original list if no filtering has been done | |
if 0 <= filtered_index < len(bicep_lines): | |
line = bicep_lines[filtered_index] | |
else: | |
print(f"Line number {identifier} is out of range.") | |
return | |
# Extract the module name from the line | |
module_name = re.search(r'\*\*(.*?)\*\*', line).group(1).replace('/', '_').replace(' ', '_') | |
# Find the full section in the content | |
pattern = rf'({re.escape(line)}.*?```bicep.*?```)' | |
match = re.search(pattern, content, re.DOTALL) | |
if match: | |
# Store the full block (including header and code) | |
selected_blocks[module_name] = match.group(1) | |
print(f"Block for {module_name} has been selected for writing") | |
else: | |
print(f"No full match found for line: {line}") | |
else: | |
# Filter lines that match the search text | |
matching_lines = [line for line in bicep_lines if identifier.lower() in line.lower()] | |
# Store the filtered lines for later use | |
fetch_markdown_blocks.current_filtered_lines = matching_lines | |
print(f"Matching Lines for '{identifier}':") | |
if matching_lines: | |
for index, line in enumerate(matching_lines, start=1): | |
print(f"{index}: {line}") | |
else: | |
print(f"No matches found for '{identifier}'") | |
def write_selected_blocks(selected_blocks): | |
if not selected_blocks: | |
print("No blocks were selected.") | |
return | |
# Write all selected blocks to a single file | |
with open("modules.md", 'w') as file: | |
for module_name, block in selected_blocks.items(): | |
file.write(f"{block}\n\n") | |
print("All selected blocks have been written to modules.md") | |
# Main loop for user input | |
if __name__ == "__main__": | |
# Fetch the content from the URL once at startup | |
url = "https://gist.githubusercontent.com/danielscholl/1b0028e9af9f5304c4f6e0cea064c3e2/raw/cce2e41a1f32a84a1a6c129344145035b110c62b/modules.md" | |
print(f"Retrieving data from {url}...") | |
response = requests.get(url) | |
content = response.text | |
# Extract lines that match the pattern '- **bicep' | |
bicep_lines = extract_bicep_lines(content) | |
# Dictionary to store selected blocks | |
selected_blocks = {} | |
while True: | |
identifier = input("Enter a line number to retrieve the module or text to filter (or press Enter to quit): ").strip() | |
if not identifier: # Exit if input is empty | |
print("\nWriting selected blocks to file...") | |
write_selected_blocks(selected_blocks) | |
print("Exiting the program.") | |
break | |
fetch_markdown_blocks(content, bicep_lines, identifier, selected_blocks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment