Switches to bash script for packaging.
[giterdone.git] / giterdone / common.py
blob8e7d4fb3a707f72262bcd1ddb914960bd5203a5f
1 from glib import GError
2 import gtk
4 import vcs.state
6 __all__ = ['emblem_names', 'get_emblem_name', 'compare_versions', 'get_icon']
8 emblem_names = {
9 vcs.state.DIRTY: 'giterdone-dirty',
10 vcs.state.STAGED: 'giterdone-staged',
11 vcs.state.SYNCED: 'giterdone-synced',
12 vcs.state.UNTRACKED: 'giterdone-untracked'
15 icons = None
17 def load_icon(name):
18 """Loads an icon corresponding to a given file name.
20 Arguments:
21 name: A file name for an icon in the 'resources' folder.
23 Returns:
24 A gtk.gdk.Pixbuf representation of the icon.
25 """
26 try:
27 return gtk.icon_theme_get_default().load_icon(name, 20, gtk.ICON_LOOKUP_USE_BUILTIN)
28 except GError:
29 print 'Giterdone: WARNING could not load icon.'
30 return None
32 def init_icons():
33 global icons
34 icons = {}
35 for status_id, emblem_name in emblem_names.iteritems():
36 icon = load_icon(emblem_name)
37 if icon: icons[status_id] = icon
39 def get_emblem_name(status_id):
40 if not status_id in emblem_names: return None
41 return emblem_names[status_id]
43 def get_icon(status_id):
44 if icons == None: init_icons()
45 if not status_id in icons: return None
46 return icons[status_id]
48 def compare_versions(v1, v2):
49 """Like the Java compareTo method, returns the result of comparing to
50 version strings.
52 The given versions should be triples of the form (<major>, <minor>,
53 <revision>) (e.g. (1,2,10)). If v1 is newer than v2, a positive number is
54 returned. If v1 and v2 are the same version, 0 is returned. If v2 is newer
55 than v1, a negative number is returned.
56 """
57 major_diff = v1[0] - v2[0]
58 if major_diff != 0: return major_diff
59 minor_diff = v1[1] - v2[1]
60 if minor_diff != 0: return minor_diff
61 return v1[2] - v2[2]