Skip to content

Commit

Permalink
add ignore directories and prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cainky committed Aug 6, 2024
1 parent 29b7e64 commit 8ec0cc7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This project replaces text in files based on a dictionary, given user input to s

- Replace text in files based on dictionaries defined in a configuration file.
- Allows user to specify the direction of replacement (keys-to-values or values-to-keys).
- Allows user to specify which file extensions to ignore.
- Allows user to specify which file extensions, file prefixes, or directories to ignore.
- Automatically uses the only dictionary if only one is defined in the configuration file.

## Requirements
Expand Down Expand Up @@ -59,7 +59,9 @@ This project replaces text in files based on a dictionary, given user input to s
"python": "rocks"
}
},
"ignore_extensions": [".png", ".jpg", ".gif"]
"ignore_extensions": [".exe", ".dll", ".bin"],
"ignore_directories": ["node_modules", "venv", ".git"],
"ignore_file_prefixes": [".", "_"]
}
```

Expand Down
4 changes: 3 additions & 1 deletion example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
"python": "rocks"
}
},
"ignore_extensions": [".png", ".jpg", ".gif"]
"ignore_extensions": [".exe", ".dll", ".bin"],
"ignore_directories": ["node_modules", "venv", ".git"],
"ignore_file_prefixes": [".", "_"]
}
37 changes: 27 additions & 10 deletions replace_text/replace_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ def replace_text(direction: int, folder: str, dict_name: str) -> None:
folder (str): Path to the folder containing text files.
dict_name (str): Name of the dictionary to use from config.json.
"""
# Load dictionaries and ignore extensions from config file
# Load dictionaries and configuration from config file
with open("config.json", "r") as config_file:
config = json.load(config_file)

# Retrieve the dictionaries and ignore extensions
# Retrieve the dictionaries and configuration options
dictionaries = config.get("dictionaries", {})
ignore_extensions = config.get("ignore_extensions", [])
ignore_directories = config.get("ignore_directories", [])
ignore_file_prefixes = config.get("ignore_file_prefixes", [])

if not dictionaries:
print("No dictionaries found in config.json")
Expand All @@ -59,22 +61,37 @@ def replace_text(direction: int, folder: str, dict_name: str) -> None:
replacement_dict = {v: k for k, v in replacement_dict.items()}

# Process each file in the folder
for root, _, files in os.walk(folder):
for root, dirs, files in os.walk(folder):
# Remove ignored directories from the dirs list
dirs[:] = [d for d in dirs if d not in ignore_directories]

for file in files:
file_path = os.path.join(root, file)

# Skip files with ignored extensions
if any(file.endswith(ext) for ext in ignore_extensions):
print(f"Skipped file (ignored extension): {file_path}")
continue
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()

for key, value in replacement_dict.items():
content = content.replace(key, value)
# Skip files with ignored prefixes
if any(file.startswith(prefix) for prefix in ignore_file_prefixes):
print(f"Skipped file (ignored prefix): {file_path}")
continue

with open(file_path, "w", encoding="utf-8") as f:
f.write(content)
print(f"Processing file: {file}")
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
for key, value in replacement_dict.items():
content = content.replace(key, value)

print(f"Processed file: {file_path}")
with open(file_path, "w", encoding="utf-8") as f:
f.write(content)

print(f"Processed file: {file_path}")
except Exception as e:
print(f"Error processing file: {file}, continuing..")
continue


if __name__ == "__main__":
Expand Down

0 comments on commit 8ec0cc7

Please sign in to comment.