Skip to content

Commit

Permalink
script
Browse files Browse the repository at this point in the history
  • Loading branch information
fscelliott committed Jun 28, 2024
1 parent fb05e48 commit 7a34834
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions assets/scripts/api_files/replace_strings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import os



#python3 replace_strings.py config_internal_links_replacement.txt /home/franc/Github/test-mintlify/ --exclude_dirs /home/franc/Github/test-mintlify/assets
#python3 replace_strings.py config_internal_links_replacement.txt /home/franc/Github/test-mintlify/ --exclude_dirs /home/franc/Github/test-mintlify/assets /home/franc/Github/node_modules


# python3 replace_strings.py config_strings.txt /home/franc/Github/test-mintlify/ --exclude_dirs /home/franc/Github/test-mintlify/assets

# python replace_strings.py config.txt /path/to/search/directory --exclude_dirs /path/to/search/directory/exclude1 /path/to/search/directory/exclude2
# python replace_strings.py config.txt /path/to/search/directory --exclude_dirs /path/to/search/directory/exclude1 /path/to/search/directory/exclude2 --extensions .md

# The script will load the configuration file, recursively search the specified directory for *.mdx and *.json files, exclude specified directories from the search, and replace the specified strings within those files.

import os
import argparse

def load_config(config_file):
replace_map = {}
with open(config_file, 'r') as f:
Expand All @@ -26,30 +28,29 @@ def replace_in_file(file_path, replace_map):
for search, replace in replace_map.items():
content = content.replace(search, replace)

with open(file_path, 'w+', encoding='utf-8') as file:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)

def replace_in_files(root_dir, replace_map, exclude_dirs):
def replace_in_files(root_dir, replace_map, exclude_dirs, extensions):
for dirpath, dirnames, filenames in os.walk(root_dir):
# Remove directories that need to be excluded from the search
dirnames[:] = [d for d in dirnames if os.path.join(dirpath, d) not in exclude_dirs]

for filename in filenames:
if filename.endswith('.mdx') or filename.endswith('.json'):
if any(filename.endswith(ext) for ext in extensions):
file_path = os.path.join(dirpath, filename)
replace_in_file(file_path, replace_map)
print(f'Replaced strings in: {file_path}')

if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser(description="Recursively search and replace strings in *.mdx and *.json files based on a tab-delimited config file.")
parser = argparse.ArgumentParser(description="Recursively search and replace strings in specified file extensions based on a tab-delimited config file.")
parser.add_argument('config_file', type=str, help="Path to the tab-delimited config file.")
parser.add_argument('root_dir', type=str, help="Root directory to start the search from.")
parser.add_argument('--exclude_dirs', nargs='*', default=[], help="List of directories to exclude from the search, separated by spaces.")
parser.add_argument('--extensions', nargs='+', default=['.mdx', '.json', '.yml'], help="List of file extensions to include in the search, separated by spaces.")

args = parser.parse_args()

replace_map = load_config(args.config_file)
exclude_dirs = [os.path.abspath(d) for d in args.exclude_dirs]
replace_in_files(os.path.abspath(args.root_dir), replace_map, exclude_dirs)
replace_in_files(os.path.abspath(args.root_dir), replace_map, exclude_dirs, args.extensions)

0 comments on commit 7a34834

Please sign in to comment.