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