2017-10-13 17:34:33 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-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
|
|
|
|
# Version: 0.1
|
|
|
|
# Description: A very simple python script that can sort items alphabetically.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import os
|
|
|
|
import shutil
|
2018-04-19 17:51:04 +02:00
|
|
|
import re
|
2017-10-13 17:34:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
README_FILE = '../README_es-ES.md'
|
|
|
|
TEMP_FILE = 'temp_es-ES.md'
|
|
|
|
|
|
|
|
# only works for those items between BEGIN and END.
|
|
|
|
BEGIN = '## Aplicaciones'
|
|
|
|
END = '## Configurar'
|
|
|
|
|
2018-04-19 17:51:04 +02:00
|
|
|
regex = re.compile(r"[^[]*\[([^]]*)\]")
|
2017-10-13 17:34:33 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
global README_FILE
|
|
|
|
|
|
|
|
# make sure the script can find file: README.md
|
|
|
|
README_FILE = os.path.abspath(README_FILE)
|
|
|
|
|
|
|
|
if not os.path.exists(README_FILE):
|
|
|
|
print('Error: archivo o directorio no existe: {}'.format(README_FILE))
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
sort_enable = False
|
|
|
|
items = list()
|
|
|
|
|
|
|
|
print('cargando archivo: {}'.format(README_FILE))
|
|
|
|
|
|
|
|
# read file: README.md
|
|
|
|
with open(README_FILE) as infile, open(TEMP_FILE, 'w') as outfile:
|
|
|
|
# process each line
|
|
|
|
for line in infile:
|
|
|
|
if not sort_enable and BEGIN in line:
|
|
|
|
sort_enable = True
|
|
|
|
|
|
|
|
# if sort_enable and END in line:
|
|
|
|
# sort_enable = False
|
|
|
|
|
|
|
|
if sort_enable:
|
2018-04-19 17:51:04 +02:00
|
|
|
# each item starts with a character '-'
|
|
|
|
if line.startswith(('-')):
|
|
|
|
line = line.strip()
|
2017-10-13 17:34:33 +02:00
|
|
|
items.append(line)
|
2018-04-19 17:51:04 +02:00
|
|
|
# When no more items, blank line or new header
|
2019-06-22 20:53:28 +02:00
|
|
|
elif line == '\n':
|
2017-10-13 17:34:33 +02:00
|
|
|
# when we meet the next header, we should stop adding new item to the list.
|
2018-04-19 17:51:04 +02:00
|
|
|
for item in sorted(items, key=lambda x: regex.findall(x.upper())[len(regex.findall(x.upper()))-1]):
|
2017-10-13 17:34:33 +02:00
|
|
|
# write the ordered list to the temporary file.
|
|
|
|
print(item, file=outfile)
|
|
|
|
items.clear()
|
|
|
|
|
|
|
|
# remember to put the next header in the temporary file.
|
2018-04-19 17:51:04 +02:00
|
|
|
print(line, end='', file=outfile)
|
|
|
|
elif line.startswith('#'):
|
|
|
|
sort_enable = False if END in line else True
|
|
|
|
print(line, end='', file=outfile)
|
2017-10-13 17:34:33 +02:00
|
|
|
else:
|
|
|
|
print(line, end='', file=outfile)
|
|
|
|
else:
|
|
|
|
print(line, end='', file=outfile)
|
|
|
|
|
|
|
|
print('reemplazar el archivo original: README_es-ES.md')
|
|
|
|
shutil.move(TEMP_FILE, README_FILE)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2018-04-19 17:51:04 +02:00
|
|
|
|