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