Awesome-Linux-Software/auto_sort/asort_es-ES.py
Mohammad Noman Kazi 5407039cee Updated code formatting and readability, adhering to PEP 8 guidelines.
This commit includes modifications to the code in asort.py to improve code formatting and readability according to the PEP 8 style guidelines. The changes include consistent indentation, proper spacing around operators, proper capitalization of comments, and using parentheses for print statements for compatibility with Python 3.

Please note that these changes are purely stylistic and do not affect the functionality of the script. The goal is to enhance code maintainability and readability.
2023-06-25 16:24:54 -04:00

75 lines
2.3 KiB
Python

#!/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.
import os
import shutil
import re
README_FILE = '../README_es-ES.md'
TEMP_FILE = 'temp_es-ES.md'
# Only works for items between BEGIN and END.
BEGIN = '## Aplicaciones'
END = '## Configurar'
regex = re.compile(r"[^[]*\[([^]]*)\]")
def main():
global README_FILE
# Make sure the script can find the 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 = []
print('Cargando archivo: {}'.format(README_FILE))
# Read the file: README.md
with open(README_FILE, 'r') 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:
# Each item starts with a character '-'
if line.startswith('-'):
line = line.strip()
items.append(line)
# When no more items, blank line or new header
elif 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()
# Remember to put the next header in the temporary file.
print(line, end='', file=outfile)
elif line.startswith('#'):
sort_enable = False if END in line else True
print(line, end='', file=outfile)
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()