1 | import lliurexstore.storeManager |
---|
2 | import time |
---|
3 | import Package |
---|
4 | import Core |
---|
5 | import random |
---|
6 | import copy |
---|
7 | import gettext |
---|
8 | |
---|
9 | class LliurexStoreManager: |
---|
10 | |
---|
11 | def __init__(self): |
---|
12 | |
---|
13 | self.store=lliurexstore.storeManager.StoreManager(snap=True,appimage=True) |
---|
14 | # library has its own textdomain and I am forced to change it back to lliurex-store |
---|
15 | gettext.textdomain('lliurex-store') |
---|
16 | self.core=Core.Core.get_core() |
---|
17 | |
---|
18 | #def init |
---|
19 | |
---|
20 | def list_sections(self): |
---|
21 | action="list_sections" |
---|
22 | self.store.execute_action(action,True) |
---|
23 | |
---|
24 | while self.store.is_action_running(action): |
---|
25 | time.sleep(0.2) |
---|
26 | |
---|
27 | print(self.store.get_status(action)) |
---|
28 | print(self.store.get_result(action)) |
---|
29 | print("DONE") |
---|
30 | |
---|
31 | #def search |
---|
32 | |
---|
33 | |
---|
34 | def search_package(self,search_text): |
---|
35 | |
---|
36 | action="search" |
---|
37 | self.store.execute_action(action,search_text) |
---|
38 | |
---|
39 | packages=[] |
---|
40 | |
---|
41 | while self.store.is_action_running("search"): |
---|
42 | time.sleep(0.2) |
---|
43 | |
---|
44 | ret=self.store.get_status(action) |
---|
45 | |
---|
46 | if ret["status"]==0: |
---|
47 | data=self.store.get_result(action) |
---|
48 | for item in data["search"]: |
---|
49 | |
---|
50 | p=Package.Package(item) |
---|
51 | packages.append(p) |
---|
52 | |
---|
53 | return packages |
---|
54 | |
---|
55 | #def search_package |
---|
56 | |
---|
57 | |
---|
58 | def get_info(self,pkg_id): |
---|
59 | |
---|
60 | action="info" |
---|
61 | |
---|
62 | self.store.execute_action(action,pkg_id) |
---|
63 | while self.store.is_action_running(action): |
---|
64 | time.sleep(0.2) |
---|
65 | ret=self.store.get_status(action) |
---|
66 | |
---|
67 | if ret["status"]==0: |
---|
68 | |
---|
69 | data=self.store.get_result(action) |
---|
70 | try: |
---|
71 | p=Package.Package(data["info"][0]) |
---|
72 | except: |
---|
73 | return(Package.Package()) |
---|
74 | |
---|
75 | categories=copy.deepcopy(p["categories"]) |
---|
76 | banned=set() |
---|
77 | |
---|
78 | for item in self.core.categories_manager.categories: |
---|
79 | if item in categories and len(categories) > 1: |
---|
80 | categories.remove(item) |
---|
81 | |
---|
82 | for item in self.core.categories_manager.banned_categories: |
---|
83 | if item in categories and len(categories) > 1: |
---|
84 | categories.remove(item) |
---|
85 | |
---|
86 | |
---|
87 | if len(categories)>0: |
---|
88 | |
---|
89 | random_id=int(random.random()*len(categories)) |
---|
90 | |
---|
91 | random_category=categories[random_id] |
---|
92 | pkgs,categories=self.get_package_list_from_category(random_category,10) |
---|
93 | |
---|
94 | if len(pkgs) >=10: |
---|
95 | samples=10 |
---|
96 | else: |
---|
97 | samples=len(pkgs) |
---|
98 | |
---|
99 | for item in random.sample(pkgs,samples): |
---|
100 | if item["package"]!=pkg_id: |
---|
101 | p["related_packages"].append(item) |
---|
102 | |
---|
103 | p.fix_info() |
---|
104 | |
---|
105 | return p |
---|
106 | |
---|
107 | #def get_info |
---|
108 | |
---|
109 | |
---|
110 | def get_package_list_from_category(self,category_tag=None,results=0): |
---|
111 | |
---|
112 | action="list" |
---|
113 | self.store.execute_action(action,[category_tag],max_results=results) |
---|
114 | |
---|
115 | while self.store.is_action_running(action): |
---|
116 | time.sleep(0.2) |
---|
117 | |
---|
118 | packages=[] |
---|
119 | categories=set() |
---|
120 | banned=set() |
---|
121 | |
---|
122 | if category_tag in self.core.categories_manager.categories: |
---|
123 | for item in self.core.categories_manager.categories[category_tag]["sections"]: |
---|
124 | categories.add(item) |
---|
125 | |
---|
126 | for item in self.core.categories_manager.banned_categories: |
---|
127 | banned.add(item) |
---|
128 | |
---|
129 | |
---|
130 | ret=self.store.get_status(action) |
---|
131 | if ret["status"]==0: |
---|
132 | for item in self.store.get_result(action)["list"]: |
---|
133 | p=Package.Package(item) |
---|
134 | |
---|
135 | for category in p["categories"]: |
---|
136 | if type(category)!=str: |
---|
137 | continue |
---|
138 | if category not in banned and not category.startswith("X-") and category in categories: |
---|
139 | categories.add(category) |
---|
140 | |
---|
141 | packages.append(p) |
---|
142 | |
---|
143 | return(packages,categories) |
---|
144 | |
---|
145 | #def get_package_list |
---|
146 | |
---|
147 | |
---|
148 | def get_installed_list(self): |
---|
149 | |
---|
150 | pkgs,categories=self.get_package_list_from_category() |
---|
151 | ret=[] |
---|
152 | |
---|
153 | for pkg in pkgs: |
---|
154 | |
---|
155 | if pkg["state"]=="installed": |
---|
156 | p=Package.Package(pkg) |
---|
157 | ret.append(p) |
---|
158 | |
---|
159 | return ret |
---|
160 | |
---|
161 | #def get_installed_list |
---|
162 | |
---|
163 | |
---|
164 | def install_package(self,pkg_id): |
---|
165 | |
---|
166 | action="install" |
---|
167 | self.store.execute_action(action,pkg_id) |
---|
168 | |
---|
169 | while self.store.is_action_running(action): |
---|
170 | time.sleep(0.2) |
---|
171 | |
---|
172 | ret=self.store.get_status(action) |
---|
173 | if ret["status"]==0: |
---|
174 | return True |
---|
175 | |
---|
176 | return False |
---|
177 | |
---|
178 | #def install_package |
---|
179 | |
---|
180 | |
---|
181 | def uninstall_package(self,pkg_id): |
---|
182 | |
---|
183 | action="remove" |
---|
184 | self.store.execute_action(action,pkg_id) |
---|
185 | |
---|
186 | while self.store.is_action_running(action): |
---|
187 | time.sleep(0.2) |
---|
188 | |
---|
189 | ret=self.store.get_status(action) |
---|
190 | if ret["status"]==0: |
---|
191 | return True |
---|
192 | |
---|
193 | return False |
---|
194 | |
---|
195 | #def install_package |
---|
196 | |
---|
197 | |
---|
198 | #class StoreManager |
---|