This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
sort.py
84 lines (74 loc) · 3.51 KB
/
sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# coding: utf-8
from collections import Counter
import json
import os
stats = {'city': 3, 'technology': 4, 'database': 5, 'cloud': 6}
def generate_charts(chart_type):
columns = filter_columns('README.md', stats[chart_type])
sorted_columns_names = sorted(columns, key=columns.get, reverse=True)
with open(f'{chart_type}_usage.md', 'w+', encoding='UTF-8') as f:
f.write('|+%s | Count |\n' % chart_type.capitalize())
f.write('|------------ | -----------|\n')
for columns_name in sorted_columns_names:
count = columns[columns_name]
f.write(f'| {columns_name} | {count} |\n')
with open(f'json/{chart_type}_usage.json', 'w+', encoding='UTF-8') as f:
json.dump(columns, f, sort_keys=True, indent=2, ensure_ascii=False)
def filter_columns(readme_file, column_number):
counter = Counter()
with open(readme_file, 'r', encoding='UTF-8') as read_me_file:
for line in read_me_file:
s_line = line.lstrip()
if any([s_line.startswith(s) for s in ['| '] if s not in ('|+')]):
tech_column = s_line.split('|')[column_number]
columns = tech_column.split(',')
columns = list(map(str.strip, columns))
columns = list(filter(lambda x: x != '-', columns))
columns = list(filter(lambda x: x, columns)) # filter empty items
for column in columns:
counter[column] += 1
return counter
def sort():
with open('README.md', 'r', encoding='UTF-8') as read_me_file:
read_me = read_me_file.readlines()
blocks = []
last_indent = None
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 s not in ('|+') ]):
if indent == last_indent:
blocks[-1].append(line)
else:
blocks.append([line])
last_indent = indent
else:
blocks.append([line])
last_indent = None
try:
os.stat('json')
except:
os.mkdir('json')
with open('README.md', 'w+', encoding='UTF-8') as sorted_file:
blocks = [''.join(sorted(block, key=lambda s: s.lower())) for block in blocks]
sorted_file.write(''.join(blocks))
with open(f'json/empresas.json', 'w+', encoding='UTF-8') as f:
for block in blocks:
if block.startswith('| '):
l_block = block.splitlines()
ciaList = {}
for cia in l_block:
ciacol = (cia.split('|'))
name = ciacol[1].strip()
site = ciacol[2].strip().replace('[Site](','').replace(')','')
city = None if ciacol[3].strip() == '-' else ciacol[3].strip()
technology = None if ciacol[4].strip() == '-' else ciacol[4].strip().split(', ')
database = None if ciacol[5].strip() == '-' else ciacol[5].strip().split(', ')
cloud = None if ciacol[6].strip() == '-' else ciacol[6].strip().split(', ')
tipo = ciacol[7].strip()
ciaList[name] = {'site': site, 'city': city, 'technology': technology , 'database': database ,'cloud': cloud ,'type': tipo}
json.dump(ciaList, f, sort_keys=True, indent=2, ensure_ascii=False)
for chart_type in stats.keys():
generate_charts(chart_type)
if __name__ == "__main__":
sort()