summaryrefslogtreecommitdiff
path: root/portato/gui/utils.py
blob: 47f6fb73ea00ade9d76987d7f93e9866cd0d41f0 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# -*- coding: utf-8 -*-
#
# File: portato/gui/utils.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
# Copyright (C) 2006-2009 René 'Necoro' Neumann
# This is free software.  You may redistribute copies of it under the terms of
# the GNU General Public License version 2.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by René 'Necoro' Neumann <necoro@necoro.net>

from __future__ import absolute_import, with_statement

# some stuff needed
import sys
import logging
import gettext
from threading import Thread

import gtk

# some backend things
from ..backend import flags, system
from ..helper import debug, info
from ..log import set_log_level
from ..constants import APP, LOCALE_DIR

# parser
from ..config_parser import ConfigParser

def get_color (cfg, name):
    return gtk.gdk.color_parse("#%s" % cfg.get(name, section = "COLORS"))

class GtkThread (Thread):
    def run(self):
        # for some reason, I have to install this for each thread ...
        gettext.install(APP, LOCALE_DIR, unicode = True)
        try:
            Thread.run(self)
        except SystemExit:
            raise # let normal thread handle it
        except:
            type, val, tb = sys.exc_info()
            try:
                sys.excepthook(type, val, tb, thread = self.getName())
            except TypeError:
                raise type, val, tb # let normal thread handle it
            finally:
                del type, val, tb

class Config (ConfigParser):
    
    def __init__ (self, cfgFile):
        """Constructor.

        @param cfgFile: path to config file
        @type cfgFile: string"""

        ConfigParser.__init__(self, cfgFile)
        
        # read config
        self.parse()

        # local configs
        self.local = {}

        # session configs
        self.session = {}

    def modify_flags_config (self):
        """Sets the internal config of the L{flags}-module.
        @see: L{flags.set_config()}"""

        flagCfg = {
                "usefile": self.get("useFile"),
                "usePerVersion" : self.get_boolean("usePerVersion"),
                "maskfile" : self.get("maskFile"),
                "maskPerVersion" : self.get_boolean("maskPerVersion"),
                "testingfile" : self.get("keywordFile"),
                "testingPerVersion" : self.get_boolean("keywordPerVersion")}
        flags.set_config(flagCfg)

    def modify_debug_config (self):
        if self.get_boolean("debug"):
            level = logging.DEBUG
        else:
            level = logging.INFO

        set_log_level(level)

    def modify_system_config (self):
        """Sets the system config."""
        system.set_system(self.get("system"))

    def modify_external_configs (self):
        """Convenience function setting all external configs."""
        self.modify_debug_config()
        self.modify_flags_config()
        self.modify_system_config()

    def set_local(self, cpv, name, val):
        """Sets some local config.

        @param cpv: the cpv describing the package for which to set this option
        @type cpv: string (cpv)
        @param name: the option's name
        @type name: string
        @param val: the value to set
        @type val: any"""
        
        if not cpv in self.local:
            self.local[cpv] = {}

        self.local[cpv].update({name:val})

    def get_local(self, cpv, name):
        """Returns something out of the local config.

        @param cpv: the cpv describing the package from which to get this option
        @type cpv: string (cpv)
        @param name: the option's name
        @type name: string
        @return: value stored for the cpv and name or None if not found
        @rtype: any"""

        if not cpv in self.local:
            return None
        if not name in self.local[cpv]:
            return None

        return self.local[cpv][name]

    def set_session (self, name, cat, val):
        self.session[(cat, name)] = val

    def get_session (self, name, cat):
        v = self.session.get((cat, name), None)

        if v == "": v = None
        return v

    def write(self):
        """Writes to the config file and modify any external configs."""
        ConfigParser.write(self)
        self.modify_external_configs()

