1 | import locale |
---|
2 | import gi |
---|
3 | gi.require_version('AppStreamGlib', '1.0') |
---|
4 | from gi.repository import AppStreamGlib as appstream |
---|
5 | |
---|
6 | class searchmanager: |
---|
7 | def __init__(self): |
---|
8 | self.locale=locale.getlocale()[0] |
---|
9 | self.dbg=False |
---|
10 | self.store='' |
---|
11 | self.plugin_actions={'search':'*','list':'*','list_sections':'*','info':'*'} |
---|
12 | self.precision=1 |
---|
13 | self.applist=[] |
---|
14 | self.progress=0 |
---|
15 | self.result={} |
---|
16 | self.result['data']={} |
---|
17 | self.result['status']={} |
---|
18 | #def __init__ |
---|
19 | |
---|
20 | def __call__(self): |
---|
21 | return (self.applist) |
---|
22 | |
---|
23 | def set_debug(self,dbg=True): |
---|
24 | self.dbg=dbg |
---|
25 | self._debug ("Debug enabled") |
---|
26 | #def set__debug |
---|
27 | |
---|
28 | def _debug(self,msg=''): |
---|
29 | if self.dbg: |
---|
30 | print ('DEBUG Search: %s'%msg) |
---|
31 | #def _debug |
---|
32 | |
---|
33 | def register(self): |
---|
34 | return(self.plugin_actions) |
---|
35 | |
---|
36 | def execute_action(self,appstreamStore,action,tokens,exact_match_for_search=False,max_results=0): |
---|
37 | self._debug("Executing action %s"%action) |
---|
38 | self._debug("Tokens: %s"%tokens) |
---|
39 | self.progress=0 |
---|
40 | if type(tokens)==type([]): |
---|
41 | tokens=' '.join(tokens) |
---|
42 | if type(tokens) is str: |
---|
43 | tokens=tokens.lower() |
---|
44 | else: |
---|
45 | tokens='' |
---|
46 | if len(tokens.split(' '))>1: |
---|
47 | if action=='search': |
---|
48 | self._debug("Tokenizing search items") |
---|
49 | tokens=appstream.utils_search_tokenize(tokens) |
---|
50 | else: |
---|
51 | tokens=tokens.split(' ') |
---|
52 | else: |
---|
53 | if len(tokens)>=1: |
---|
54 | tokens=[tokens] |
---|
55 | else: |
---|
56 | tokens=[] |
---|
57 | |
---|
58 | self.store=appstreamStore |
---|
59 | self.result['status']={'status':-1,'msg':''} |
---|
60 | self.result['data']=[] |
---|
61 | if self.store: |
---|
62 | if action=='list': |
---|
63 | self._list_category(tokens,max_results) |
---|
64 | if action=='list_sections': |
---|
65 | self._list_sections() |
---|
66 | if (action=='search' or action=='info'): |
---|
67 | self._search_app(tokens,exact_match_for_search) |
---|
68 | else: |
---|
69 | self._debug("Search needs a store") |
---|
70 | self.progress=100 |
---|
71 | return(self.result) |
---|
72 | |
---|
73 | def _set_status(self,status,msg=''): |
---|
74 | self.result['status']={'status':status,'msg':msg} |
---|
75 | |
---|
76 | def set_precision(self,precision): |
---|
77 | self.precision=precision |
---|
78 | |
---|
79 | def _search_app(self,tokens,exact_match): |
---|
80 | self._debug("Searching app "+str(tokens)+ " with exact_match="+str(exact_match)) |
---|
81 | applist=[] |
---|
82 | app=None |
---|
83 | # if len(tokens)==1: |
---|
84 | if exact_match: |
---|
85 | app=self._app_exists(tokens[0]) |
---|
86 | if app: |
---|
87 | if type(app)==type([]): |
---|
88 | applist.extend(app) |
---|
89 | else: |
---|
90 | applist.append(app) |
---|
91 | # self._debug("App direct match found: "+app.get_id()) |
---|
92 | if not exact_match: |
---|
93 | applist.extend(self._get_apps_by_match(tokens,applist)) |
---|
94 | # if len(applist): |
---|
95 | # self._set_status(0) |
---|
96 | # else: |
---|
97 | applist.extend(self._get_app_by_pkgname(tokens,applist)) |
---|
98 | applist=set(applist) |
---|
99 | if len(applist): |
---|
100 | self._set_status(0) |
---|
101 | else: |
---|
102 | self._set_status(1) |
---|
103 | self.result['data']=applist |
---|
104 | return(applist) |
---|
105 | |
---|
106 | def _list_sections(self): |
---|
107 | applist=[] |
---|
108 | categories={} |
---|
109 | for app in self.store.get_apps(): |
---|
110 | for cat in app.get_categories(): |
---|
111 | if cat not in categories.keys(): |
---|
112 | categories[cat]=1 |
---|
113 | else: |
---|
114 | categories[cat]=categories[cat]+1 |
---|
115 | for section in categories: |
---|
116 | applist.append({str(section):categories[section]}) |
---|
117 | self.result['data']=applist |
---|
118 | if len(applist): |
---|
119 | self._set_status(0) |
---|
120 | else: |
---|
121 | self._set_status(1) |
---|
122 | return(applist) |
---|
123 | |
---|
124 | def _list_category(self,tokens=[],max_results=0): |
---|
125 | applist=[] |
---|
126 | self._debug("tokens: "+str(tokens)) |
---|
127 | self._debug("Max results: %s"%max_results) |
---|
128 | if len(tokens)>=1: |
---|
129 | self._debug("Searching category "+str(tokens)) |
---|
130 | categories_set=set(tokens) |
---|
131 | apps_in_store=self.store.get_apps() |
---|
132 | count_apps=len(apps_in_store) |
---|
133 | self.progress=0 |
---|
134 | inc=100/count_apps |
---|
135 | for app in apps_in_store: |
---|
136 | self.progress=self.progress+inc |
---|
137 | if 'categories_set' in locals(): |
---|
138 | try: |
---|
139 | app_categories=[cat.lower() for cat in app.get_categories()] |
---|
140 | except: |
---|
141 | pass |
---|
142 | app_categories_set=set(app_categories) |
---|
143 | if categories_set.issubset(app_categories_set): |
---|
144 | self._debug("Found "+app.get_id()) |
---|
145 | applist.append(app) |
---|
146 | else: |
---|
147 | self._debug("Loading all apps in store") |
---|
148 | applist=self.store.get_apps() |
---|
149 | categories_set=set(['snap','appimage']) |
---|
150 | applist_2=[] |
---|
151 | for app in applist: |
---|
152 | if 'categories_set' in locals(): |
---|
153 | try: |
---|
154 | app_categories=[cat.lower() for cat in app.get_categories()] |
---|
155 | except: |
---|
156 | pass |
---|
157 | app_categories_set=set(app_categories) |
---|
158 | if not categories_set.issubset(app_categories_set): |
---|
159 | applist_2.append(app) |
---|
160 | else: |
---|
161 | print("Removing %s"%app.get_pkgname()) |
---|
162 | applist=applist_2 |
---|
163 | # for app in applist: |
---|
164 | # self._debug("Added "+app.get_id()) |
---|
165 | if max_results: |
---|
166 | applist=applist[0:max_results] |
---|
167 | #List only valid categories |
---|
168 | |
---|
169 | self.result['data']=applist |
---|
170 | if len(applist): |
---|
171 | self._set_status(0) |
---|
172 | else: |
---|
173 | self._set_status(1) |
---|
174 | return(applist) |
---|
175 | |
---|
176 | def _app_exists(self,app_name): |
---|
177 | self._debug("Trying direct match for "+app_name) |
---|
178 | #id_matches defines how to search for an app |
---|
179 | # %s -> app_name; zero-lliurex-%s -> app_name with zero-lliurex- prefix and so on... |
---|
180 | id_matches=['%s','zero-lliurex-%s','%s.desktop'] |
---|
181 | app=None |
---|
182 | for id_string in id_matches: |
---|
183 | # app=self.store.get_app_by_id_ignore_prefix(id_string%app_name) |
---|
184 | app=self.store.get_apps_by_id(id_string%app_name) |
---|
185 | if app: |
---|
186 | break |
---|
187 | |
---|
188 | |
---|
189 | #1.- Try exact match |
---|
190 | # app=self.store.get_apps_by_id(app_name) |
---|
191 | # if not app: |
---|
192 | # #2.- Try exact match with zero-lliurex- for the zomandos |
---|
193 | # app=self.store.get_app_by_id("zero-lliurex-"+app_name) |
---|
194 | # if not app: |
---|
195 | # #3.- Try exact match with .desktop |
---|
196 | # app=self.store.get_apps_by_id(app_name+".desktop") |
---|
197 | if not app: |
---|
198 | #4.- Try exact match by pkgname |
---|
199 | app=self.store.get_app_by_pkgname(app_name) |
---|
200 | # if not app: |
---|
201 | # app=self.store.get_app_by_id_ignore_prefix(app_name) |
---|
202 | self._debug("App found %s"%app) |
---|
203 | return(app) |
---|
204 | |
---|
205 | def _get_apps_by_match(self,tokens,applist=[]): |
---|
206 | #Add items with match >= self.precision |
---|
207 | self._debug("Searching app by fuzzy match") |
---|
208 | if not applist: |
---|
209 | position=1 |
---|
210 | else: |
---|
211 | position=len(applist)+1 |
---|
212 | apps_in_store=self.store.get_apps() |
---|
213 | if apps_in_store: |
---|
214 | tmp_app_dict={} |
---|
215 | count_apps=len(apps_in_store) |
---|
216 | self.progress=0 |
---|
217 | inc=100.0/count_apps |
---|
218 | for app in apps_in_store: |
---|
219 | self.progress=self.progress+inc |
---|
220 | if app not in self.applist: |
---|
221 | for token in tokens: |
---|
222 | score=app.search_matches(token) |
---|
223 | if score>=self.precision: |
---|
224 | # if "appimage" in app.get_id().lower(): |
---|
225 | # score=1 |
---|
226 | if score in tmp_app_dict: |
---|
227 | tmp_app_dict[score].append(app) |
---|
228 | else: |
---|
229 | tmp_app_dict[score]=[app] |
---|
230 | fake_app=[] |
---|
231 | for match in sorted(tmp_app_dict.keys()): |
---|
232 | for app in tmp_app_dict[match]: |
---|
233 | if app not in applist: |
---|
234 | self._debug("Adding app "+app.get_id() + " with score: "+str(match)) |
---|
235 | applist.insert(0,app) |
---|
236 | return(applist) |
---|
237 | |
---|
238 | def _get_app_by_pkgname(self,tokens,applist=[]): |
---|
239 | if not applist: |
---|
240 | position=1 |
---|
241 | else: |
---|
242 | position=len(applist)+1 |
---|
243 | apps_in_store=self.store.get_apps() |
---|
244 | if apps_in_store: |
---|
245 | count_apps=len(apps_in_store) |
---|
246 | self.progress=0 |
---|
247 | inc=100.0/count_apps |
---|
248 | for app in apps_in_store: |
---|
249 | self.progress=self.progress+inc |
---|
250 | if app not in self.applist: |
---|
251 | for token in tokens: |
---|
252 | if app.get_pkgname_default()==token: |
---|
253 | applist.insert(1,app) |
---|
254 | |
---|
255 | return(applist) |
---|