2408e9a00b40a739d70c4919e7b417181c5c5313
[findit] / src / debs / search.py
1 #!/usr/bin/env python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4
5 import apt_pkg
6 from heapq import nlargest
7
8 from misc import size_hum_read, _
9
10 #==============================================================================
11
12 class Control(object):
13
14     def __init__(self, ui):
15         count = 12
16
17         print ui
18         if ui == 'cli':
19             self.present = Cli_Presentation(count, self.start_search)
20         elif ui == 'gtk':
21             self.present = Gtk_Presentation(count, self.start_search)
22
23         self.abstrac = Abstraction(self.present)
24
25     def start_search(self, get_data):
26         deblist = []
27         count, outtype = get_data()
28         search_func = self.abstrac.pkggetter()
29         for size, name in nlargest(count, search_func):
30             deblist.append([int(size), name, size_hum_read(size)])
31         self.present.show_out_toplevel(None, outtype, deblist)
32
33     def run(self):
34         return self.present.toplevel
35
36 #==============================================================================
37
38 class Abstraction(object):
39     def __init__(self, presentation):
40         self.presentation = presentation
41         apt_pkg.InitConfig()
42         apt_pkg.InitSystem()
43         self.cache = apt_pkg.GetCache()
44
45     def pkggetter(self):
46         self.fullsize = 0
47         for pkg in self.cache.Packages:
48             # pkg is from a list of packages, sorted by name.
49             if pkg.CurrentState == apt_pkg.CurStateInstalled:
50                 pkgsize = [version.InstalledSize for version in pkg.VersionList][0]
51                 self.fullsize = self.fullsize + pkgsize
52                 yield pkgsize, pkg.Name
53
54     def full(self):
55         return self.fullsize
56
57
58 #==============================================================================
59
60 class Cli_Presentation(object):
61     def __init__(self, start_func):
62         self.stopit = False
63                   #     get_data,      get_stopit, label, kill_func)
64         start_func(self.get_data, self.get_stopit, self.kill_wind)
65         pass
66
67     def show_current_status(self, current_path):
68         print current_path
69
70 #==============================================================================
71
72 class Gtk_Presentation(object):
73
74     def __init__(self, count, start_func):
75         import gtk
76
77         # "Packages quantity" label
78         qty_label = gtk.Label('Debian packages quantity')
79
80         # "Packages quantity" spin
81         self.qty_spin = gtk.SpinButton()
82         self.qty_spin.set_numeric(True)
83         self.qty_spin.set_range(0, 65536)
84         self.qty_spin.set_increments(1, 10)
85         self.qty_spin.set_value(count)
86
87         # "Start" button
88         start_btn = gtk.Button('Start')
89         start_btn.connect('released', self.start_btn_released, start_func)
90
91         # Output selection
92         self.outtable_rbtn = gtk.RadioButton(None, 'Table')
93         self.outtable_rbtn.set_name('outtable')
94         outdiagram_rbtn = gtk.RadioButton(self.outtable_rbtn, 'Diagram')
95         outdiagram_rbtn.set_name('outdiagram')
96         out2_rbtn = gtk.RadioButton(self.outtable_rbtn, 'Another 2')
97         out2_rbtn.set_name('outanother2')
98         out_rbtns = [self.outtable_rbtn, outdiagram_rbtn, out2_rbtn]
99
100         hbox = gtk.HBox(False, 4)
101         hbox.pack_start(qty_label, False, False, 0)
102         hbox.pack_start(self.qty_spin, False, False, 0)
103         hbox.pack_start(start_btn, False, False, 0)
104         for btn in reversed(out_rbtns):
105             hbox.pack_end(btn, False, False, 0)
106
107         statusbar = gtk.Statusbar()
108         context_id = statusbar.get_context_id('Selected package')
109         statusbar.push(context_id, 'test')
110
111         self.vbox = gtk.VBox(False, 4)
112         self.vbox.set_border_width(4)
113         self.vbox.pack_start(hbox, False, False, 0)
114         self.vbox.pack_end(statusbar, False, False, 0)
115
116         self.toplevel = self.vbox
117
118 #         self.show_out_toplevel(None, 'outtable') ###
119 #         self.vbox.show_all()
120
121     #=== Functions ============================================================
122     def start_btn_released(self, btn, start_func):
123         self.stopit = False
124         start_func(self.get_data)
125
126     def get_data(self):
127         for btn in self.outtable_rbtn.get_group():
128             if btn.get_active():
129                 out = btn.get_name()
130         return int(self.qty_spin.get_value()), out
131
132     #=== Output type selecting ================================================
133     def show_out_toplevel(self, btn, outtype, results):
134         print 'Entering <' + outtype + '> output mode...'
135         out_submodule = __import__('debs.' + outtype, None, None, outtype)
136
137         try:
138             self.current_outtoplevel.destroy()
139         except:
140             pass
141
142         out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
143         self.current_outtoplevel = out_toplevel
144         self.vbox.add(out_toplevel)
145         out_toplevel.show_all()
146 #        out_submodule.Gtk_Presentation().show_results(results)