Created
October 1, 2024 20:05
-
-
Save alvarmaciel/72c727da780b32ba9c4aecaf21415b23 to your computer and use it in GitHub Desktop.
rename postman dumped collection
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
""" | |
Usage: | |
rename_postman_collections.py <input_dir> [--output-dir=<output_dir>] | |
Options: | |
<input_dir> Path to the Postman collection dir. | |
--output-dir=<output_dir> Path to save the renamed collection file [default: renamed]. | |
""" | |
import os | |
import json | |
import re | |
from docopt import docopt | |
def sanitize_filename(name): | |
return re.sub(r'\W+', '_', name) | |
def rename_collections(input_dir, output_dir='renamed'): | |
if not os.path.exists(output_dir): | |
os.makedirs(output_dir) | |
for file in os.listdir(input_dir): | |
file_path = os.path.join(input_dir, file) | |
print(f"Procesando archivo: {file_path}") | |
with open(file_path) as f: | |
data = json.load(f) | |
collection_name = data['info']['name'] | |
sanitized_name = sanitize_filename(collection_name) | |
print(f"Nombre de la colección: {sanitized_name}") | |
output_file = os.path.join(output_dir, f"{sanitized_name}.json") | |
with open(output_file, "w") as f: | |
json.dump(data, f, indent=4) | |
def main(): | |
arguments = docopt(__doc__) | |
input_dir = arguments['<input_dir>'] | |
output_dir = arguments['--output-dir'] or 'renamed' | |
rename_collections(input_dir, output_dir) | |
if __name__ == "__main__": | |
try: | |
main() | |
print("Script ejecutado exitosamente.") | |
except Exception as e: | |
print(f"Algo falló: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment