summaryrefslogtreecommitdiff
path: root/DysonInstaller/snack/__init__.py
blob: 580aabae736fd6265b7551344cd1c358a2ee6ba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/python

"""
Written by Igor Pashev <pashev.igor@gmail.com>

The author has placed this work in the Public Domain,
thereby relinquishing all copyrights. Everyone is free
to use, modify, republish, sell or give away this work
without prior consent from anybody.
"""

from snack import *
import math
import apt.progress.base
import os

class ProgressMessage(object):
    _text = None

    @property
    def text(self):
        return self._text
    @text.setter
    def text(self, text):
        self._text = text
        self._textbox.setText(self._text)
        self._refresh()

    def _refresh(self):
        self._form.draw()
        self._screen.refresh()

    def __init__(self, screen, width=60, title=' ', text=' '):
        self._screen = screen

        self._textbox = TextboxReflowed(width, text)

        self._grid = Grid(1, 1)
        self._grid.setField(self._textbox, 0, 0)

        self._screen.gridWrappedWindow(self._grid, title)

        self._form = Form()
        self._form.add(self._textbox)
        self._refresh()
    
    def __del__(self):
        self._screen.popWindow()

class ProgressBar(object):
    _progress = 0
    _top = 0
    _text = None
    _textbox = None
    _screen = None
    _form = None
    _scale = None
    _grid = None
    _allow_back = False

    @property
    def text(self):
        return self._text
    @text.setter
    def text(self, text):
        self._text = text
        self._textbox.setText(self._text)
        self._refresh()

    @property
    def progress(self):
        return self._progress
    @text.setter
    def progress(self, i):
        if type(i) == 'str':
            i = float(i)
        if type(i) == 'float':
            i = math.ceil(i)
        if i > self._top:
            i = self._top
        i = int(i)
        if i > self._progress or self._allow_back:
            self._progress = int(i)
            self._scale.set(self._progress)
            self._refresh()

    def advance(self, i=1):
        self.progress = self._progress + i

    def reset(self):
        self._progress = 0;
        self._refresh()

    def _refresh(self):
        self._form.draw()
        self._screen.refresh()


    def __init__(self, screen, width=60, title='', text=' ', top=100, allow_back=False):
        self._allow_back = allow_back
        self._screen = screen
        self._top = top

        self._textbox = TextboxReflowed(width, text)

        self._scale = Scale(width, self._top)
        self._scale.set(0)

        self._grid = Grid(1, 2)
        self._grid.setField(self._textbox, 0, 0)
        self._grid.setField(self._scale, 0, 1)

        self._screen.gridWrappedWindow(self._grid, title)

        self._form = Form()
        self._form.add(self._textbox)
        self._form.add(self._scale)
        self._refresh()

    
    def __del__(self):
        self._screen.popWindow()


class BaseProgress(object):
    _progressbar = None
    _screen = None
    _title = None
    _text = ''

    def __init__(self, screen, title, text=''):
        self._screen = screen
        self._title = title
        self._text = text

    def _create_progressbar(self):
        self._progressbar = ProgressBar(self._screen,
                                   title=self._title, text=self._text)

    def _destroy_progressbar(self):
        del self._progressbar
        self._progressbar = None


class DownloadProgress(apt.progress.base.AcquireProgress, BaseProgress):
    def pulse(self, acquire):
        self._progressbar.progress = (
            ((self.current_bytes + self.current_items) * 100.0) /
                        float(self.total_bytes + self.total_items))
        return True

    def start(self):
        self._create_progressbar()

    def stop(self):
        self._destroy_progressbar()

class OpenProgress(apt.progress.base.OpProgress, BaseProgress):
    def __init__(self, screen, title, text=''):
        BaseProgress.__init__(self, screen, title, text)

    def update(self, percent=None):
        if not self._progressbar:
            self._create_progressbar()
        if percent:
            self._progressbar.progress = percent

    def done(self):
        self._destroy_progressbar()

class InstallProgress(apt.progress.base.InstallProgress, BaseProgress):
    _logfile = '/dev/null'

    def __init__(self, screen, title, text='', logfile=None):
        apt.progress.base.InstallProgress.__init__(self)
        BaseProgress.__init__(self, screen, title, text)
        if logfile:
            self._logfile = logfile


    def start_update(self):
        self._create_progressbar()

    def finish_update(self):
        self._destroy_progressbar()

    def status_change(self, pkg, percent, status):
        self._progressbar.progress = percent
        self._progressbar.text = status

    def dpkg_status_change(self, pkg, status):
        self._progressbar.progress = percent
        self._progressbar.text = status + ' ' + pkg

    def fork(self):
        """ Shut up dpkg """
        pid = os.fork()
        if pid == 0:
            log = os.open(self._logfile, os.O_WRONLY + os.O_CREAT)
            os.dup2(log, 1)
            os.dup2(log, 2)
        return pid