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':'*'} |
---|
12 | self.precision=10 |
---|
13 | self.applist=[] |
---|
14 | self.progress=0 |
---|
15 | #def __init__ |
---|
16 | |
---|
17 | def __call__(self): |
---|
18 | return (self.applist) |
---|
19 | |
---|
20 | def set_debug(self,dbg='1'): |
---|
21 | self.dbg=int(dbg) |
---|
22 | self._debug ("Debug enabled") |
---|
23 | #def set__debug |
---|
24 | |
---|
25 | def _debug(self,msg=''): |
---|
26 | if self.dbg==1: |
---|
27 | print ('DEBUG Search: '+msg) |
---|
28 | #def _debug |
---|
29 | |
---|
30 | def register(self): |
---|
31 | return(self.pluginInfo) |
---|
32 | |
---|
33 | def execute_action(self,appstreamStore,action,tokens): |
---|
34 | if not type(tokens) is str: |
---|
35 | tokens='' |
---|
36 | if len(tokens.split(' '))>1 or action=='list': |
---|
37 | self._debug("Tokenizing search items") |
---|
38 | tokens=appstream.utils_search_tokenize(tokens) |
---|
39 | else: |
---|
40 | tokens=[tokens] |
---|
41 | self.store=appstreamStore |
---|
42 | if action=='list': |
---|
43 | self._list_category(tokens) |
---|
44 | if action=='list_sections': |
---|
45 | self._list_sections() |
---|
46 | if action=='search': |
---|
47 | self._search_app(tokens) |
---|
48 | self.progress=100 |
---|
49 | return(self.applist) |
---|
50 | |
---|
51 | def set_precision(self,precision): |
---|
52 | self.precision=precision |
---|
53 | |
---|
54 | def _search_app(self,tokens): |
---|
55 | self._debug("Searching app "+str(tokens)) |
---|
56 | applist=[] |
---|
57 | app=None |
---|
58 | if len(tokens)==1: |
---|
59 | app=self._app_exists(tokens[0]) |
---|
60 | |
---|
61 | if app: |
---|
62 | applist.append(app) |
---|
63 | self._debug("App direct match found: "+app.get_id()) |
---|
64 | else: |
---|
65 | for app in self._get_apps_by_match(tokens): |
---|
66 | applist.append(app) |
---|
67 | self.applist=applist |
---|
68 | return(applist) |
---|
69 | |
---|
70 | def _list_sections(self): |
---|
71 | applist=[] |
---|
72 | catDict={} |
---|
73 | for app in self.store.get_apps(): |
---|
74 | for cat in app.get_categories(): |
---|
75 | if cat not in catDict.keys(): |
---|
76 | catDict[cat]=1 |
---|
77 | else: |
---|
78 | catDict[cat]=catDict[cat]+1 |
---|
79 | for section in catDict: |
---|
80 | applist.append({section:catDict[section]}) |
---|
81 | self.applist=applist |
---|
82 | return(applist) |
---|
83 | |
---|
84 | def _list_category(self,tokens=[]): |
---|
85 | applist=[] |
---|
86 | self._debug("tokens: "+str(tokens)) |
---|
87 | if len(tokens)>=1: |
---|
88 | self._debug("Searching category "+str(tokens)) |
---|
89 | setCategories=set(tokens) |
---|
90 | apps_in_store=self.store.get_apps() |
---|
91 | count_apps=len(apps_in_store) |
---|
92 | self.progress=0 |
---|
93 | inc=100/count_apps |
---|
94 | for app in apps_in_store: |
---|
95 | self.progress=self.progress+inc |
---|
96 | if 'setCategories' in locals(): |
---|
97 | try: |
---|
98 | appCategories=[cat.lower() for cat in app.get_categories()] |
---|
99 | except: |
---|
100 | pass |
---|
101 | setAppCategories=set(appCategories) |
---|
102 | if list(setCategories.intersection(setAppCategories)): |
---|
103 | self._debug("Found "+app.get_id()) |
---|
104 | applist.append(app) |
---|
105 | else: |
---|
106 | self._debug("Loading all apps in store") |
---|
107 | applist=self.store.get_apps() |
---|
108 | # for app in applist: |
---|
109 | # self._debug("Added "+app.get_id()) |
---|
110 | self.applist=applist |
---|
111 | return(applist) |
---|
112 | |
---|
113 | def _app_exists(self,appName): |
---|
114 | self._debug("Trying direct match for "+appName) |
---|
115 | app=None |
---|
116 | #1.- Try exact match |
---|
117 | app=self.store.get_app_by_id(appName) |
---|
118 | if not app: |
---|
119 | #2.- Try with exact match with oorg.lliurex |
---|
120 | app=self.store.get_app_by_id("org.lliurex."+appName) |
---|
121 | if not app: |
---|
122 | #2.- Try with exact match with .desktop |
---|
123 | app=self.store.get_app_by_id(appName+".desktop") |
---|
124 | return(app) |
---|
125 | |
---|
126 | def _get_apps_by_match(self,tokens): |
---|
127 | #Add items witch match >= self.precision |
---|
128 | applist=[] |
---|
129 | apps_in_store=self.store.get_apps() |
---|
130 | if apps_in_store: |
---|
131 | auxDict={} |
---|
132 | count_apps=len(apps_in_store) |
---|
133 | self.progress=0 |
---|
134 | inc=100.0/count_apps |
---|
135 | for app in apps_in_store: |
---|
136 | self.progress=self.progress+inc |
---|
137 | if app not in self.applist: |
---|
138 | for token in tokens: |
---|
139 | score=app.search_matches(token) |
---|
140 | if score>=self.precision: |
---|
141 | if score in auxDict: |
---|
142 | auxDict[score].append(app) |
---|
143 | else: |
---|
144 | auxDict[score]=[app] |
---|
145 | for match in sorted(auxDict): |
---|
146 | for app in auxDict[match]: |
---|
147 | self._debug("Adding app "+app.get_id() + " with score: "+str(match)) |
---|
148 | applist.insert(0,app) |
---|
149 | return(applist) |
---|