1
1
#!/usr/bin/env python3
2
2
3
+ import argparse , re , sys
4
+ import xml .etree .ElementTree as ET
3
5
from datetime import datetime
6
+ from difflib import SequenceMatcher
4
7
5
8
import yaml
6
-
7
9
from natsort import natsorted as sorted
8
10
11
+ from resolve_paths import paths
12
+
9
13
10
- class CustomDumper (yaml .SafeDumper ):
14
+ class YamlNewLines (yaml .SafeDumper ):
11
15
# https://github.com/yaml/pyyaml/issues/127#issuecomment-525800484
12
16
def write_line_break (self , data = None ):
13
17
super ().write_line_break (data )
@@ -17,6 +21,31 @@ def write_line_break(self, data=None):
17
21
def increase_indent (self , flow = False , indentless = False ):
18
22
return super ().increase_indent (flow , False )
19
23
24
+ filename = 'requests.yml'
25
+
26
+
27
+ parser = argparse .ArgumentParser (description = f'parse { filename } ' )
28
+
29
+ parser .add_argument ('-f' , '--format' ,
30
+ dest = 'format' ,
31
+ choices = ['xml' , 'yml' ],
32
+ help = 'output in specific format' )
33
+ parser .add_argument ('-r' , '--remove' ,
34
+ dest = 'remove' ,
35
+ help = f'remove existing compinfos from { filename } ' ,
36
+ default = False ,
37
+ action = argparse .BooleanOptionalAction )
38
+ parser .add_argument ('-s' , '--sort' ,
39
+ dest = 'sort' ,
40
+ help = 'sort by specific value' ,
41
+ choices = ['name' , 'ratio' , 'request' ],
42
+ default = 'ratio' )
43
+ parser .add_argument ('-H' , '--hide-ratios' ,
44
+ dest = 'hide_ratios' ,
45
+ help = 'hide ratios in output' ,
46
+ default = False ,
47
+ action = argparse .BooleanOptionalAction )
48
+
20
49
21
50
def read (path ):
22
51
with open (path , 'r+' ) as file :
@@ -34,7 +63,71 @@ def write(path, data):
34
63
# header message with total number of requested icons and last time update
35
64
header = (f"# { len (data )} requested apps pending \n "
36
65
f"# updated { datetime .today ().strftime ('%Y-%m-%d %H:%M:%S' )} \n \n " )
37
- dump = yaml .dump (data , Dumper = CustomDumper , allow_unicode = True , indent = 4 , sort_keys = False )
66
+ dump = yaml .dump (data , Dumper = YamlNewLines , allow_unicode = True , indent = 4 , sort_keys = False )
38
67
file .seek (0 )
39
68
file .write (header + dump )
40
69
file .truncate ()
70
+
71
+ def main ():
72
+ requests = read (paths ['requests' ])
73
+ updatable = {}
74
+
75
+ with open (paths ['appfilter' ][0 ], 'r' ) as file :
76
+ appfilter = ET .ElementTree (ET .fromstring (file .read ())).getroot ()
77
+
78
+ for item in appfilter :
79
+ try :
80
+ compinfo = re .search ('ComponentInfo{(.*)}' , item .attrib ['component' ]).group (1 )
81
+ id = compinfo .split ('/' )[0 ]
82
+ name = item .attrib ['drawable' ]
83
+
84
+ for request in requests :
85
+ if id not in request : continue
86
+ diff = SequenceMatcher (None , request , compinfo ).ratio ()
87
+ ratio = round (diff , 2 )
88
+
89
+ if ratio == 1.0 :
90
+ requests .pop (compinfo )
91
+ continue
92
+
93
+ if ratio >= 0.75 :
94
+ if request in updatable :
95
+ if updatable [request ]['ratio' ] < ratio :
96
+ ratio = updatable [request ]['ratio' ]
97
+
98
+ updatable [request ] = {
99
+ 'name' : name ,
100
+ 'ratio' : ratio ,
101
+ 'request' : request
102
+ }
103
+ except :
104
+ continue
105
+
106
+ updatable = dict (sorted (updatable .items (), key = lambda x : x [1 ][args .sort ]))
107
+
108
+ if args .remove :
109
+ write (paths ['requests' ], requests )
110
+
111
+ for [key , value ] in updatable .items ():
112
+ name = value ['name' ]
113
+ ratio = int (value ['ratio' ] * 100 )
114
+
115
+ match args .format :
116
+ case 'xml' :
117
+ ratios = f'<!-- { ratio } % --> ' if not args .hide_ratios else ''
118
+ print (ratios + f'<item component="ComponentInfo{{{ key } }}" drawable="{ name } " />' )
119
+ case 'yml' :
120
+ ratios = f' # { ratio } %' if not args .hide_ratios else ''
121
+ print (f'{ name } :' + ratios )
122
+ print (f' - { key } \n ' )
123
+ case _:
124
+ ratios = f'[{ ratio } %] ' if not args .hide_ratios else ''
125
+ print (ratios + f'{ name } -> { key } ' )
126
+
127
+ if __name__ == '__main__' :
128
+ if len (sys .argv ) > 1 :
129
+ args = parser .parse_args ()
130
+ else :
131
+ parser .print_help ()
132
+ sys .exit (1 )
133
+ main ()
0 commit comments