2017-10-13 11:34:33 -04:00
|
|
|
#!/usr/bin/env python3
|
2023-06-25 16:24:54 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: Christopher L
|
|
|
|
# Blog: http://blog.chriscabin.com
|
|
|
|
# GitHub: https://www.github.com/chrisleegit
|
|
|
|
# File: asort.py
|
|
|
|
# Date: 2016/08/22 11:12
|
2024-01-13 01:19:20 -05:00
|
|
|
# Version: 0.2
|
|
|
|
# Description: A python script to sort items alphabetically.
|
2017-10-13 11:34:33 -04:00
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
2018-04-19 11:51:04 -04:00
|
|
|
import re
|
2017-10-13 11:34:33 -04:00
|
|
|
|
|
|
|
README_FILE = '../README_es-ES.md'
|
|
|
|
TEMP_FILE = 'temp_es-ES.md'
|
|
|
|
|
2023-06-25 16:24:54 -04:00
|
|
|
# Only works for items between BEGIN and END.
|
2017-10-13 11:34:33 -04:00
|
|
|
BEGIN = '## Aplicaciones'
|
|
|
|
END = '## Configurar'
|
|
|
|
|
2018-04-19 11:51:04 -04:00
|
|
|
regex = re.compile(r"[^[]*\[([^]]*)\]")
|
2017-10-13 11:34:33 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
global README_FILE
|
|
|
|
|
2023-06-25 16:24:54 -04:00
|
|
|
# Make sure the script can find the file: README.md
|
2017-10-13 11:34:33 -04:00
|
|
|
README_FILE = os.path.abspath(README_FILE)
|
|
|
|
|
|
|
|
if not os.path.exists(README_FILE):
|
2023-09-06 13:12:38 -04:00
|
|
|
print(f'Error: archivo o directorio no existe: {README_FILE}')
|
2017-10-13 11:34:33 -04:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
sort_enable = False
|
2023-06-25 16:24:54 -04:00
|
|
|
items = []
|
2017-10-13 11:34:33 -04:00
|
|
|
|
2023-09-06 13:12:38 -04:00
|
|
|
print(f'Cargando archivo: {README_FILE}')
|
2017-10-13 11:34:33 -04:00
|
|
|
|
2023-06-25 16:24:54 -04:00
|
|
|
# Read the file: README.md
|
2024-01-13 01:19:20 -05:00
|
|
|
with open(README_FILE, 'r') as infile, open(TEMP_FILE, 'w') as outfile:
|
2023-06-25 16:24:54 -04:00
|
|
|
# Process each line
|
2017-10-13 11:34:33 -04:00
|
|
|
for line in infile:
|
|
|
|
if not sort_enable and BEGIN in line:
|
|
|
|
sort_enable = True
|
|
|
|
|
2023-09-06 13:12:38 -04:00
|
|
|
if sort_enable and line.startswith('-'):
|
|
|
|
line = line.strip()
|
|
|
|
items.append(line)
|
|
|
|
elif sort_enable and not line.startswith('-') and line == '\n':
|
|
|
|
# When we meet the next header, we should stop adding new items to the list.
|
|
|
|
for item in sorted(items, key=lambda x: regex.findall(x.upper())[-1]):
|
|
|
|
# Write the ordered list to the temporary file.
|
|
|
|
print(item, file=outfile)
|
|
|
|
items.clear()
|
2017-10-13 11:34:33 -04:00
|
|
|
|
2023-09-06 13:12:38 -04:00
|
|
|
# Remember to put the next header in the temporary file.
|
|
|
|
print(line, end='', file=outfile)
|
|
|
|
elif (
|
|
|
|
sort_enable
|
|
|
|
and not line.startswith('-')
|
|
|
|
and line != '\n'
|
|
|
|
and line.startswith('#')
|
|
|
|
):
|
|
|
|
sort_enable = END not in line
|
|
|
|
print(line, end='', file=outfile)
|
2017-10-13 11:34:33 -04:00
|
|
|
else:
|
|
|
|
print(line, end='', file=outfile)
|
2024-01-13 01:19:20 -05:00
|
|
|
|
2023-06-25 16:24:54 -04:00
|
|
|
print('Reemplazar el archivo original: README_es-ES.md')
|
2017-10-13 11:34:33 -04:00
|
|
|
shutil.move(TEMP_FILE, README_FILE)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|