mirror of
https://github.com/vinta/awesome-python.git
synced 2025-04-15 16:57:34 +00:00
Update sort.py
This commit is contained in:
parent
60734b11e8
commit
ebab050c3b
47
sort.py
47
sort.py
@ -15,35 +15,32 @@
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# First, we load the current README into memory as an array of lines
|
# First, we load the current README into memory
|
||||||
with open('README.md', 'r') as read_me_file:
|
with open('README.md', 'r') as read_me_file:
|
||||||
read_me = read_me_file.readlines()
|
read_me = read_me_file.read()
|
||||||
|
|
||||||
# Then we cluster the lines together as blocks
|
# Separating the 'table of contents' from the contents (blocks)
|
||||||
# Each block represents a collection of lines that should be sorted
|
table_of_contents = ''.join(read_me.split('- - -')[0])
|
||||||
# This was done by assuming only links ([...](...)) are meant to be sorted
|
blocks = ''.join(read_me.split('- - -')[1]).split('\n# ')
|
||||||
# Clustering is done by indentation
|
for i in range(len(blocks)):
|
||||||
blocks = []
|
if i == 0:
|
||||||
last_indent = None
|
blocks[i] = blocks[i]+'\n'
|
||||||
for line in read_me:
|
|
||||||
s_line = line.lstrip()
|
|
||||||
indent = len(line) - len(s_line)
|
|
||||||
|
|
||||||
if any([s_line.startswith(s) for s in ['* [', '- [']]):
|
|
||||||
if indent == last_indent:
|
|
||||||
blocks[-1].append(line)
|
|
||||||
else:
|
|
||||||
blocks.append([line])
|
|
||||||
last_indent = indent
|
|
||||||
else:
|
else:
|
||||||
blocks.append([line])
|
blocks[i] = '#' + blocks[i]+'\n'
|
||||||
last_indent = None
|
|
||||||
|
# Sorting the libraries
|
||||||
|
inner_blocks = sorted(blocks[0].split('##'))
|
||||||
|
for i in range(1 , len(inner_blocks)):
|
||||||
|
if inner_blocks[i][0] != '#':
|
||||||
|
inner_blocks[i]='##'+inner_blocks[i]
|
||||||
|
inner_blocks=''.join(inner_blocks)
|
||||||
|
|
||||||
|
# Replacing the non-sorted libraries by the sorted ones and gathering all at the final_README file
|
||||||
|
blocks[0] = inner_blocks
|
||||||
|
final_README = table_of_contents + '- - -'+ ''.join(blocks)
|
||||||
|
|
||||||
with open('README.md', 'w+') as sorted_file:
|
with open('README.md', 'w+') as sorted_file:
|
||||||
# Then all of the blocks are sorted individually
|
sorted_file.write(final_README)
|
||||||
blocks = [''.join(sorted(block, key=lambda s: s.lower())) for block in blocks]
|
|
||||||
# And the result is written back to README.md
|
|
||||||
sorted_file.write(''.join(blocks))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user