Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils.py
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2011-05-11 18:31:37 (GMT)
committer Walter Bender <walter.bender@gmail.com>2011-05-11 18:31:37 (GMT)
commitfc5153e84281a179a0a2881f945d73f96abbe577 (patch)
tree475dcedcba72cf6ffa37467e4bd42fd32a75bb3a /utils.py
parent5be0685e8203873059f00d1e7017732b97d43528 (diff)
common utilities
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py233
1 files changed, 233 insertions, 0 deletions
diff --git a/utils.py b/utils.py
new file mode 100644
index 0000000..6913a8b
--- /dev/null
+++ b/utils.py
@@ -0,0 +1,233 @@
+# -*- coding: utf-8 -*-
+#Copyright (c) 2011 Walter Bender
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+import gtk
+import os
+
+from gettext import gettext as _
+
+from sugar.graphics.toolbutton import ToolButton
+
+
+def get_path(activity, subpath):
+ """ Find a Rainbow-approved place for temporary files. """
+ try:
+ return(os.path.join(activity.get_activity_root(), subpath))
+ except:
+ # Early versions of Sugar didn't support get_activity_root()
+ return(os.path.join(os.environ['HOME'], ".sugar/default",
+ "org.sugarlabs.PortfolioActivity", subpath))
+
+
+def _luminance(color):
+ ''' Calculate luminance value '''
+ return int(color[0:2], 16) * 0.3 + int(color[2:4], 16) * 0.6 + int(color[4:6]) * 0.1
+
+
+def lighter_color(colors):
+ ''' Which color is lighter? Use that one for the text background '''
+ if _luminance(colors[0][1:6]) > _luminance(colors[1][1:6]):
+ return 0
+ return 1
+
+
+def svg_str_to_pixbuf(svg_string):
+ ''' Load pixbuf from SVG string '''
+ pl = gtk.gdk.PixbufLoader('svg')
+ pl.write(svg_string)
+ pl.close()
+ pixbuf = pl.get_pixbuf()
+ return pixbuf
+
+
+def load_svg_from_file(file_path, width, height):
+ '''Create a pixbuf from SVG in a file. '''
+ return gtk.gdk.pixbuf_new_from_file_at_size(file_path, width, height)
+
+
+def button_factory(icon_name, tooltip, callback, toolbar, cb_arg=None,
+ accelerator=None):
+ '''Factory for making toolbar buttons'''
+ my_button = ToolButton(icon_name)
+ my_button.set_tooltip(tooltip)
+ my_button.props.sensitive = True
+ if accelerator is not None:
+ my_button.props.accelerator = accelerator
+ if cb_arg is not None:
+ my_button.connect('clicked', callback, cb_arg)
+ else:
+ my_button.connect('clicked', callback)
+ if hasattr(toolbar, 'insert'): # the main toolbar
+ toolbar.insert(my_button, -1)
+ else: # or a secondary toolbar
+ toolbar.props.page.insert(my_button, -1)
+ my_button.show()
+ return my_button
+
+
+def label_factory(label, toolbar):
+ ''' Factory for adding a label to a toolbar '''
+ my_label = gtk.Label(label)
+ my_label.set_line_wrap(True)
+ my_label.show()
+ toolitem = gtk.ToolItem()
+ toolitem.add(my_label)
+ toolbar.insert(toolitem, -1)
+ toolitem.show()
+ return my_label
+
+
+def separator_factory(toolbar, visible=True, expand=False):
+ ''' Factory for adding a separator to a toolbar '''
+ separator = gtk.SeparatorToolItem()
+ separator.props.draw = visible
+ separator.set_expand(expand)
+ toolbar.insert(separator, -1)
+ separator.show()
+
+
+def slider_factory(tooltip, callback, toolbar, cb_arg=None):
+ ''' Factory for adding a slider to a toolbar '''
+ adjustment = gtk.Adjustment(2, 1, 30, 1, 5, 0)
+ adjustment.connect('value_changed', callback)
+ range = gtk.HScale(adjustment)
+ range.set_size_request(240, 15)
+ range_tool = gtk.ToolItem()
+ range_tool.add(range)
+
+ toolbar.insert(range_tool, -1)
+ return adjustment
+
+
+def image_to_base64(pixbuf, path_name):
+ """ Convert an image to base64-encoded data """
+ file_name = os.path.join(path_name, 'imagetmp.png')
+ if pixbuf != None:
+ pixbuf.save(file_name, "png")
+ base64 = os.path.join(path_name, 'base64tmp')
+ cmd = "base64 <" + file_name + " >" + base64
+ subprocess.check_call(cmd, shell=True)
+ file_handle = open(base64, 'r')
+ data = file_handle.read()
+ file_handle.close()
+ return data
+
+
+def get_pixbuf_from_journal(dsobject, w, h):
+ """ Load a pixbuf from a Journal object. """
+ # _pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(dsobject.file_path,
+ try:
+ pixbufloader = \
+ gtk.gdk.pixbuf_loader_new_with_mime_type('image/png')
+ pixbufloader.set_size(min(300, int(w)), min(225, int(h)))
+ pixbufloader.write(dsobject.metadata['preview'])
+ pixbufloader.close()
+ pixbuf = pixbufloader.get_pixbuf()
+ except:
+ pixbuf = None
+ return pixbuf
+
+
+def genblank(w, h, colors):
+ svg = SVG()
+ svg.set_colors(colors)
+ svg_string = svg.header(w, h)
+ svg_string += svg.footer()
+ return svg_string
+
+
+def get_hardware():
+ """ Determine whether we are using XO 1.0, 1.5, or "unknown" hardware """
+ if _get_dmi('product_name') != 'XO':
+ return 'UNKNOWN'
+ version = _get_dmi('product_version')
+ if version == '1':
+ return 'XO1'
+ elif version == '1.5':
+ return 'XO15'
+ else:
+ return 'UNKNOWN'
+
+
+def _get_dmi(node):
+ path = os.path.join('/sys/class/dmi/id', node)
+ try:
+ return open(path).readline().strip()
+ except:
+ return None
+
+
+class SVG:
+ ''' SVG generators '''
+
+ def __init__(self):
+ self._scale = 1
+ self._stroke_width = 1
+ self._fill = '#FFFFFF'
+ self._stroke = '#FFFFFF'
+
+ def _svg_style(self, extras=""):
+ return "%s%s%s%s%s%f%s%s%s" % ("style=\"fill:", self._fill, ";stroke:",
+ self._stroke, ";stroke-width:",
+ self._stroke_width, ";", extras,
+ "\" />\n")
+
+ def _svg_rect(self, w, h, rx, ry, x, y):
+ svg_string = " <rect\n"
+ svg_string += " width=\"%f\"\n" % (w)
+ svg_string += " height=\"%f\"\n" % (h)
+ svg_string += " rx=\"%f\"\n" % (rx)
+ svg_string += " ry=\"%f\"\n" % (ry)
+ svg_string += " x=\"%f\"\n" % (x)
+ svg_string += " y=\"%f\"\n" % (y)
+ self.set_stroke_width(1.0)
+ svg_string += self._svg_style()
+ return svg_string
+
+ def _background(self, w=80, h=60, scale=1):
+ return self._svg_rect((w - 0.5) * scale, (h - 0.5) * scale,
+ 1, 1, 0.25, 0.25)
+
+ def header(self, w=80, h=60, scale=1, background=True):
+ svg_string = "<?xml version=\"1.0\" encoding=\"UTF-8\""
+ svg_string += " standalone=\"no\"?>\n"
+ svg_string += "<!-- Created with Emacs -->\n"
+ svg_string += "<svg\n"
+ svg_string += " xmlns:svg=\"http://www.w3.org/2000/svg\"\n"
+ svg_string += " xmlns=\"http://www.w3.org/2000/svg\"\n"
+ svg_string += " version=\"1.0\"\n"
+ svg_string += "%s%f%s" % (" width=\"", scale * w * self._scale,
+ "\"\n")
+ svg_string += "%s%f%s" % (" height=\"", scale * h * self._scale,
+ "\">\n")
+ svg_string += "%s%f%s%f%s" % ("<g\n transform=\"matrix(",
+ self._scale, ",0,0,", self._scale,
+ ",0,0)\">\n")
+ if background:
+ svg_string += self._background(w, h, scale)
+ return svg_string
+
+ def footer(self):
+ svg_string = "</g>\n"
+ svg_string += "</svg>\n"
+ return svg_string
+
+ def set_scale(self, scale=1.0):
+ self._scale = scale
+
+ def set_colors(self, colors):
+ self._stroke = colors[0]
+ self._fill = colors[1]
+
+ def set_stroke_width(self, stroke_width=1.0):
+ self._stroke_width = stroke_width