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=0 |
---|
10 | self.store='' |
---|
11 | self.pluginInfo={'search':'*','list':'*','list_sections':'*','info':'*'} |
---|
12 | self.precision=10 |
---|
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='1'): |
---|
24 | self.dbg=int(dbg) |
---|
25 | self._debug ("Debug enabled") |
---|
26 | #def set__debug |
---|
27 | |
---|
28 | def _debug(self,msg=''): |
---|
29 | if self.dbg==1: |
---|
30 | print ('DEBUG Search: '+msg) |
---|
31 | #def _debug |
---|
32 | |
---|
33 | def register(self): |
---|
34 | return(self.pluginInfo) |
---|
35 | |
---|
36 | def execute_action(self,appstreamStore,action,tokens,exact_match_for_search=False): |
---|
37 | if not type(tokens) is str: |
---|
38 | tokens='' |
---|
39 | else: |
---|
40 | tokens=tokens.lower() |
---|
41 | if len(tokens.split(' '))>1: |
---|
42 | if action=='search': |
---|
43 | self._debug("Tokenizing search items") |
---|
44 | tokens=appstream.utils_search_tokenize(tokens) |
---|
45 | else: |
---|
46 | tokens=tokens.split(' ') |
---|
47 | else: |
---|
48 | if len(tokens)>=1: |
---|
49 | tokens=[tokens] |
---|
50 | else: |
---|
51 | tokens=[] |
---|
52 | |
---|
53 | self.store=appstreamStore |
---|
54 | self.result['status']={'status':-1,'msg':''} |
---|
55 | self.result['data']=[] |
---|
56 | if action=='list': |
---|
57 | self._list_category(tokens) |
---|
58 | if action=='list_sections': |
---|
59 | self._list_sections() |
---|
60 | if (action=='search' or action=='info'): |
---|
61 | self._search_app(tokens,exact_match_for_search) |
---|
62 | self.progress=100 |
---|
63 | return(self.result) |
---|
64 | |
---|
65 | def _set_status(self,status,msg=''): |
---|
66 | self.result['status']={'status':status,'msg':msg} |
---|
67 | |
---|
68 | def set_precision(self,precision): |
---|
69 | self.precision=precision |
---|
70 | |
---|
71 | def _search_app(self,tokens,exact_match): |
---|
72 | self._debug("Searching app "+str(tokens)+ " with exact_match="+str(exact_match)) |
---|
73 | applist=[] |
---|
74 | app=None |
---|
75 | if len(tokens)==1: |
---|
76 | app=self._app_exists(tokens[0]) |
---|
77 | if app: |
---|
78 | applist.append(app) |
---|
79 | self._debug("App direct match found: "+app.get_id()) |
---|
80 | if not exact_match: |
---|
81 | applist=self._get_apps_by_match(tokens,applist) |
---|
82 | if len(applist): |
---|
83 | self._set_status(0) |
---|
84 | else: |
---|
85 | applist=self._get_app_by_pkgname(tokens,applist) |
---|
86 | if len(applist): |
---|
87 | self._set_status(0) |
---|
88 | else: |
---|
89 | self._set_status(1) |
---|
90 | self.result['data']=applist |
---|
91 | return(applist) |
---|
92 | |
---|
93 | def _list_sections(self): |
---|
94 | applist=[] |
---|
95 | catDict={} |
---|
96 | for app in self.store.get_apps(): |
---|
97 | for cat in app.get_categories(): |
---|
98 | if cat not in catDict.keys(): |
---|
99 | catDict[cat]=1 |
---|
100 | else: |
---|
101 | catDict[cat]=catDict[cat]+1 |
---|
102 | for section in catDict: |
---|
103 | applist.append({str(section):catDict[section]}) |
---|
104 | self.result['data']=applist |
---|
105 | if len(applist): |
---|
106 | self._set_status(0) |
---|
107 | else: |
---|
108 | self._set_status(1) |
---|
109 | return(applist) |
---|
110 | |
---|
111 | def _list_category(self,tokens=[]): |
---|
112 | applist=[] |
---|
113 | self._debug("tokens: "+str(tokens)) |
---|
114 | if len(tokens)>=1: |
---|
115 | self._debug("Searching category "+str(tokens)) |
---|
116 | setCategories=set(tokens) |
---|
117 | apps_in_store=self.store.get_apps() |
---|
118 | count_apps=len(apps_in_store) |
---|
119 | self.progress=0 |
---|
120 | inc=100/count_apps |
---|
121 | for app in apps_in_store: |
---|
122 | self.progress=self.progress+inc |
---|
123 | if 'setCategories' in locals(): |
---|
124 | try: |
---|
125 | appCategories=[cat.lower() for cat in app.get_categories()] |
---|
126 | except: |
---|
127 | pass |
---|
128 | setAppCategories=set(appCategories) |
---|
129 | if setCategories.issubset(setAppCategories): |
---|
130 | self._debug("Found "+app.get_id()) |
---|
131 | applist.append(app) |
---|
132 | else: |
---|
133 | self._debug("Loading all apps in store") |
---|
134 | applist=self.store.get_apps() |
---|
135 | # for app in applist: |
---|
136 | # self._debug("Added "+app.get_id()) |
---|
137 | self.result['data']=applist |
---|
138 | if len(applist): |
---|
139 | self._set_status(0) |
---|
140 | else: |
---|
141 | self._set_status(1) |
---|
142 | return(applist) |
---|
143 | |
---|
144 | def _app_exists(self,appName): |
---|
145 | self._debug("Trying direct match for "+appName) |
---|
146 | app=None |
---|
147 | #1.- Try exact match |
---|
148 | app=self.store.get_app_by_id(appName) |
---|
149 | if not app: |
---|
150 | #2.- Try exact match with org.lliurex |
---|
151 | app=self.store.get_app_by_id("org.lliurex."+appName) |
---|
152 | if not app: |
---|
153 | #2.- Try exact match with .desktop |
---|
154 | app=self.store.get_app_by_id(appName+".desktop") |
---|
155 | return(app) |
---|
156 | |
---|
157 | def _get_apps_by_match(self,tokens,applist=[]): |
---|
158 | #Add items witch match >= self.precision |
---|
159 | apps_in_store=self.store.get_apps() |
---|
160 | if apps_in_store: |
---|
161 | auxDict={} |
---|
162 | count_apps=len(apps_in_store) |
---|
163 | self.progress=0 |
---|
164 | inc=100.0/count_apps |
---|
165 | for app in apps_in_store: |
---|
166 | self.progress=self.progress+inc |
---|
167 | if app not in self.applist: |
---|
168 | for token in tokens: |
---|
169 | score=app.search_matches(token) |
---|
170 | if score>=self.precision: |
---|
171 | if score in auxDict: |
---|
172 | auxDict[score].append(app) |
---|
173 | else: |
---|
174 | auxDict[score]=[app] |
---|
175 | for match in sorted(auxDict): |
---|
176 | for app in auxDict[match]: |
---|
177 | if app not in applist: |
---|
178 | self._debug("Adding app "+app.get_id() + " with score: "+str(match)) |
---|
179 | applist.insert(1,app) |
---|
180 | return(applist) |
---|
181 | |
---|
182 | def _get_app_by_pkgname(self,tokens,applist=[]): |
---|
183 | apps_in_store=self.store.get_apps() |
---|
184 | if apps_in_store: |
---|
185 | count_apps=len(apps_in_store) |
---|
186 | self.progress=0 |
---|
187 | inc=100.0/count_apps |
---|
188 | for app in apps_in_store: |
---|
189 | self.progress=self.progress+inc |
---|
190 | if app not in self.applist: |
---|
191 | for token in tokens: |
---|
192 | if app.get_pkgname_default()==token: |
---|
193 | applist.append(app) |
---|
194 | |
---|
195 | return(applist) |
---|