class GtkTree (object):
    """The implementation of the abstract tree."""

    def __init__ (self, tree, col = 0):
        """Constructor.

        @param tree: original tree
        @type tree: gtk.TreeStore
        @param col: the column where the cpv is stored
        @type col: int"""

        self.tree = tree
        self.cpv_col = col
        self.emergeIt = None
        self.unmergeIt = None
        self.updateIt = None

    def build_append_value (self, cpv, oneshot = False, update = False, downgrade = False, version = None, useChange = []):
        """
        Builds the list, which is going to be passed to append. 

        @param cpv: the cpv
        @type cpv: string (cpv)
        @param oneshot: True if oneshot
        @type oneshot: boolean
        @param update: True if this is an update
        @type update: boolean
        @param downgrade: True if this is a downgrade
        @type downgrade: boolean
        @param version: the version we update from
        @type version: string
        @param useChange: list of changed useflags; use "-use" for removed and "+use" for added flags
        @type useChange: string[]

        @returns: the created list
        @rtype: list
        """

        string = ""

        if oneshot:
            string += "<i>%s</i>" % _("oneshot")

        if update:
            if oneshot: string += "; "
            if version is not None:
                string += "<i>%s</i>" % (_("updating from version %s") % version)
            else:
                string += "<i>%s</i>" % _("updating")

        elif downgrade:
            if oneshot: string += "; "
            if version is not None:
                string += "<i>%s</i>" % (_("downgrading from version %s") % version)
            else:
                string += "<i>%s</i>" % _("downgrading")

        if useChange:
            if update or downgrade or oneshot: string += "; "
            string += "<i><b>%s </b></i>" % _("IUSE changes:")
            useChange.sort()
            string += "<i>%s</i>" % " ".join(useChange)

        return [cpv, string, False]

    def set_in_progress (self, it, to = True):
        """
        Marks the queue where the given iterator belongs as being in progress.

        @param it: one iterator of the queue to mark to
        @type it: Iterator
        @param to: whether to enable or disable
        @type to: boolean
        """

        iter = self.first_iter(it)
        if to:
            self.tree.set_value(iter, 1, "<b>%s</b>" % _("(In Progress)"))
        else:
            self.tree.set_value(iter, 1, "")
        
        self.tree.set_value(iter, 2, to)

    def is_in_progress (self, it):
        """
        Returns whether the queue where the given iterator belongs to, is marked as "being in progress".

        @param it: the iterator
        @type it: Iterator
        @returns: whether the queue is marked "in progress"
        @rtype: boolean
        """
        return self.tree.get_value(it, 2)

    def get_emerge_it (self):
        """
        Returns an iterator signaling the top of the emerge section.

        @returns: emerge-iterator
        @rtype: Iterator
        """
        if self.emergeIt is None:
            self.emergeIt = self.append(None, ["<b>%s</b>" % _("Install"), "", False])
        return self.emergeIt

    def get_unmerge_it (self):
        """
        Returns an iterator signaling the top of the unmerge section.

        @returns: unmerge-iterator
        @rtype: Iterator
        """
        if self.unmergeIt is None:
            self.unmergeIt = self.append(None, ["<b>%s</b>" % _("Uninstall"), "", False])

        return self.unmergeIt

    def get_update_it (self):
        """
        Returns an iterator signaling the top of the update section.

        @returns: unmerge-iterator
        @rtype: Iterator
        """
        if self.updateIt is None:
            self.updateIt = self.append(None, ["<b>%s</b>" % _("Update"), "", False])

        return self.updateIt

    def first_iter (self, it):
        """
        Returns the iterator at the top.

        @param it: the iterator
        @type it: Iterator
        @returns: the top iterator
        @rtype: Iterator
        """
        return self.tree.get_iter_from_string(self.tree.get_string_from_iter(it).split(":")[0])

    def is_in (self, it, in_it):
        return in_it and self.iter_equal(self.first_iter(it), in_it)

    def is_in_emerge (self, it):
        """
        Checks whether an iterator is part of the "Emerge" section.

        @param it: the iterator to check
        @type it: Iterator
        @returns: True if the iter is part; False otherwise
        @rtype: boolean
        """
        return self.is_in(it, self.emergeIt)

    def is_in_unmerge (self, it):
        """
        Checks whether an iterator is part of the "Unmerge" section.

        @param it: the iterator to check
        @type it: Iterator
        @returns: True if the iter is part; False otherwise
        @rtype: boolean
        """
        return self.is_in(it, self.unmergeIt)

    def is_in_update (self, it):
        """
        Checks whether an iterator is part of the "Update" section.

        @param it: the iterator to check
        @type it: Iterator
        @returns: True if the iter is part; False otherwise
        @rtype: boolean
        """
        return self.is_in(it, self.updateIt)
    
    def iter_has_parent (self, it):
        """
        Returns whether the actual iterator has a parent.
        @param it: the iterator
        @type it: Iterator
        @returns: True if it has a parent it, else False.
        @rtype: boolean
        """
        return (self.tree.iter_parent(it) != None)

    def parent_iter (self, it):
        """
        Returns the parent iter.

        @param it: the iterator
        @type it: Iterator
        @returns: Parent iterator or None if the current it has no parent.
        @rtype: Iterator; None
        """
        return self.tree.iter_parent(it)

    def first_child_iter (self, it):
        """
        Returns the first child iter.

        @param it: the iterator
        @type it: Iterator
        @returns: First child iterator or None if the current it has no children.
        @rtype: Iterator; None
        """

        return self.tree.iter_children(it)

    def iter_has_children (self, it):
        """
        Returns whether the actual iterator has children.

        @param it: the iterator
        @type it: Iterator
        @returns: True if it has children, else False.
        @rtype: boolean
        """

        return self.tree.iter_has_child(it)

    def next_iter (self, it):
        """
        Returns the next iter.

        @param it: the iterator
        @type it: Iterator
        @returns: Next iterator or None if the current iter is the last one.
        @rtype: Iterator; None
        """
        return self.tree.iter_next(it)

    def get_value (self, it, column):
        """
        Returns the value of the specific column at the given iterator.

        @param it: the iterator
        @type it: Iterator
        @param column: the column of the iterator from where to get the value
        @type column: int
        @returns: the value
        @rtype: anything
        """

        return self.tree.get_value(it, column)

    def iter_equal (self, it, other_it):
        """
        Checks whether to iterators are equal.

        @param it: the one iterator to compare
        @type it: Iterator
        @param other_it: the other iterator to compare
        @type other_it: Iterator
        @returns: True if both iterators are equal; False otherwise
        @rtype boolean
        """
        return self.tree.get_string_from_iter(it) == self.tree.get_string_from_iter(other_it)

    def append (self, parent = None, values = None):
        """
        Appends some values right after the given parent. If parent is None, it is appended as the first element.

        @param parent: the iterator to append the values right after; if None it symbolizes the top
        @type parent: Iterator
        @param values: a list of values which are going to be appended to the tree
        @type values: list
        @returns: Iterator pointing to the newly appended stuff
        @rtype: Iterator
        """

        return self.tree.append(parent, values)

    def remove (self, it):
        """
        Removes an iterator out of the tree. 
        @attention: The iterator can point to anything hereafter. Do not reuse!
        
        @param it: iterator to remove
        @type it: Iterator
        """
        
        if self.emergeIt and self.iter_equal(it, self.emergeIt) : self.emergeIt = None
        elif self.unmergeIt and self.iter_equal(it, self.unmergeIt) : self.unmergeIt = None
        elif self.updateIt and self.iter_equal(it, self.updateIt) : self.updateIt = None
        
        self.tree.remove(it)

    def get_original (self):
        """
        Returns the original tree-object.
        
        @returns: original tree-object
        @rtype: tree-object
        """
        return self.tree

    def get_cpv_column (self):
        """
        Returns the number of the column where the cpv's are stored.

        @returns: column with cpv's
        @rtype: int
        """
        return self.cpv_col